feat: v0.1.13 — 事务回滚 + 子查询 + 外键级联 + 连接池
CI / test (18.x) (push) Successful in 9m54s
CI / test (20.x) (push) Successful in 9m52s
CI / test (22.x) (push) Successful in 9m54s
CI / test (24.x) (push) Successful in 9m47s

This commit is contained in:
thzxx
2026-07-26 16:31:15 +08:00
parent 7a53cfd530
commit 0f128da34a
24 changed files with 2264 additions and 166 deletions
+9 -3
View File
@@ -2,8 +2,7 @@
* metona-sqlark Transaction — 事务管理
* @module transaction
*
* 封装多操作事务,保证原子性。
* v0.0.1: 基于引擎层操作实现,IndexedDB 引擎利用原生事务。
* v0.1.13: 支持真正的回滚 — 利用引擎层 begin/commit/rollback 实现原子性。
*/
import type { IStorageEngine } from '../engine/interface';
@@ -51,15 +50,22 @@ export class Transaction {
export class TransactionManager {
constructor(private engine: IStorageEngine) {}
/** 执行事务 */
/** 执行事务 — 支持自动回滚 */
async execute<T>(fn: (trx: Transaction) => Promise<T>): Promise<T> {
const trx = new Transaction(this.engine);
// 开始引擎层事务
await this.engine.beginTransaction();
try {
const result = await fn(trx);
// 成功 → 提交
await this.engine.commitTransaction();
trx._markCompleted();
return result;
} catch (error) {
// 失败 → 回滚
await this.engine.rollbackTransaction();
if (error instanceof DatabaseError) throw error;
throw new DatabaseError(`Transaction failed: ${(error as Error).message}`, 'TRANSACTION_ERROR', error);
}