Move PRAGMA utils to a separate file, log out PRAGMA values on start

This commit is contained in:
Alex Gleason 2023-10-11 23:44:28 -05:00
parent f35d38d83b
commit 9686469c28
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 22 additions and 10 deletions

View File

@ -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<DittoDB>({
}),
});
// 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<any>, 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,

15
src/pragma.ts Normal file
View File

@ -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<any>, 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<any>, pragma: string) {
const result = await sql.raw(`PRAGMA ${pragma}`).execute(db);
const row = result.rows[0] as Record<string, unknown>;
return row[pragma];
}
export { getPragma, setPragma };