Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for "onInit" callback usage? #59

Open
karli2000 opened this issue Jan 17, 2025 · 2 comments
Open

Example for "onInit" callback usage? #59

karli2000 opened this issue Jan 17, 2025 · 2 comments

Comments

@karli2000
Copy link

karli2000 commented Jan 17, 2025

Thank you for your great library!

I am trying to use the "onInit" callback to set a few PRAGMAs, but i am unable to get it working.
What is the right syntax? I tried:

const { sql } = new SQLocal({
    databasePath: 'file:test.sqlite3',
    verbose: true,
    onInit: (sql) => {
      return [sql`PRAGMA XXXXX`, sql`PRAGMA YYYY`]
    }
})

and get the error:

DataCloneError: Failed to execute 'postMessage' on 'Worker': (sql) => {
return [sql`PRAGMA XXXXX`, sql`PRAGMA YYYY`]
} could not be cloned.

It doesn't work, even when i try other ways like it is explained in the docs for the "batch" command.
Could you please give me a working example?

@DallasHoff
Copy link
Owner

Are you sure that the example code you posted is representative of your actual code? That syntax looks correct and should not result in the error you gave.

Here's an example based on one of the library's unit tests:

const { sql } = new SQLocal({
  databasePath: 'test.sqlite3',
  onInit: (sql) => {
    return [sql`PRAGMA foreign_keys = ON`];
  },
});

const [foreignKeys] = await sql`PRAGMA foreign_keys`;
// foreignKeys should be { foreign_keys: 1 }

@karli2000
Copy link
Author

Thank you for the reply!
I realized that i was using an old version of sqlocal, now it works!
First i thought it has to do with my sqlite3.wasm file because i use the binaries from this project:
https://github.com/utelle/SQLite3MultipleCiphers
but it was just a problem with the wrong version!

Keep up the great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants