@strapi/database 4.11.0-alpha.0 → 4.11.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -168,6 +168,8 @@ export interface Database {
168
168
  trx: Knex.Transaction;
169
169
  rollback: () => Promise<void>;
170
170
  commit: () => Promise<void>;
171
+ onCommit: (cb) => void;
172
+ onRollback: (cb) => void;
171
173
  }) => Promise<unknown>
172
174
  ):
173
175
  | Promise<unknown>
package/lib/index.js CHANGED
@@ -61,31 +61,29 @@ class Database {
61
61
 
62
62
  async function commit() {
63
63
  if (notNestedTransaction) {
64
- transactionCtx.clear();
65
- await trx.commit();
64
+ await transactionCtx.commit(trx);
66
65
  }
67
66
  }
68
67
 
69
68
  async function rollback() {
70
69
  if (notNestedTransaction) {
71
- transactionCtx.clear();
72
- await trx.rollback();
70
+ await transactionCtx.rollback(trx);
73
71
  }
74
72
  }
75
73
 
76
74
  if (!cb) {
77
- return {
78
- commit,
79
- rollback,
80
- get() {
81
- return trx;
82
- },
83
- };
75
+ return { commit, rollback, get: () => trx };
84
76
  }
85
77
 
86
78
  return transactionCtx.run(trx, async () => {
87
79
  try {
88
- const callbackParams = { trx, commit, rollback };
80
+ const callbackParams = {
81
+ trx,
82
+ commit,
83
+ rollback,
84
+ onCommit: transactionCtx.onCommit,
85
+ onRollback: transactionCtx.onRollback,
86
+ };
89
87
  const res = await cb(callbackParams);
90
88
  await commit();
91
89
  return res;
@@ -6,7 +6,7 @@ const storage = new AsyncLocalStorage();
6
6
 
7
7
  const transactionCtx = {
8
8
  async run(store, cb) {
9
- return storage.run({ trx: store }, cb);
9
+ return storage.run({ trx: store, commitCallbacks: [], rollbackCallbacks: [] }, cb);
10
10
  },
11
11
 
12
12
  get() {
@@ -14,11 +14,54 @@ const transactionCtx = {
14
14
  return store?.trx;
15
15
  },
16
16
 
17
- clear() {
17
+ async commit(trx) {
18
18
  const store = storage.getStore();
19
+
20
+ // Clear transaction from store
21
+ if (store?.trx) {
22
+ store.trx = null;
23
+ }
24
+
25
+ // Commit transaction
26
+ await trx.commit();
27
+
28
+ if (!store?.commitCallbacks.length) return;
29
+
30
+ // Run callbacks
31
+ store.commitCallbacks.forEach((cb) => cb());
32
+ store.commitCallbacks = [];
33
+ },
34
+
35
+ async rollback(trx) {
36
+ const store = storage.getStore();
37
+
38
+ // Clear transaction from store
19
39
  if (store?.trx) {
20
40
  store.trx = null;
21
41
  }
42
+
43
+ // Rollback transaction
44
+ await trx.rollback();
45
+
46
+ if (!store?.rollbackCallbacks.length) return;
47
+
48
+ // Run callbacks
49
+ store.rollbackCallbacks.forEach((cb) => cb());
50
+ store.rollbackCallbacks = [];
51
+ },
52
+
53
+ onCommit(cb) {
54
+ const store = storage.getStore();
55
+ if (store?.commitCallbacks) {
56
+ store.commitCallbacks.push(cb);
57
+ }
58
+ },
59
+
60
+ onRollback(cb) {
61
+ const store = storage.getStore();
62
+ if (store?.rollbackCallbacks) {
63
+ store.rollbackCallbacks.push(cb);
64
+ }
22
65
  },
23
66
  };
24
67
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/database",
3
- "version": "4.11.0-alpha.0",
3
+ "version": "4.11.0-beta.1",
4
4
  "description": "Strapi's database layer",
5
5
  "homepage": "https://strapi.io",
6
6
  "bugs": {
@@ -33,7 +33,7 @@
33
33
  "lint": "run -T eslint ."
34
34
  },
35
35
  "dependencies": {
36
- "date-fns": "2.29.3",
36
+ "date-fns": "2.30.0",
37
37
  "debug": "4.3.4",
38
38
  "fs-extra": "10.0.0",
39
39
  "knex": "2.4.0",
@@ -45,5 +45,5 @@
45
45
  "node": ">=14.19.1 <=18.x.x",
46
46
  "npm": ">=6.0.0"
47
47
  },
48
- "gitHead": "ca5fb0fcfab1b6784208d2ddf6d51617b8893b35"
48
+ "gitHead": "a5fa3bd7e1c4680dd350580620d383612597d25d"
49
49
  }