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 确认
三处引导等价、路径细节无边角、无回归。
This commit is contained in:
DrSmoothl committed 2026-07-01 18:37:42 +08:00
1 parent d89b095af5
commit 85ca207279
4 files changed
+27 -23

No files matched your search

+2 -8
View File
@@ -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(
+2 -8
View File
@@ -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
+2 -7
View File
@@ -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(
+21
View File
@@ -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;
}