Skip to content

Commit

Permalink
Merge pull request #549 from SaintPepsi/patch-1
Browse files Browse the repository at this point in the history
Update TypeORM-Usage-From-5.6.0.md
  • Loading branch information
jepiqueau committed Apr 28, 2024
2 parents dcbd8d8 + a058cd6 commit 1d3f57f
Showing 1 changed file with 79 additions and 52 deletions.
131 changes: 79 additions & 52 deletions docs/TypeORM-Usage-From-5.6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,83 +204,110 @@ 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 (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}`);
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);
});
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}`);
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 1d3f57f

Please sign in to comment.