From 85ca20727993949a61625d557c9c7a9667d96b8d Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Wed, 1 Jul 2026 18:37:42 +0800 Subject: [PATCH] refactor(onebot): extract openSqliteDb helper for the three stores MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit message/media/reaction store 构造器各自逐字复制同一段引导(mkdir 父目录 + 打开 DatabaseSync + PRAGMA journal_mode=WAL + PRAGMA synchronous=NORMAL),改一处 PRAGMA 要改三处。抽 openSqliteDb(dbPath) 收敛,schema init 与路径改写留在各调用方。 - message-store 的 .json→.db 改写保留(改写后同目录,mkdir 目标不变,等价)。 - 三文件删掉不再使用的 fs/path import。 行为严格不变:onebot 516 全绿、typecheck 干净;对抗性 Agent 逐字对比 HEAD 确认 三处引导等价、路径细节无边角、无回归。 --- packages/onebot/src/media-store.ts | 10 ++-------- packages/onebot/src/message-store.ts | 10 ++-------- packages/onebot/src/reaction-store.ts | 9 ++------- packages/onebot/src/sqlite-open.ts | 21 +++++++++++++++++++++ 4 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 packages/onebot/src/sqlite-open.ts 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; +}