Git Product home page Git Product logo

Comments (9)

jepiqueau avatar jepiqueau commented on June 12, 2024

@CosminCraciun this is a known error. Someone has modify the driver wrongly it has been reported to the typeOrm team. See the close issue @capacitor-community/sqlite #512 PRAMA table_info throws an error. I suggested a script to postinstall in waiting the fix from the typeOrm team.

from sqlite.

CosminCraciun avatar CosminCraciun commented on June 12, 2024

I had already included that in the post-install. Also the error only occurs in WAL mode, not on any of the others.
The error does not occur in CapacitorQueryRunner.js.
It's actually in CapacitorDriver.js, when journal mode is set. That calls directly .run from the sqlite driver, rather than using the capacitor query runner

from sqlite.

jepiqueau avatar jepiqueau commented on June 12, 2024

@CosminCraciun you could try to modify the script modify-typeorm.cjs like this

const fs = require('fs');

const modTypeOrmCapacitor = (filePath, lineToModify, replacementText) => {
  fs.readFile(filePath, 'utf8', (err, data) => {
    if (err) {
      console.error('Error reading file:', err);
      return;
    }
  
    // Split data by line
    const lines = data.split('\n');
  
    // Modify the specific line
    if (lines.length >= lineToModify) {
      lines[lineToModify - 1] = replacementText; // Line numbers are 1-based
    } else {
      console.error('Line number to modify is out of range.');
      return;
    }
  
    // Join lines back together
    const modifiedData = lines.join('\n');
  
    // Write the modified data back to the file
    fs.writeFile(filePath, modifiedData, 'utf8', (err) => {
      if (err) {
        console.error('Error writing file:', err);
        return;
      }
     
      console.log('File modified successfully.');
    });
  });
}
/* Moddify CapacitorQueryRunner.js */
let filePath = './node_modules/typeorm/driver/capacitor/CapacitorQueryRunner.js';
let lineToModify = 61;
let replacementText = '    else if (["INSERT", "UPDATE", "DELETE"].indexOf(command) !== -1) {';

modTypeOrmCapacitor(filePath, lineToModify, replacementText)

/* Moddify CapacitorDriver.js */
filePath = './node_modules/typeorm/driver/capacitor/CapacitorDriver.js';
lineToModify = 62;
replacementText = '        await connection.execute(`PRAGMA foreign_keys = ON`);';

modTypeOrmCapacitor(filePath, lineToModify, replacementText)

/* Moddify CapacitorDriver.js */
filePath = './node_modules/typeorm/driver/capacitor/CapacitorDriver.js';
lineToModify = 65;
replacementText = '            await connection.execute(`PRAGMA journal_mode = ${this.options.journalMode}`);';

modTypeOrmCapacitor(filePath, lineToModify, replacementText)

if it works you must report the error on the typeorm package

from sqlite.

CosminCraciun avatar CosminCraciun commented on June 12, 2024

There is a problem with modifying CapacitorDriver.js in this way.
Because the operations are asyncronous, the modifications are done in parallel, so we hit a concurency issue. I end up with just the second modification instead of both. Also, looking into the API section, .execute still does stuff in a transaction as default. I did some changes to the script:

const fs = require('fs');

const modTypeOrmCapacitor = (filePath, lineToModify, replacementText) => {
  fs.readFile(filePath, 'utf8', (err, data) => {
    if (err) {
      console.error('Error reading file:', err);
      return;
    }
  
    // Split data by line
    const lines = data.split('\n');
  
    // Modify the specific line
    if (lines.length >= lineToModify) {
      lines[lineToModify - 1] = replacementText; // Line numbers are 1-based
    } else {
      console.error('Line number to modify is out of range.');
      return;
    }
  
    // Join lines back together
    const modifiedData = lines.join('\n');
  
    // Write the modified data back to the file
    fs.writeFile(filePath, modifiedData, 'utf8', (err) => {
      if (err) {
        console.error('Error writing file:', err);
        return;
      }
     
      console.log('File modified successfully.');
    });
  });
}

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`);'
            );
            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}`);
    }
}


/* Moddify CapacitorQueryRunner.js */
let filePath = './node_modules/typeorm/driver/capacitor/CapacitorQueryRunner.js';
let lineToModify = 61;
let replacementText = '    else if (["INSERT", "UPDATE", "DELETE"].indexOf(command) !== -1) {';

modTypeOrmCapacitor(filePath, lineToModify, replacementText)

/* Moddify CapacitorDriver.js */
correctBugInCapacitorDriver('./node_modules/typeorm/driver/capacitor/CapacitorDriver.js');
correctBugInCapacitorDriver('./node_modules/typeorm/browser/driver/capacitor/CapacitorDriver.js');

The problem is, I'm still getting the error on WAL. I'm still investigating why. I've tried removing the lines alltogether instead of replacing, and I still get the error. Which doesn't make sense at all.

from sqlite.

CosminCraciun avatar CosminCraciun commented on June 12, 2024

Apparently, angular was caching my node_modules files.
The script above works fine.
So we need the .execute(..., false) and editing the file in one go.

from sqlite.

jepiqueau avatar jepiqueau commented on June 12, 2024

@CosminCraciun ok i modify the script based on your proposal

const fs = require('fs');

const modTypeOrmCapacitor = (filePath, lineToModify, replacementText) => {
  fs.readFile(filePath, 'utf8', (err, data) => {
    if (err) {
      console.error('Error reading file:', err);
      return;
    }
  
    // Split data by line
    const lines = data.split('\n');
  
    // Modify the specific line
    if (lines.length >= lineToModify) {
      lines[lineToModify - 1] = replacementText; // Line numbers are 1-based
    } else {
      console.error('Line number to modify is out of range.');
      return;
    }
  
    // Join lines back together
    const modifiedData = lines.join('\n');
  
    // Write the modified data back to the file
    fs.writeFile(filePath, modifiedData, 'utf8', (err) => {
      if (err) {
        console.error('Error writing file:', err);
        return;
      }
     
      console.log('File modified successfully.');
    });
  });
}
/* Moddify CapacitorQueryRunner.js */
let filePath = './node_modules/typeorm/driver/capacitor/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);
        });
    });
  } 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}`);
  }
}
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');

i remove the cache files of the browser close it and reopen it
i run

npm run build --force
npm run dev

Now it is working

from sqlite.

jepiqueau avatar jepiqueau commented on June 12, 2024

@CosminCraciun Sorry i did not see your previous answer yes it was a cash issue. I add PRAMA to the list for execute command. I test it on a react-ionic-vite-app

from sqlite.

CosminCraciun avatar CosminCraciun commented on June 12, 2024

Perfect!
Thanks a lot!
For me, I actually had to delete the .angular folder to remove the cache.

from sqlite.

jepiqueau avatar jepiqueau commented on June 12, 2024

@CosminCraciun OK i close the issue then

from sqlite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.