Elijah M
Elijah M

Reputation: 835

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.

Upvotes: 1

Views: 529

Answers (2)

Michihiro Muta
Michihiro Muta

Reputation: 46

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

Upvotes: 3

Eduards Sizovs
Eduards Sizovs

Reputation: 187

Option A (execute raw SQL via Kysely):

const dialect = new SqliteDialect({
    database: new SQLite(":memory:")
});

export const db = new Kysely<Database>({
    dialect
})

sql`PRAGMA journal_mode=WAL`.execute(db)

Option B (execute SQL directly on SQLite instance):

const sqlite = new SQLite(":memory:")
const dialect = new SqliteDialect({
    database: sqlite
});

sqlite.exec('PRAGMA journal_mode=WAL')

Upvotes: 0

Related Questions