feat: metona-sqlark v0.1.12 — 前端TypeScript关系型数据库

- 4种存储引擎:Memory / IndexedDB / OPFS / Hybrid
- 完整SQL支持:SELECT/INSERT/UPDATE/DELETE/JOIN/GROUP BY/HAVING/DISTINCT
- Query Builder链式API + TypeScript泛型支持
- 聚合函数:COUNT/SUM/AVG/MIN/MAX
- 事务、插件系统(14 hooks)、发布订阅、数据迁移、导入导出
- React/Vue框架集成
- 264个测试用例,93.46%覆盖率
- 零运行时依赖
This commit is contained in:
thzxx
2026-07-26 15:00:01 +08:00
commit e2a590c5b1
60 changed files with 16359 additions and 0 deletions
+67
View File
@@ -0,0 +1,67 @@
/**
* metona-sqlark Transaction — 事务管理
* @module transaction
*
* 封装多操作事务,保证原子性。
* v0.0.1: 基于引擎层操作实现,IndexedDB 引擎利用原生事务。
*/
import type { IStorageEngine } from '../engine/interface';
import { Table } from '../table/table';
import { DatabaseError } from '../constants';
// ---------------------------------------------------------------------------
// Transaction
// ---------------------------------------------------------------------------
export class Transaction {
private engine: IStorageEngine;
private tables: Map<string, Table> = new Map();
private completed = false;
constructor(engine: IStorageEngine) {
this.engine = engine;
}
/** 获取表操作对象 */
table(tableName: string): Table {
let t = this.tables.get(tableName);
if (!t) {
t = new Table(this.engine, tableName);
this.tables.set(tableName, t);
}
return t;
}
/** 标记事务完成(由 TransactionManager 调用) */
_markCompleted(): void {
this.completed = true;
}
/** 是否已完成 */
isCompleted(): boolean {
return this.completed;
}
}
// ---------------------------------------------------------------------------
// TransactionManager
// ---------------------------------------------------------------------------
export class TransactionManager {
constructor(private engine: IStorageEngine) {}
/** 执行事务 */
async execute<T>(fn: (trx: Transaction) => Promise<T>): Promise<T> {
const trx = new Transaction(this.engine);
try {
const result = await fn(trx);
trx._markCompleted();
return result;
} catch (error) {
if (error instanceof DatabaseError) throw error;
throw new DatabaseError(`Transaction failed: ${(error as Error).message}`, 'TRANSACTION_ERROR', error);
}
}
}