Files
SnowLuma/packages/onebot/src/sqlite-open.ts
T
DrSmoothl 85ca207279 refactor(onebot): extract openSqliteDb helper for the three stores
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 确认
三处引导等价、路径细节无边角、无回归。
2026-07-01 18:37:42 +08:00

22 lines
822 B
TypeScript

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;
}