-
Notifications
You must be signed in to change notification settings - Fork 1
/
transcation.ts
36 lines (36 loc) · 1.07 KB
/
transcation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { IDataContext } from './tinyDB';
/**
*
*
* @export
* @param {*} target
* @param {string} propertyName
* @param {TypedPropertyDescriptor<Function>} descriptor
*/
export function Transaction(ctx: IDataContext) {
return (target: any, propertyName: string, descriptor: TypedPropertyDescriptor<Function>) => {
let method = descriptor.value;
descriptor.value = async function () {
if (!this.ctx) {
this.ctx = ctx;
}
this.ctx.BeginTranscation();
let result;
try {
result = await method.apply(this, arguments);
let r = await this.ctx.Commit();
if (r === true) {
this.ctx = null;
}
return result;
} catch (error) {
console.log("RollBack propertyName:", propertyName);
if (this.ctx) {
await this.ctx.RollBack();
this.ctx = null;
}
throw error;
}
}
}
}