mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +08:00
feat(node-runtime): add BetterSqliteAdapter implementing DatabaseAdapter
Phase 1.2: create @openchatlab/node-runtime package with better-sqlite3 adapter that implements the DatabaseAdapter interface from core. Also add pragma() method to the DatabaseAdapter interface.
This commit is contained in:
1 parent
d1e160fe23
commit
7c99ae2d26
5 files changed
+100
-1
No files matched your search
@@ -56,6 +56,11 @@ export interface DatabaseAdapter {
|
||||
*/
|
||||
transaction<T>(fn: () => T): T
|
||||
|
||||
/**
|
||||
* 执行 PRAGMA 命令
|
||||
*/
|
||||
pragma(pragma: string): unknown
|
||||
|
||||
/**
|
||||
* 关闭数据库连接
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "@openchatlab/node-runtime",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"description": "ChatLab Node.js 运行时适配器:better-sqlite3、文件系统、路径管理",
|
||||
"main": "./src/index.ts",
|
||||
"types": "./src/index.ts",
|
||||
"dependencies": {
|
||||
"@openchatlab/core": "workspace:*"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* DatabaseAdapter 的 better-sqlite3 实现
|
||||
*
|
||||
* 薄包装层:better-sqlite3 的 API 与 DatabaseAdapter 接口天然匹配,
|
||||
* 本适配器主要做类型桥接,几乎零额外逻辑。
|
||||
*/
|
||||
|
||||
import Database from 'better-sqlite3'
|
||||
import type { DatabaseAdapter, PreparedStatement, RunResult } from '@openchatlab/core'
|
||||
|
||||
class BetterSqlitePreparedStatement implements PreparedStatement {
|
||||
constructor(private stmt: Database.Statement) {}
|
||||
|
||||
get(...params: unknown[]): Record<string, unknown> | undefined {
|
||||
return this.stmt.get(...params) as Record<string, unknown> | undefined
|
||||
}
|
||||
|
||||
all(...params: unknown[]): Record<string, unknown>[] {
|
||||
return this.stmt.all(...params) as Record<string, unknown>[]
|
||||
}
|
||||
|
||||
run(...params: unknown[]): RunResult {
|
||||
const result = this.stmt.run(...params)
|
||||
return {
|
||||
changes: result.changes,
|
||||
lastInsertRowid: result.lastInsertRowid,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于 better-sqlite3 的 DatabaseAdapter 实现
|
||||
*/
|
||||
export class BetterSqliteAdapter implements DatabaseAdapter {
|
||||
readonly?: boolean
|
||||
|
||||
constructor(private db: Database.Database) {
|
||||
this.readonly = db.readonly
|
||||
}
|
||||
|
||||
exec(sql: string): void {
|
||||
this.db.exec(sql)
|
||||
}
|
||||
|
||||
prepare(sql: string): PreparedStatement {
|
||||
return new BetterSqlitePreparedStatement(this.db.prepare(sql))
|
||||
}
|
||||
|
||||
transaction<T>(fn: () => T): T {
|
||||
return this.db.transaction(fn)()
|
||||
}
|
||||
|
||||
pragma(pragma: string): unknown {
|
||||
return this.db.pragma(pragma)
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.db.close()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件路径打开数据库并返回适配器
|
||||
*/
|
||||
export function openBetterSqliteDatabase(
|
||||
filePath: string,
|
||||
options?: { readonly?: boolean }
|
||||
): BetterSqliteAdapter {
|
||||
const db = new Database(filePath, {
|
||||
readonly: options?.readonly ?? false,
|
||||
})
|
||||
db.pragma('journal_mode = WAL')
|
||||
return new BetterSqliteAdapter(db)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* @openchatlab/node-runtime
|
||||
*
|
||||
* Node.js 运行时适配器,提供 better-sqlite3 数据库适配器、
|
||||
* 路径管理等平台特定实现。
|
||||
*/
|
||||
|
||||
export { BetterSqliteAdapter, openBetterSqliteDatabase } from './better-sqlite3-adapter'
|
||||
+2
-1
@@ -8,7 +8,8 @@
|
||||
"electron/shared/**/*",
|
||||
"src/types/**/*",
|
||||
"packages/shared-types/**/*",
|
||||
"packages/core/**/*"
|
||||
"packages/core/**/*",
|
||||
"packages/node-runtime/**/*"
|
||||
],
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
|
||||
Reference in new issue
Block a user