From 9686469c28885de20605cfaf8e29b7cffedf06a4 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 11 Oct 2023 23:44:28 -0500 Subject: [PATCH] Move PRAGMA utils to a separate file, log out PRAGMA values on start --- src/db.ts | 17 +++++++---------- src/pragma.ts | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 src/pragma.ts diff --git a/src/db.ts b/src/db.ts index 3d6b4db..627679b 100644 --- a/src/db.ts +++ b/src/db.ts @@ -3,6 +3,7 @@ import path from 'node:path'; import { DenoSqlite3, DenoSqliteDialect, FileMigrationProvider, Kysely, Migrator, sql } from '@/deps.ts'; import { Conf } from '@/config.ts'; +import { getPragma, setPragma } from '@/pragma.ts'; interface DittoDB { events: EventRow; @@ -61,22 +62,18 @@ const db = new Kysely({ }), }); +// Set PRAGMA values. await Promise.all([ setPragma(db, 'synchronous', 'normal'), setPragma(db, 'temp_store', 'memory'), setPragma(db, 'mmap_size', Conf.sqlite.mmapSize), ]); -/** Set the PRAGMA and then read back its value to confirm. */ -async function setPragma(db: Kysely, pragma: string, value: string | number) { - await sql.raw(`PRAGMA ${pragma} = ${value}`).execute(db); - const result = (await sql.raw(`PRAGMA ${pragma}`).execute(db)).rows[0] as object; - - for (const [key, value] of Object.entries(result)) { - console.log(`PRAGMA ${key} = ${value};`); - break; - } -} +// Log out PRAGMA values for debugging. +['journal_mode', 'synchronous', 'temp_store', 'mmap_size'].forEach(async (pragma) => { + const value = await getPragma(db, pragma); + console.log(`PRAGMA ${pragma} = ${value};`); +}); const migrator = new Migrator({ db, diff --git a/src/pragma.ts b/src/pragma.ts new file mode 100644 index 0000000..09a0610 --- /dev/null +++ b/src/pragma.ts @@ -0,0 +1,15 @@ +import { type Kysely, sql } from '@/deps.ts'; + +/** Set the PRAGMA and then read back its value to confirm. */ +function setPragma(db: Kysely, pragma: string, value: string | number) { + return sql.raw(`PRAGMA ${pragma} = ${value}`).execute(db); +} + +/** Get value of PRAGMA from the database. */ +async function getPragma(db: Kysely, pragma: string) { + const result = await sql.raw(`PRAGMA ${pragma}`).execute(db); + const row = result.rows[0] as Record; + return row[pragma]; +} + +export { getPragma, setPragma };