Skip to content

Commit

Permalink
Update TypeORM-Usage-From-5.6.0.md
Browse files Browse the repository at this point in the history
feat: added `isModifiedKey` to both bug correction examples to prevent double modifications causing file to contain errors.
docs: Additional step for adding to `package.json` -> `postinstall` script
docs: Alternative for esmodule so you don't get type errors
  • Loading branch information
SaintPepsi committed Apr 28, 2024
1 parent ffa6662 commit c32d432
Showing 1 changed file with 87 additions and 64 deletions.
151 changes: 87 additions & 64 deletions docs/TypeORM-Usage-From-5.6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,83 +204,106 @@ export const getCountOfElements = (async (connection: DataSource, entity:any):

- create a `scripts` directory at the root of the App.

- create a `modify-typeorm.cjs` file under this directory with:
- create a `modify-typeorm.cjs`/`modify-typeorm.js (for ESModule)` file under this directory with:

```js
const fs = require('fs');
// For ESModule:
// import fs from 'fs';

/* Moddify CapacitorQueryRunner.js */

/* Modify CapacitorQueryRunner.js */
const correctBugInCapacitorQueryRunner = (file) => {
if (fs.existsSync(file)) {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
return console.error(err);
}

const index = `"DROP",`
if (index === -1) {
console.warn('Line not found. Package probably fixed.');
return;
}

var result = data.replace(
` "DROP",`,
` "DROP",
"PRAGMA"`

);
result = result.replace(
'else if (["INSERT", "UPDATE", "DELETE", "PRAGMA"].indexOf(command) !== -1) {',
'else if (["INSERT", "UPDATE", "DELETE"].indexOf(command) !== -1) {'
);

fs.writeFile(file, result, 'utf8', function (err) {
if (err) return console.error(err);
if (fs.existsSync(file)) {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
return console.error(err);
}

// This key helps to identify that this file has been modified by this script
const isModifiedKey = "/** correctBugInCapacitorQueryRunner */";
const isModifiedIndex = data.indexOf(isModifiedKey);

if (isModifiedIndex !== -1) {
console.warn(`${isModifiedKey} found. Package probably fixed.`);
return;
}

const index = data.indexOf(`"DROP",`)
if (index === -1) {
console.warn('Line not found. Package probably fixed.');
return;
}

var result = data.replace(
` "DROP",`,
` "DROP",
"PRAGMA"`

);
result = result.replace(
'else if (["INSERT", "UPDATE", "DELETE", "PRAGMA"].indexOf(command) !== -1) {',
'else if (["INSERT", "UPDATE", "DELETE"].indexOf(command) !== -1) {'
);

result += isModifiedKey;

fs.writeFile(file, result, 'utf8', function (err) {
if (err) return console.error(err);
});
});
});
} else {
utils.warn(`Couldn't find file ${file}`);
}
} else {
utils.warn(`Couldn't find file ${file}`);
}

}
/* Moddify CapacitorDriver.js */
const correctBugInCapacitorDriver = (file) => {
if (fs.existsSync(file)) {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
return console.error(err);
}

const index = data.indexOf('await connection.run(`PRAGMA foreign_keys = ON`);');
if (index === -1) {
console.warn('Line not found. Package probably fixed.');
return;
}

var result = data.replace(
'await connection.run(`PRAGMA foreign_keys = ON`);',
'await connection.execute(`PRAGMA foreign_keys = ON`, false);'
);
result = result.replace(
'await connection.run(`PRAGMA journal_mode = ${this.options.journalMode}`);',
'await connection.execute(`PRAGMA journal_mode = ${this.options.journalMode}`, false);'
);

fs.writeFile(file, result, 'utf8', function (err) {
if (err) return console.error(err);
});
});
} else {
utils.warn(`Couldn't find file ${file}`);
}
if (fs.existsSync(file)) {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
return console.error(err);
}

// This key helps to identify that this file has been modified by this script
const isModifiedKey = "/** correctBugInCapacitorDriver */";
const isModifiedIndex = data.indexOf(isModifiedKey);

if (isModifiedIndex !== -1) {
console.warn(`${isModifiedKey} found. Package probably fixed.`);
return;
}

const index = data.indexOf('await connection.run(`PRAGMA foreign_keys = ON`);');
if (index === -1) {
console.warn('Line not found. Package probably fixed.');
return;
}

var result = data.replace(
'await connection.run(`PRAGMA foreign_keys = ON`);',
'await connection.execute(`PRAGMA foreign_keys = ON`, false);'
);

result = result.replace(
'await connection.run(`PRAGMA journal_mode = ${this.options.journalMode}`);',
'await connection.execute(`PRAGMA journal_mode = ${this.options.journalMode}`, false);'
);

result += isModifiedKey;

fs.writeFile(file, result, 'utf8', function (err) {
if (err) return console.error(err);
});
});
} else {
utils.warn(`Couldn't find file ${file}`);
}
}
correctBugInCapacitorQueryRunner('./node_modules/typeorm/driver/capacitor/CapacitorQueryRunner.js');
correctBugInCapacitorQueryRunner('./node_modules/typeorm/browser/driver/capacitor/CapacitorQueryRunner.js');
correctBugInCapacitorDriver('./node_modules/typeorm/driver/capacitor/CapacitorDriver.js');
correctBugInCapacitorDriver('./node_modules/typeorm/browser/driver/capacitor/CapacitorDriver.js');

```

- In your `package.json` add `"postinstall": "node ./scripts/modify-typeorm.cjs"` or for ESModule: `"postinstall": "npx tsx ./scripts/modify-typeorm.js"`



## Create the initial migration file
Expand Down

0 comments on commit c32d432

Please sign in to comment.