Skip to content

Proper init for IDBBatchAtomicVFS #50

Answered by rhashimoto
steida asked this question in Q&A
Proper init for IDBBatchAtomicVFS #50
May 12, 2022 · 1 answer

Is this configuration correct? I'm not sure about SQLite.SQLITE_OPEN_CREATE and the other constants. I'm not sure also about PRAGMA and shared connection, are they correct? Thank you

 export const initDb: TaskEither<UnknownError, Database> = taskEither.tryCatch(
  async () => {
    const asyncModule = await SQLiteAsyncESMFactory();
    const sqlite3 = SQLite.Factory(asyncModule);
    sqlite3.vfs_register(
      new IDBBatchAtomicVFS("dbName", { durability: "relaxed" })
    );
    const connection = await sqlite3.open_v2(
      "myAppName",
      SQLite.SQLITE_OPEN_CREATE |
        SQLite.SQLITE_OPEN_READWRITE |
        SQLite.SQLITE_OPEN_URI,
      "dbName"
    );
    await sqlite3.exec(connection, `PRAGMA journal_mode=delete;`);

    return { connection, sqlite3 };
  },
  (error): UnknownError => ({ type: "UnknownError", error })
);

It looks fine to me. The default journal_mode is delete, so that PRAGMA isn't necessary unless it's a placeholder to try a different setting later.

The default open flags are CREATE | READWRITE so if you don't really need URI then you could just pass undefined. Adding URI doesn't hurt anything but you might not need it - the reasons to use it are to pass extra parameters to the VFS (which IDBBatchAtomic doesn't use) and that it enables passing a URI to ATTACH DATABASE statements, which can be handy if you need it, e.g. to attach another database read-only, with a non-default VFS, or with VFS-specific parameters.

Replies

1 suggested answer

It looks fine to me. The default journal_mode is delete, so that PRAGMA isn't necessary unless it's a placeholder to try a different setting later.

The default open flags are CREATE | READWRITE so if you don't really need URI then you could just pass undefined. Adding URI doesn't hurt anything but you might not need it - the reasons to use it are to pass extra parameters to the VFS (which IDBBatchAtomic doesn't use) and that it enables passing a URI to ATTACH DATABASE statements, which can be handy if you need it, e.g. to attach another database read-only, with a non-default VFS, or with VFS-specific parameters.

0 replies
Answer selected by steida
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants