Search code examples
node.jssqlitekysely

Set PRAGMA after database connection in Kysely


I want to run PRAGMA journal_mode = WAL on my database connection as soon as it is connected using the Kysely library.

Here's what I've tried:

const dialect = new SqliteDialect({
    database: new SQLite(":memory:"),
    onCreateConnection: async connnection => {
        const pragmaQuery = sql<string>`PRAGMA journal_mode = WAL`;
        connection.executeQuery(pragmaQuery.compile());
    },
});

However, pragmaQuery.compile() requires a reference to the database, which I don't have in this function's scope. (I instantiate db below, which needs dialect as an argument.)

I'm having trouble using the DatabaseConnection interface, as it has very few callable methods.


Solution

  • const dialect = new SqliteDialect({
        database: new SQLite(":memory:"),
        
        onCreateConnection: async connnection => {
           await connnection.executeQuery(CompiledQuery.raw(`PRAGMA journal_mode = WAL`));
        },
    });