diff --git a/packages/onebot/src/media-store.ts b/packages/onebot/src/media-store.ts index fa475747..79ccd5aa 100644 --- a/packages/onebot/src/media-store.ts +++ b/packages/onebot/src/media-store.ts @@ -1,7 +1,6 @@ import type { MessageElement } from '@snowluma/protocol/events'; -import fs from 'fs'; import { DatabaseSync, type StatementSync } from 'node:sqlite'; -import path from 'path'; +import { openSqliteDb } from './sqlite-open'; export interface CachedImage { file: string; @@ -77,12 +76,7 @@ export class MediaStore { constructor(dbPath: string, maxEntriesPerType = DEFAULT_KEEP_ENTRIES) { this.maxEntriesPerType = Math.max(64, maxEntriesPerType); - const dir = path.dirname(dbPath); - fs.mkdirSync(dir, { recursive: true }); - - this.db = new DatabaseSync(dbPath); - this.db.exec('PRAGMA journal_mode = WAL'); - this.db.exec('PRAGMA synchronous = NORMAL'); + this.db = openSqliteDb(dbPath); this.initSchema(); this.upsertEntry = this.db.prepare( diff --git a/packages/onebot/src/message-store.ts b/packages/onebot/src/message-store.ts index 5b87f7f1..c79da5cc 100644 --- a/packages/onebot/src/message-store.ts +++ b/packages/onebot/src/message-store.ts @@ -1,7 +1,6 @@ -import fs from 'fs'; import { DatabaseSync, type StatementSync } from 'node:sqlite'; -import path from 'path'; import type { JsonObject, MessageMeta } from './types'; +import { openSqliteDb } from './sqlite-open'; export class MessageStore { private readonly db: DatabaseSync; @@ -15,14 +14,9 @@ export class MessageStore { private readonly stmtListEventsLatest: StatementSync; constructor(dbPath: string) { - const dir = path.dirname(dbPath); - fs.mkdirSync(dir, { recursive: true }); - // Replace .json extension with .db if present const finalPath = dbPath.replace(/\.json$/, '.db'); - this.db = new DatabaseSync(finalPath); - this.db.exec('PRAGMA journal_mode = WAL'); - this.db.exec('PRAGMA synchronous = NORMAL'); + this.db = openSqliteDb(finalPath); this.initSchema(); // Prepare once. Statements survive for the lifetime of the diff --git a/packages/onebot/src/reaction-store.ts b/packages/onebot/src/reaction-store.ts index 388b6715..e46e9011 100644 --- a/packages/onebot/src/reaction-store.ts +++ b/packages/onebot/src/reaction-store.ts @@ -1,6 +1,5 @@ -import fs from 'fs'; import { DatabaseSync, type StatementSync } from 'node:sqlite'; -import path from 'path'; +import { openSqliteDb } from './sqlite-open'; export class ReactionStore { private readonly db: DatabaseSync; @@ -11,11 +10,7 @@ export class ReactionStore { private readonly stmtCountByEmoji: StatementSync; constructor(dbPath: string) { - const dir = path.dirname(dbPath); - fs.mkdirSync(dir, { recursive: true }); - this.db = new DatabaseSync(dbPath); - this.db.exec('PRAGMA journal_mode = WAL'); - this.db.exec('PRAGMA synchronous = NORMAL'); + this.db = openSqliteDb(dbPath); this.initSchema(); this.stmtUpsert = this.db.prepare( diff --git a/packages/onebot/src/sqlite-open.ts b/packages/onebot/src/sqlite-open.ts new file mode 100644 index 00000000..8019c9a8 --- /dev/null +++ b/packages/onebot/src/sqlite-open.ts @@ -0,0 +1,21 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { DatabaseSync } from 'node:sqlite'; + +/** + * Open a SQLite database with the store-wide defaults: ensure the parent + * directory exists, then open with WAL journaling + NORMAL synchronous — the + * durability/throughput balance the message / media / reaction stores all + * want. The caller owns schema init and any path munging. + * + * Concentrates the bootstrap each store previously hand-copied verbatim, so a + * PRAGMA / journal-mode / busy-timeout change lands in one place instead of + * three. + */ +export function openSqliteDb(dbPath: string): DatabaseSync { + fs.mkdirSync(path.dirname(dbPath), { recursive: true }); + const db = new DatabaseSync(dbPath); + db.exec('PRAGMA journal_mode = WAL'); + db.exec('PRAGMA synchronous = NORMAL'); + return db; +}