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 { DenoSqlite3, DenoSqliteDialect, FileMigrationProvider, Kysely, Migrator, sql } from '@/deps.ts';
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
|
import { getPragma, setPragma } from '@/pragma.ts';
|
||||||
|
|
||||||
interface DittoDB {
|
interface DittoDB {
|
||||||
events: EventRow;
|
events: EventRow;
|
||||||
|
@ -61,22 +62,18 @@ const db = new Kysely<DittoDB>({
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Set PRAGMA values.
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
setPragma(db, 'synchronous', 'normal'),
|
setPragma(db, 'synchronous', 'normal'),
|
||||||
setPragma(db, 'temp_store', 'memory'),
|
setPragma(db, 'temp_store', 'memory'),
|
||||||
setPragma(db, 'mmap_size', Conf.sqlite.mmapSize),
|
setPragma(db, 'mmap_size', Conf.sqlite.mmapSize),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/** Set the PRAGMA and then read back its value to confirm. */
|
// Log out PRAGMA values for debugging.
|
||||||
async function setPragma(db: Kysely<any>, pragma: string, value: string | number) {
|
['journal_mode', 'synchronous', 'temp_store', 'mmap_size'].forEach(async (pragma) => {
|
||||||
await sql.raw(`PRAGMA ${pragma} = ${value}`).execute(db);
|
const value = await getPragma(db, pragma);
|
||||||
const result = (await sql.raw(`PRAGMA ${pragma}`).execute(db)).rows[0] as object;
|
console.log(`PRAGMA ${pragma} = ${value};`);
|
||||||
|
});
|
||||||
for (const [key, value] of Object.entries(result)) {
|
|
||||||
console.log(`PRAGMA ${key} = ${value};`);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const migrator = new Migrator({
|
const migrator = new Migrator({
|
||||||
db,
|
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