@strapi/database 4.9.1 → 4.9.2

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.
@@ -121,6 +121,28 @@ describe('Given I have some relations in the database', () => {
121
121
  ]);
122
122
  });
123
123
  });
124
+
125
+ describe('When you connect a relation before one with null order', () => {
126
+ test('Then it replaces null order values to 1 and properly reorders relations', () => {
127
+ const orderer = relationsOrderer(
128
+ [
129
+ { id: 2, order: null },
130
+ { id: 3, order: null },
131
+ ],
132
+ 'id',
133
+ 'order'
134
+ );
135
+
136
+ orderer.connect([{ id: 4, position: { before: 3 } }, { id: 5 }]);
137
+
138
+ expect(orderer.get()).toMatchObject([
139
+ { id: 2, order: 1 },
140
+ { id: 4, order: 0.5 },
141
+ { id: 3, order: 1 },
142
+ { id: 5, order: 1.5 },
143
+ ]);
144
+ });
145
+ });
124
146
  });
125
147
 
126
148
  describe('Given there are no relations in the database', () => {
@@ -135,7 +135,7 @@ const relationsOrderer = (initArr, idColumn, orderColumn, strict) => {
135
135
  const computedRelations = _.castArray(initArr || []).map((r) => ({
136
136
  init: true,
137
137
  id: r[idColumn],
138
- order: r[orderColumn],
138
+ order: r[orderColumn] || 1,
139
139
  }));
140
140
 
141
141
  const maxOrder = _.maxBy('order', computedRelations)?.order || 0;
package/lib/index.js CHANGED
@@ -57,15 +57,18 @@ class Database {
57
57
 
58
58
  async function commit() {
59
59
  if (notNestedTransaction) {
60
+ transactionCtx.clear();
60
61
  await trx.commit();
61
62
  }
62
63
  }
63
64
 
64
65
  async function rollback() {
65
66
  if (notNestedTransaction) {
67
+ transactionCtx.clear();
66
68
  await trx.rollback();
67
69
  }
68
70
  }
71
+
69
72
  if (!cb) {
70
73
  return {
71
74
  commit,
@@ -6,11 +6,19 @@ const storage = new AsyncLocalStorage();
6
6
 
7
7
  const transactionCtx = {
8
8
  async run(store, cb) {
9
- return storage.run(store, cb);
9
+ return storage.run({ trx: store }, cb);
10
10
  },
11
11
 
12
12
  get() {
13
- return storage.getStore();
13
+ const store = storage.getStore();
14
+ return store?.trx;
15
+ },
16
+
17
+ clear() {
18
+ const store = storage.getStore();
19
+ if (store?.trx) {
20
+ store.trx = null;
21
+ }
14
22
  },
15
23
  };
16
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/database",
3
- "version": "4.9.1",
3
+ "version": "4.9.2",
4
4
  "description": "Strapi's database layer",
5
5
  "homepage": "https://strapi.io",
6
6
  "bugs": {
@@ -45,5 +45,5 @@
45
45
  "node": ">=14.19.1 <=18.x.x",
46
46
  "npm": ">=6.0.0"
47
47
  },
48
- "gitHead": "c8f2f0543b45dda8eadde21a0782b64d156fc786"
48
+ "gitHead": "91e0be2708e4d1e8ec731c75e73e54c0dfacb67d"
49
49
  }