feat: v0.1.13 — 事务回滚 + 子查询 + 外键级联 + 连接池
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user