Move PRAGMA utils to a separate file, log out PRAGMA values on start
This commit is contained in:
parent
f35d38d83b
commit
9686469c28
17
src/db.ts
17
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<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,
|
||||
|
|
|
@ -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 };
|
Loading…
Reference in New Issue