-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction.ts
More file actions
67 lines (61 loc) · 1.28 KB
/
transaction.ts
File metadata and controls
67 lines (61 loc) · 1.28 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { DBI } from './dbi';
import { Store } from './store.js';
import { NativeTransaction, type TransactionOptions } from './load-binding.js';
/**
* Provides transaction level operations to a transaction callback.
*/
export class Transaction extends DBI {
#txn: NativeTransaction;
/**
* Create a new transaction.
*
* @param store - The base store interface for this transaction.
* @param options - The options for the transaction.
*/
constructor(store: Store, options?: TransactionOptions) {
const txn = new NativeTransaction(store.db, options);
super(store, txn);
this.#txn = txn;
}
/**
* Abort the transaction.
*/
abort() {
this.#txn.abort();
}
/**
* Commit the transaction.
*/
async commit(): Promise<number> {
try {
return await new Promise<number>((resolve, reject) => {
this.notify('beforecommit');
this.#txn.commit(resolve, reject);
});
} finally {
this.notify('aftercommit', {
next: null,
last: null,
txnId: this.#txn.id
});
}
}
commitSync(): number {
try {
this.notify('beforecommit');
return this.#txn.commitSync();
} finally {
this.notify('aftercommit', {
next: null,
last: null,
txnId: this.#txn.id
});
}
}
/**
* Get the transaction id.
*/
get id() {
return this.#txn.id;
}
}