@strapi/database 4.6.0-beta.0 → 4.6.0-beta.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.
@@ -1,85 +1,164 @@
1
1
  'use strict';
2
2
 
3
- const relationsOrderer = require('../relations-orderer');
3
+ const { relationsOrderer } = require('../relations-orderer');
4
4
 
5
- describe('relations orderer', () => {
6
- test('connect at the end', () => {
7
- const orderer = relationsOrderer(
8
- [
5
+ describe('Given I have some relations in the database', () => {
6
+ describe('When I connect a relation at the end', () => {
7
+ test('Then it is placed at the end with the correct order', () => {
8
+ const orderer = relationsOrderer(
9
+ [
10
+ { id: 2, order: 4 },
11
+ { id: 3, order: 10 },
12
+ ],
13
+ 'id',
14
+ 'order'
15
+ );
16
+
17
+ orderer.connect([{ id: 4, position: { end: true } }, { id: 5 }]);
18
+
19
+ expect(orderer.get()).toMatchObject([
9
20
  { id: 2, order: 4 },
10
21
  { id: 3, order: 10 },
11
- ],
12
- 'id',
13
- 'order'
14
- );
15
-
16
- orderer.connect([{ id: 4, position: { end: true } }, { id: 5 }]);
17
-
18
- expect(orderer.get()).toMatchObject([
19
- { id: 2, order: 4 },
20
- { id: 3, order: 10 },
21
- { id: 4, order: 10.5 },
22
- { id: 5, order: 10.5 },
23
- ]);
22
+ { id: 4, order: 10.5 },
23
+ { id: 5, order: 10.5 },
24
+ ]);
25
+ });
24
26
  });
25
27
 
26
- test('connect at the start', () => {
27
- const orderer = relationsOrderer(
28
- [
28
+ describe('When I connect a relation at the start', () => {
29
+ test('Then it is placed at the start with the correct order', () => {
30
+ const orderer = relationsOrderer(
31
+ [
32
+ { id: 2, order: 4 },
33
+ { id: 3, order: 10 },
34
+ ],
35
+ 'id',
36
+ 'order'
37
+ );
38
+
39
+ orderer.connect([{ id: 4, position: { start: true } }]);
40
+
41
+ expect(orderer.get()).toMatchObject([
42
+ { id: 4, order: 0.5 },
29
43
  { id: 2, order: 4 },
30
44
  { id: 3, order: 10 },
31
- ],
32
- 'id',
33
- 'order'
34
- );
35
-
36
- orderer.connect([{ id: 4, position: { start: true } }]);
37
-
38
- expect(orderer.get()).toMatchObject([
39
- { id: 4, order: 0.5 },
40
- { id: 2, order: 4 },
41
- { id: 3, order: 10 },
42
- ]);
45
+ ]);
46
+ });
43
47
  });
44
48
 
45
- test('connect multiple relations', () => {
46
- const orderer = relationsOrderer(
47
- [
49
+ describe('When I connect multiple relations using before', () => {
50
+ test('Then they are correctly ordered', () => {
51
+ const orderer = relationsOrderer(
52
+ [
53
+ { id: 2, order: 4 },
54
+ { id: 3, order: 10 },
55
+ ],
56
+ 'id',
57
+ 'order'
58
+ );
59
+
60
+ orderer.connect([
61
+ { id: 4, position: { before: 3 } },
62
+ { id: 5, position: { before: 4 } },
63
+ ]);
64
+
65
+ expect(orderer.get()).toMatchObject([
48
66
  { id: 2, order: 4 },
67
+ { id: 5, order: 9.5 },
68
+ { id: 4, order: 9.5 },
49
69
  { id: 3, order: 10 },
50
- ],
51
- 'id',
52
- 'order'
53
- );
54
-
55
- orderer.connect([
56
- { id: 4, position: { before: 2 } },
57
- { id: 4, position: { before: 3 } },
58
- { id: 5, position: { before: 4 } },
59
- ]);
60
-
61
- expect(orderer.get()).toMatchObject([
62
- { id: 2, order: 4 },
63
- { id: 5, order: 9.5 },
64
- { id: 4, order: 9.5 },
65
- { id: 3, order: 10 },
66
- ]);
70
+ ]);
71
+ });
67
72
  });
68
73
 
69
- test('connect with no initial relations', () => {
70
- const orderer = relationsOrderer([], 'id', 'order');
71
-
72
- orderer.connect([
73
- { id: 1, position: { start: true } },
74
- { id: 2, position: { start: true } },
75
- { id: 3, position: { after: 1 } },
76
- { id: 1, position: { after: 2 } },
77
- ]);
78
-
79
- expect(orderer.get()).toMatchObject([
80
- { id: 2, order: 0.5 },
81
- { id: 1, order: 0.5 },
82
- { id: 3, order: 0.5 },
83
- ]);
74
+ describe('When you connect multiple disordered relations', () => {
75
+ test('Then they are correctly ordered', () => {
76
+ const orderer = relationsOrderer(
77
+ [
78
+ { id: 1, order: 1 },
79
+ { id: 2, order: 2 },
80
+ { id: 3, order: 3 },
81
+ ],
82
+ 'id',
83
+ 'order'
84
+ );
85
+
86
+ orderer.connect([
87
+ { id: 5, position: { before: 1 } },
88
+ { id: 1, position: { before: 2 } },
89
+ { id: 2, position: { end: true } },
90
+ ]);
91
+
92
+ expect(orderer.get()).toMatchObject([
93
+ { id: 5, order: 0.5 },
94
+ { id: 1, order: 1.5 },
95
+ { id: 3, order: 3 },
96
+ { id: 2, order: 3.5 },
97
+ ]);
98
+ });
99
+ });
100
+
101
+ describe('When you connect a relation before a non-existing relation in non-strict mode', () => {
102
+ test('Then it is placed at the end', () => {
103
+ const orderer = relationsOrderer(
104
+ [
105
+ { id: 1, order: 1 },
106
+ { id: 2, order: 2 },
107
+ { id: 3, order: 3 },
108
+ ],
109
+ 'id',
110
+ 'order',
111
+ false
112
+ );
113
+
114
+ orderer.connect([{ id: 4, position: { before: 5 } }]);
115
+
116
+ expect(orderer.get()).toMatchObject([
117
+ { id: 1, order: 1 },
118
+ { id: 2, order: 2 },
119
+ { id: 3, order: 3 },
120
+ { id: 4, order: 3.5 },
121
+ ]);
122
+ });
123
+ });
124
+ });
125
+
126
+ describe('Given there are no relations in the database', () => {
127
+ describe('When you connect multiple new relations', () => {
128
+ test('Then they are correctly ordered', () => {
129
+ const orderer = relationsOrderer([], 'id', 'order');
130
+
131
+ orderer.connect([
132
+ { id: 1, position: { start: true } },
133
+ { id: 2, position: { start: true } },
134
+ { id: 3, position: { after: 1 } },
135
+ ]);
136
+
137
+ expect(orderer.get()).toMatchObject([
138
+ { id: 2, order: 0.5 },
139
+ { id: 1, order: 0.5 },
140
+ { id: 3, order: 0.5 },
141
+ ]);
142
+ });
143
+ });
144
+
145
+ describe('When you connect multiple disordered relations', () => {
146
+ test('Then they are correctly ordered', () => {
147
+ const orderer = relationsOrderer([], 'id', 'order');
148
+
149
+ orderer.connect([
150
+ { id: 5, position: { before: 1 } },
151
+ { id: 1, position: { before: 2 } },
152
+ { id: 2, position: { end: true } },
153
+ { id: 3, position: { after: 1 } },
154
+ ]);
155
+
156
+ expect(orderer.get()).toMatchObject([
157
+ { id: 5, order: 0.5 },
158
+ { id: 1, order: 0.5 },
159
+ { id: 3, order: 0.5 },
160
+ { id: 2, order: 0.5 },
161
+ ]);
162
+ });
84
163
  });
85
164
  });
@@ -0,0 +1,79 @@
1
+ 'use strict';
2
+
3
+ const { sortConnectArray } = require('../relations-orderer');
4
+
5
+ describe('sortConnectArray', () => {
6
+ test('sorts connect array', () => {
7
+ const sortConnect = sortConnectArray([
8
+ { id: 5, position: { before: 1 } },
9
+ { id: 1, position: { before: 2 } },
10
+ { id: 2, position: { end: true } },
11
+ { id: 3, position: { after: 1 } },
12
+ ]);
13
+
14
+ expect(sortConnect).toMatchObject([
15
+ { id: 2, position: { end: true } },
16
+ { id: 1, position: { before: 2 } },
17
+ { id: 5, position: { before: 1 } },
18
+ { id: 3, position: { after: 1 } },
19
+ ]);
20
+ });
21
+
22
+ test('sorts connect array with initial relations', () => {
23
+ const sortConnect = sortConnectArray(
24
+ [
25
+ { id: 5, position: { before: 1 } },
26
+ { id: 1, position: { before: 2 } },
27
+ { id: 2, position: { end: true } },
28
+ { id: 3, position: { after: 1 } },
29
+ ],
30
+ [{ id: 1 }]
31
+ );
32
+
33
+ expect(sortConnect).toMatchObject([
34
+ { id: 5, position: { before: 1 } },
35
+ { id: 2, position: { end: true } },
36
+ { id: 1, position: { before: 2 } },
37
+ { id: 3, position: { after: 1 } },
38
+ ]);
39
+ });
40
+
41
+ test("error if position doesn't exist", () => {
42
+ const sortConnect = () => sortConnectArray([{ id: 1, position: { after: 2 } }]);
43
+
44
+ expect(sortConnect).toThrowError(
45
+ 'There was a problem connecting relation with id 1 at position {"after":2}. The relation with id 2 needs to be connected first.'
46
+ );
47
+ });
48
+
49
+ test('error with circular references', () => {
50
+ const sortConnect = () =>
51
+ sortConnectArray(
52
+ [
53
+ { id: 2, position: { after: 1 } },
54
+ { id: 3, position: { after: 1 } },
55
+ { id: 1, position: { after: 3 } },
56
+ ],
57
+ []
58
+ );
59
+
60
+ expect(sortConnect).toThrowError(
61
+ 'A circular reference was found in the connect array. One relation is trying to connect before/after another one that is trying to connect before/after it'
62
+ );
63
+ });
64
+
65
+ test('error when connecting same relation twice', () => {
66
+ const sortConnect = () =>
67
+ sortConnectArray(
68
+ [
69
+ { id: 1, position: { after: 2 } },
70
+ { id: 1, position: { after: 3 } },
71
+ ],
72
+ []
73
+ );
74
+
75
+ expect(sortConnect).toThrowError(
76
+ 'The relation with id 1 is already connected. You cannot connect the same relation twice.'
77
+ );
78
+ });
79
+ });
@@ -39,7 +39,7 @@ const {
39
39
  deleteRelations,
40
40
  cleanOrderColumns,
41
41
  } = require('./regular-relations');
42
- const relationsOrderer = require('./relations-orderer');
42
+ const { relationsOrderer } = require('./relations-orderer');
43
43
 
44
44
  const toId = (value) => value.id || value;
45
45
  const toIds = (value) => castArray(value || []).map(toId);
@@ -78,6 +78,9 @@ const toAssocs = (data) => {
78
78
  }
79
79
 
80
80
  return {
81
+ options: {
82
+ strict: data?.options?.strict,
83
+ },
81
84
  connect: toIdArray(data?.connect).map((elm) => ({
82
85
  id: elm.id,
83
86
  position: elm.position ? elm.position : { end: true },
@@ -583,7 +586,12 @@ const createEntityManager = (db) => {
583
586
  });
584
587
  } else if (cleanRelationData.connect && hasOrderColumn(attribute)) {
585
588
  // use position attributes to calculate order
586
- const orderMap = relationsOrderer([], inverseJoinColumn.name, joinTable.orderColumnName)
589
+ const orderMap = relationsOrderer(
590
+ [],
591
+ inverseJoinColumn.name,
592
+ joinTable.orderColumnName,
593
+ true // Always make an strict connect when inserting
594
+ )
587
595
  .connect(relsToAdd)
588
596
  .get()
589
597
  // set the order based on the order of the ids
@@ -873,7 +881,8 @@ const createEntityManager = (db) => {
873
881
  const orderMap = relationsOrderer(
874
882
  adjacentRelations,
875
883
  inverseJoinColumn.name,
876
- joinTable.orderColumnName
884
+ joinTable.orderColumnName,
885
+ cleanRelationData.options.strict
877
886
  )
878
887
  .connect(cleanRelationData.connect)
879
888
  .getOrderMap();
@@ -198,60 +198,42 @@ const cleanOrderColumns = async ({ id, attribute, db, inverseRelIds, transaction
198
198
  return;
199
199
  }
200
200
 
201
- // Handle databases that don't support window function ROW_NUMBER
202
- if (!strapi.db.dialect.supportsWindowFunctions()) {
203
- await cleanOrderColumnsForOldDatabases({ id, attribute, db, inverseRelIds, transaction: trx });
204
- return;
205
- }
206
-
207
- const { joinTable } = attribute;
208
- const { joinColumn, inverseJoinColumn, orderColumnName, inverseOrderColumnName } = joinTable;
209
- const update = [];
210
- const updateBinding = [];
211
- const select = ['??'];
212
- const selectBinding = ['id'];
213
- const where = [];
214
- const whereBinding = [];
215
-
216
- if (hasOrderColumn(attribute) && id) {
217
- update.push('?? = b.src_order');
218
- updateBinding.push(orderColumnName);
219
- select.push('ROW_NUMBER() OVER (PARTITION BY ?? ORDER BY ??) AS src_order');
220
- selectBinding.push(joinColumn.name, orderColumnName);
221
- where.push('?? = ?');
222
- whereBinding.push(joinColumn.name, id);
223
- }
224
-
225
- if (hasInverseOrderColumn(attribute) && !isEmpty(inverseRelIds)) {
226
- update.push('?? = b.inv_order');
227
- updateBinding.push(inverseOrderColumnName);
228
- select.push('ROW_NUMBER() OVER (PARTITION BY ?? ORDER BY ??) AS inv_order');
229
- selectBinding.push(inverseJoinColumn.name, inverseOrderColumnName);
230
- where.push(`?? IN (${inverseRelIds.map(() => '?').join(', ')})`);
231
- whereBinding.push(inverseJoinColumn.name, ...inverseRelIds);
232
- }
233
-
234
- // raw query as knex doesn't allow updating from a subquery
235
- // https://github.com/knex/knex/issues/2504
236
201
  switch (strapi.db.dialect.client) {
237
202
  case 'mysql':
238
- await db.connection
239
- .raw(
240
- `UPDATE
241
- ?? as a,
242
- (
243
- SELECT ${select.join(', ')}
244
- FROM ??
245
- WHERE ${where.join(' OR ')}
246
- ) AS b
247
- SET ${update.join(', ')}
248
- WHERE b.id = a.id`,
249
- [joinTable.name, ...selectBinding, joinTable.name, ...whereBinding, ...updateBinding]
250
- )
251
- .transacting(trx);
203
+ await cleanOrderColumnsForInnoDB({ id, attribute, db, inverseRelIds, transaction: trx });
252
204
  break;
253
205
  default: {
206
+ const { joinTable } = attribute;
207
+ const { joinColumn, inverseJoinColumn, orderColumnName, inverseOrderColumnName } = joinTable;
208
+ const update = [];
209
+ const updateBinding = [];
210
+ const select = ['??'];
211
+ const selectBinding = ['id'];
212
+ const where = [];
213
+ const whereBinding = [];
214
+
215
+ if (hasOrderColumn(attribute) && id) {
216
+ update.push('?? = b.src_order');
217
+ updateBinding.push(orderColumnName);
218
+ select.push('ROW_NUMBER() OVER (PARTITION BY ?? ORDER BY ??) AS src_order');
219
+ selectBinding.push(joinColumn.name, orderColumnName);
220
+ where.push('?? = ?');
221
+ whereBinding.push(joinColumn.name, id);
222
+ }
223
+
224
+ if (hasInverseOrderColumn(attribute) && !isEmpty(inverseRelIds)) {
225
+ update.push('?? = b.inv_order');
226
+ updateBinding.push(inverseOrderColumnName);
227
+ select.push('ROW_NUMBER() OVER (PARTITION BY ?? ORDER BY ??) AS inv_order');
228
+ selectBinding.push(inverseJoinColumn.name, inverseOrderColumnName);
229
+ where.push(`?? IN (${inverseRelIds.map(() => '?').join(', ')})`);
230
+ whereBinding.push(inverseJoinColumn.name, ...inverseRelIds);
231
+ }
232
+
254
233
  const joinTableName = addSchema(joinTable.name);
234
+
235
+ // raw query as knex doesn't allow updating from a subquery
236
+ // https://github.com/knex/knex/issues/2504
255
237
  await db.connection
256
238
  .raw(
257
239
  `UPDATE ?? as a
@@ -265,24 +247,29 @@ const cleanOrderColumns = async ({ id, attribute, db, inverseRelIds, transaction
265
247
  [joinTableName, ...updateBinding, ...selectBinding, joinTableName, ...whereBinding]
266
248
  )
267
249
  .transacting(trx);
250
+
251
+ /*
252
+ `UPDATE :joinTable: as a
253
+ SET :orderColumn: = b.src_order, :inverseOrderColumn: = b.inv_order
254
+ FROM (
255
+ SELECT
256
+ id,
257
+ ROW_NUMBER() OVER ( PARTITION BY :joinColumn: ORDER BY :orderColumn:) AS src_order,
258
+ ROW_NUMBER() OVER ( PARTITION BY :inverseJoinColumn: ORDER BY :inverseOrderColumn:) AS inv_order
259
+ FROM :joinTable:
260
+ WHERE :joinColumn: = :id OR :inverseJoinColumn: IN (:inverseRelIds)
261
+ ) AS b
262
+ WHERE b.id = a.id`,
263
+ */
268
264
  }
269
- /*
270
- `UPDATE :joinTable: as a
271
- SET :orderColumn: = b.src_order, :inverseOrderColumn: = b.inv_order
272
- FROM (
273
- SELECT
274
- id,
275
- ROW_NUMBER() OVER ( PARTITION BY :joinColumn: ORDER BY :orderColumn:) AS src_order,
276
- ROW_NUMBER() OVER ( PARTITION BY :inverseJoinColumn: ORDER BY :inverseOrderColumn:) AS inv_order
277
- FROM :joinTable:
278
- WHERE :joinColumn: = :id OR :inverseJoinColumn: IN (:inverseRelIds)
279
- ) AS b
280
- WHERE b.id = a.id`,
281
- */
282
265
  }
283
266
  };
284
267
 
285
- const cleanOrderColumnsForOldDatabases = async ({
268
+ /*
269
+ * Ensure that orders are following a 1, 2, 3 sequence, without gap.
270
+ * The use of a temporary table instead of a window function makes the query compatible with MySQL 5 and prevents some deadlocks to happen in innoDB databases
271
+ */
272
+ const cleanOrderColumnsForInnoDB = async ({
286
273
  id,
287
274
  attribute,
288
275
  db,
@@ -309,7 +296,9 @@ const cleanOrderColumnsForOldDatabases = async ({
309
296
  FROM :joinTableName: b
310
297
  WHERE a.:orderColumnName: >= b.:orderColumnName: AND a.:joinColumnName: = b.:joinColumnName: AND a.:joinColumnName: = :id
311
298
  ) AS src_order
312
- FROM :joinTableName: a`,
299
+ FROM :joinTableName: a
300
+ WHERE a.:joinColumnName: = :id
301
+ `,
313
302
  {
314
303
  tempOrderTableName,
315
304
  joinTableName: joinTable.name,
@@ -319,6 +308,9 @@ const cleanOrderColumnsForOldDatabases = async ({
319
308
  }
320
309
  )
321
310
  .transacting(trx);
311
+
312
+ // raw query as knex doesn't allow updating from a subquery
313
+ // https://github.com/knex/knex/issues/2504
322
314
  await db.connection
323
315
  .raw(
324
316
  `UPDATE ?? as a, (SELECT * FROM ??) AS b
@@ -338,7 +330,7 @@ const cleanOrderColumnsForOldDatabases = async ({
338
330
  await db.connection
339
331
  .raw(
340
332
  `
341
- CREATE TEMPORARY TABLE ??
333
+ CREATE TABLE ??
342
334
  SELECT
343
335
  id,
344
336
  (
@@ -348,7 +340,9 @@ const cleanOrderColumnsForOldDatabases = async ({
348
340
  .map(() => '?')
349
341
  .join(', ')})
350
342
  ) AS inv_order
351
- FROM ?? a`,
343
+ FROM ?? a
344
+ WHERE a.?? IN (${inverseRelIds.map(() => '?').join(', ')})
345
+ `,
352
346
  [
353
347
  tempInvOrderTableName,
354
348
  joinTable.name,
@@ -359,6 +353,8 @@ const cleanOrderColumnsForOldDatabases = async ({
359
353
  inverseJoinColumn.name,
360
354
  ...inverseRelIds,
361
355
  joinTable.name,
356
+ inverseJoinColumn.name,
357
+ ...inverseRelIds,
362
358
  ]
363
359
  )
364
360
  .transacting(trx);
@@ -371,9 +367,7 @@ const cleanOrderColumnsForOldDatabases = async ({
371
367
  )
372
368
  .transacting(trx);
373
369
  } finally {
374
- await db.connection
375
- .raw(`DROP TEMPORARY TABLE IF EXISTS ??`, [tempInvOrderTableName])
376
- .transacting(trx);
370
+ await db.connection.raw(`DROP TABLE IF EXISTS ??`, [tempInvOrderTableName]).transacting(trx);
377
371
  }
378
372
  }
379
373
  };
@@ -1,6 +1,107 @@
1
1
  'use strict';
2
2
 
3
+ const { castArray } = require('lodash/fp');
3
4
  const _ = require('lodash/fp');
5
+ const { InvalidRelationError } = require('../errors');
6
+ /**
7
+ * When connecting relations, the order you connect them matters.
8
+ *
9
+ * Example, if you connect the following relations:
10
+ * { id: 5, position: { before: 1 } }
11
+ * { id: 1, position: { before: 2 } }
12
+ * { id: 2, position: { end: true } }
13
+ *
14
+ * Going through the connect array, id 5 has to be connected before id 1,
15
+ * so the order of id5 = id1 - 1. But the order value of id 1 is unknown.
16
+ * The only way to know the order of id 1 is to connect it first.
17
+ *
18
+ * This function makes sure the relations are connected in the right order:
19
+ * { id: 2, position: { end: true } }
20
+ * { id: 1, position: { before: 2 } }
21
+ * { id: 5, position: { before: 1 } }
22
+ *
23
+ */
24
+ const sortConnectArray = (connectArr, initialArr = [], strictSort = true) => {
25
+ const sortedConnect = [];
26
+ // Boolean to know if we have to recalculate the order of the relations
27
+ let needsSorting = false;
28
+ // Map to validate if relation is already in sortedConnect or DB.
29
+ const relationInInitialArray = initialArr.reduce((acc, rel) => ({ ...acc, [rel.id]: true }), {});
30
+ // Map to store the first index where a relation id is connected
31
+ const mappedRelations = connectArr.reduce((mapper, relation) => {
32
+ const adjacentRelId = relation.position?.before || relation.position?.after;
33
+
34
+ if (!relationInInitialArray[adjacentRelId] && !mapper[adjacentRelId]) {
35
+ needsSorting = true;
36
+ }
37
+
38
+ // If the relation is already in the array, throw an error
39
+ if (mapper[relation.id]) {
40
+ throw new InvalidRelationError(
41
+ `The relation with id ${relation.id} is already connected. ` +
42
+ 'You cannot connect the same relation twice.'
43
+ );
44
+ }
45
+
46
+ return {
47
+ [relation.id]: { ...relation, computed: false },
48
+ ...mapper,
49
+ };
50
+ }, {});
51
+
52
+ // If we don't need to sort the connect array, we can return it as is
53
+ if (!needsSorting) return connectArr;
54
+
55
+ // Recursively compute in which order the relation should be connected
56
+ const computeRelation = (relation, relationsSeenInBranch) => {
57
+ const adjacentRelId = relation.position?.before || relation.position?.after;
58
+ const adjacentRelation = mappedRelations[adjacentRelId];
59
+
60
+ // If the relation has already been seen in the current branch,
61
+ // it means there is a circular reference
62
+ if (relationsSeenInBranch[adjacentRelId]) {
63
+ throw new InvalidRelationError(
64
+ 'A circular reference was found in the connect array. ' +
65
+ 'One relation is trying to connect before/after another one that is trying to connect before/after it'
66
+ );
67
+ }
68
+
69
+ // This relation has already been computed
70
+ if (mappedRelations[relation.id]?.computed) return;
71
+
72
+ mappedRelations[relation.id].computed = true;
73
+
74
+ // Relation does not have a before or after attribute or is in the initial array
75
+ if (!adjacentRelId || relationInInitialArray[adjacentRelId]) {
76
+ sortedConnect.push(relation);
77
+ return;
78
+ }
79
+
80
+ // Look if id is referenced elsewhere in the array
81
+ if (mappedRelations[adjacentRelId]) {
82
+ computeRelation(adjacentRelation, { ...relationsSeenInBranch, [relation.id]: true });
83
+ sortedConnect.push(relation);
84
+ } else if (strictSort) {
85
+ // If we reach this point, it means that the adjacent relation is not in the connect array
86
+ // and it is not in the database.
87
+ throw new InvalidRelationError(
88
+ `There was a problem connecting relation with id ${
89
+ relation.id
90
+ } at position ${JSON.stringify(
91
+ relation.position
92
+ )}. The relation with id ${adjacentRelId} needs to be connected first.`
93
+ );
94
+ } else {
95
+ // We are in non-strict mode so we can push the relation.
96
+ sortedConnect.push({ id: relation.id, position: { end: true } });
97
+ }
98
+ };
99
+
100
+ // Iterate over connectArr and populate sortedConnect
101
+ connectArr.forEach((relation) => computeRelation(relation, {}));
102
+
103
+ return sortedConnect;
104
+ };
4
105
 
5
106
  /**
6
107
  * Responsible for calculating the relations order when connecting them.
@@ -23,34 +124,31 @@ const _ = require('lodash/fp');
23
124
  * - The final step would be to recalculate fractional order values.
24
125
  * [ { id: 2, order: 4 }, { id: 5, order: 3.33 }, { id: 4, order: 3.66 }, { id: 3, order: 10 } ]
25
126
  *
26
- * Constraints:
27
- * - Expects you will never connect a relation before / after one that does not exist
28
- * - Expect initArr to have all relations referenced in the positional attributes
29
- *
30
127
  * @param {Array<*>} initArr - array of relations to initialize the class with
31
128
  * @param {string} idColumn - the column name of the id
32
129
  * @param {string} orderColumn - the column name of the order
130
+ * @param {boolean} strict - if true, will throw an error if a relation is connected adjacent to
131
+ * another one that does not exist
33
132
  * @return {*}
34
133
  */
35
- const relationsOrderer = (initArr, idColumn, orderColumn) => {
36
- const arr = _.castArray(initArr || []).map((r) => ({
134
+ const relationsOrderer = (initArr, idColumn, orderColumn, strict) => {
135
+ const computedRelations = _.castArray(initArr || []).map((r) => ({
37
136
  init: true,
38
137
  id: r[idColumn],
39
138
  order: r[orderColumn],
40
139
  }));
41
140
 
42
- const maxOrder = _.maxBy('order', arr)?.order || 0;
141
+ const maxOrder = _.maxBy('order', computedRelations)?.order || 0;
43
142
 
44
- // TODO: Improve performance by using a map
45
143
  const findRelation = (id) => {
46
- const idx = arr.findIndex((r) => r.id === id);
47
- return { idx, relation: arr[idx] };
144
+ const idx = computedRelations.findIndex((r) => r.id === id);
145
+ return { idx, relation: computedRelations[idx] };
48
146
  };
49
147
 
50
148
  const removeRelation = (r) => {
51
149
  const { idx } = findRelation(r.id);
52
150
  if (idx >= 0) {
53
- arr.splice(idx, 1);
151
+ computedRelations.splice(idx, 1);
54
152
  }
55
153
  };
56
154
 
@@ -72,45 +170,46 @@ const relationsOrderer = (initArr, idColumn, orderColumn) => {
72
170
  idx = 0;
73
171
  } else {
74
172
  r.order = maxOrder + 0.5;
75
- idx = arr.length;
173
+ idx = computedRelations.length;
76
174
  }
77
175
 
78
176
  // Insert the relation in the array
79
- arr.splice(idx, 0, r);
177
+ computedRelations.splice(idx, 0, r);
80
178
  };
81
179
 
82
180
  return {
83
181
  disconnect(relations) {
84
- _.castArray(relations).forEach((relation) => {
182
+ castArray(relations).forEach((relation) => {
85
183
  removeRelation(relation);
86
184
  });
87
185
  return this;
88
186
  },
89
187
  connect(relations) {
90
- _.castArray(relations).forEach((relation) => {
188
+ sortConnectArray(castArray(relations), computedRelations, strict).forEach((relation) => {
91
189
  this.disconnect(relation);
92
190
 
93
191
  try {
94
192
  insertRelation(relation);
95
193
  } catch (err) {
96
- strapi.log.error(err);
97
194
  throw new Error(
98
- `Could not connect ${relation.id}, position ${JSON.stringify(
195
+ `There was a problem connecting relation with id ${
196
+ relation.id
197
+ } at position ${JSON.stringify(
99
198
  relation.position
100
- )} is invalid`
199
+ )}. The list of connect relations is not valid`
101
200
  );
102
201
  }
103
202
  });
104
203
  return this;
105
204
  },
106
205
  get() {
107
- return arr;
206
+ return computedRelations;
108
207
  },
109
208
  /**
110
209
  * Get a map between the relation id and its order
111
210
  */
112
211
  getOrderMap() {
113
- return _(arr)
212
+ return _(computedRelations)
114
213
  .groupBy('order')
115
214
  .reduce((acc, relations) => {
116
215
  if (relations[0]?.init) return acc;
@@ -123,4 +222,4 @@ const relationsOrderer = (initArr, idColumn, orderColumn) => {
123
222
  };
124
223
  };
125
224
 
126
- module.exports = relationsOrderer;
225
+ module.exports = { relationsOrderer, sortConnectArray };
@@ -5,6 +5,7 @@ const NotNullError = require('./not-null');
5
5
  const InvalidTimeError = require('./invalid-time');
6
6
  const InvalidDateError = require('./invalid-date');
7
7
  const InvalidDateTimeError = require('./invalid-datetime');
8
+ const InvalidRelationError = require('./invalid-relation');
8
9
 
9
10
  module.exports = {
10
11
  DatabaseError,
@@ -12,4 +13,5 @@ module.exports = {
12
13
  InvalidTimeError,
13
14
  InvalidDateError,
14
15
  InvalidDateTimeError,
16
+ InvalidRelationError,
15
17
  };
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ const DatabaseError = require('./database');
4
+
5
+ class InvalidRelationError extends DatabaseError {
6
+ constructor(message) {
7
+ super();
8
+ this.name = 'InvalidRelationFormat';
9
+ this.message = message || 'Invalid relation format';
10
+ this.details = {};
11
+ }
12
+ }
13
+
14
+ module.exports = InvalidRelationError;
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Knex } from 'knex';
1
2
  import { LifecycleProvider } from './lifecycles';
2
3
  import { MigrationProvider } from './migrations';
3
4
  import { SchemaProvider } from './schema';
@@ -31,8 +32,7 @@ type AttributeOperators<T, K extends keyof T> = {
31
32
 
32
33
  export type WhereParams<T> = {
33
34
  [K in keyof T]?: T[K] | T[K][] | AttributeOperators<T, K>;
34
- } &
35
- LogicalOperators<T>;
35
+ } & LogicalOperators<T>;
36
36
 
37
37
  type Sortables<T> = {
38
38
  // check sortable
@@ -158,6 +158,9 @@ export interface Database {
158
158
  lifecycles: LifecycleProvider;
159
159
  migrations: MigrationProvider;
160
160
  entityManager: EntityManager;
161
+ queryBuilder: any;
162
+ metadata: any;
163
+ connection: Knex;
161
164
 
162
165
  query<T extends keyof AllTypes>(uid: T): QueryFromContentType<T>;
163
166
  }
package/lib/index.js CHANGED
@@ -20,6 +20,8 @@ class Database {
20
20
  connection: {},
21
21
  settings: {
22
22
  forceMigration: true,
23
+ runMigrations: true,
24
+ ...config.settings,
23
25
  },
24
26
  ...config,
25
27
  };
@@ -61,7 +61,7 @@ const createMigrationsProvider = (db) => {
61
61
  async shouldRun() {
62
62
  const pending = await migrations.pending();
63
63
 
64
- return pending.length > 0;
64
+ return pending.length > 0 && db.config.settings.runMigrations;
65
65
  },
66
66
  async up() {
67
67
  await migrations.up();
@@ -7,4 +7,5 @@ module.exports = {
7
7
  ...require('./populate'),
8
8
  ...require('./where'),
9
9
  ...require('./transform'),
10
+ ...require('./streams'),
10
11
  };
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ module.exports = {
4
+ ReadableQuery: require('./readable'),
5
+ };
@@ -0,0 +1,174 @@
1
+ 'use strict';
2
+
3
+ const { Readable } = require('stream');
4
+ const { isFinite } = require('lodash/fp');
5
+
6
+ const { applyPopulate } = require('../populate');
7
+ const { fromRow } = require('../transform');
8
+
9
+ const knexQueryDone = Symbol('knexQueryDone');
10
+ const knexPerformingQuery = Symbol('knexPerformingQuery');
11
+
12
+ class ReadableStrapiQuery extends Readable {
13
+ /**
14
+ * @param {object} options
15
+ * @param {ReturnType<typeof import('../../query-builder')>} options.qb The strapi query builder instance
16
+ * @param {string} options.uid The model uid
17
+ * @param {import('../../../index').Database} options.db The Database instance
18
+ * @param {boolean} [options.mapResults] The maximum number of entities to fetch per query
19
+ * @param {number} [options.batchSize] The maximum number of entities to fetch per query
20
+ */
21
+ constructor({ qb, db, uid, mapResults = true, batchSize = 500 }) {
22
+ super({ objectMode: true, highWaterMark: batchSize });
23
+
24
+ // Extract offset & limit from the query-builder's state
25
+ const { offset, limit } = qb.state;
26
+
27
+ // Original offset value
28
+ this._offset = isFinite(offset) ? offset : 0;
29
+
30
+ // Max amount of entities to fetch, force null as undefined value
31
+ this._limit = isFinite(limit) ? limit : null;
32
+
33
+ // Total amount of entities fetched
34
+ this._fetched = 0;
35
+
36
+ /**
37
+ * Original query
38
+ * @type {import('knex').Knex}
39
+ */
40
+ this._query = qb.getKnexQuery();
41
+
42
+ // Query Builder instance
43
+ this._qb = qb;
44
+
45
+ // Database related properties
46
+ this._db = db;
47
+ this._uid = uid;
48
+ this._meta = db.metadata.get(uid);
49
+
50
+ // Stream params
51
+ this._batchSize = batchSize;
52
+ this._mapResults = mapResults;
53
+
54
+ // States
55
+ this[knexPerformingQuery] = false;
56
+ }
57
+
58
+ _destroy(err, cb) {
59
+ // If the stream is destroyed while a query is being made, then wait for a
60
+ // kQueryDone event to be emitted before actually destroying the stream
61
+ if (this[knexPerformingQuery]) {
62
+ this.once(knexQueryDone, (er) => cb(err || er));
63
+ } else {
64
+ cb(err);
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Custom ._read() implementation
70
+ *
71
+ * NOTE: Here "size" means the number of entities to be read from the database.
72
+ * Not the actual byte size, as it would means that we need to return partial entities.
73
+ *
74
+ * @param {number} size
75
+ */
76
+ async _read(size) {
77
+ const query = this._query;
78
+
79
+ // Remove the original offset & limit properties from the query
80
+ // Theoretically, they would be replaced by calling them again, but this is just to be sure
81
+ query.clear('limit').clear('offset');
82
+
83
+ // Define the maximum read size based on the limit and the requested size
84
+ // NOTE: size is equal to _batchSize by default. Since we want to allow customizing it on
85
+ // the fly, we need to use its value instead of batchSize when computing the maxReadSize value
86
+ const maxReadSize =
87
+ // if no limit is defined in the query, use the given size,
88
+ // otherwise, use the smallest value between the two
89
+ this._limit === null ? size : Math.min(size, this._limit);
90
+
91
+ // Compute the limit for the next query
92
+ const limit =
93
+ // If a limit is defined
94
+ this._limit !== null &&
95
+ // And reading `maxReadSize` would fetch too many entities (> _limit)
96
+ this._fetched + maxReadSize > this._limit
97
+ ? // Then adjust the limit so that it only get the remaining entities
98
+ this._limit - this._fetched
99
+ : // Else, use the max read size
100
+ maxReadSize;
101
+
102
+ // If we don't have anything left to read (_limit === _fetched),
103
+ // don't bother making the query and end the stream by pushing null
104
+ if (limit <= 0) {
105
+ this.push(null);
106
+ return;
107
+ }
108
+
109
+ // Compute the offset (base offset + number of entities already fetched)
110
+ const offset = this._offset + this._fetched;
111
+
112
+ // Update the query with the new values (offset + limit)
113
+ query.offset(offset).limit(limit);
114
+
115
+ // Lock the ._destroy()
116
+ this[knexPerformingQuery] = true;
117
+
118
+ let results;
119
+ let count;
120
+ let err;
121
+
122
+ try {
123
+ // Execute the query and store the results & count
124
+ results = await query;
125
+
126
+ const { populate } = this._qb.state;
127
+
128
+ // Apply populate if needed
129
+ if (populate) {
130
+ await applyPopulate(results, populate, { qb: this._qb, uid: this._uid, db: this._db });
131
+ }
132
+
133
+ // Map results if asked to
134
+ if (this._mapResults) {
135
+ results = fromRow(this._meta, results);
136
+ }
137
+
138
+ count = results.length;
139
+ } catch (e) {
140
+ err = e;
141
+ }
142
+
143
+ // Unlock the ._destroy()
144
+ this[knexPerformingQuery] = false;
145
+
146
+ // Tell ._destroy() that it's now safe to close the db connection
147
+ if (this.destroyed) {
148
+ this.emit(knexQueryDone);
149
+ return;
150
+ }
151
+
152
+ // If there is an error, destroy with the given error
153
+ if (err) {
154
+ this.destroy(err);
155
+ return;
156
+ }
157
+
158
+ // Update the amount of fetched entities
159
+ this._fetched += count;
160
+
161
+ // While there is at least one value to unpack
162
+ for (const result of results) {
163
+ this.push(result);
164
+ }
165
+
166
+ // If the amount of fetched entities is smaller than the
167
+ // maximum read size, Then push null to close the stream
168
+ if (this._fetched === this._limit || count < this._batchSize) {
169
+ this.push(null);
170
+ }
171
+ }
172
+ }
173
+
174
+ module.exports = ReadableStrapiQuery;
@@ -2,6 +2,7 @@
2
2
 
3
3
  const _ = require('lodash/fp');
4
4
 
5
+ const { DatabaseError } = require('../errors');
5
6
  const helpers = require('./helpers');
6
7
 
7
8
  const createQueryBuilder = (uid, db, initialState = {}) => {
@@ -488,6 +489,16 @@ const createQueryBuilder = (uid, db, initialState = {}) => {
488
489
  db.dialect.transformErrors(error);
489
490
  }
490
491
  },
492
+
493
+ stream({ mapResults = true } = {}) {
494
+ if (state.type === 'select') {
495
+ return new helpers.ReadableQuery({ qb: this, db, uid, mapResults });
496
+ }
497
+
498
+ throw new DatabaseError(
499
+ `query-builder.stream() has been called with an unsupported query type: "${state.type}"`
500
+ );
501
+ },
491
502
  };
492
503
  };
493
504
 
@@ -46,4 +46,4 @@ export interface SchemaProvider {
46
46
  drop(): Promise<void>;
47
47
  }
48
48
 
49
- export default function(db: Database): SchemaProvider;
49
+ export default function (db: Database): SchemaProvider;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/database",
3
- "version": "4.6.0-beta.0",
3
+ "version": "4.6.0-beta.2",
4
4
  "description": "Strapi's database layer",
5
5
  "homepage": "https://strapi.io",
6
6
  "bugs": {
@@ -31,7 +31,7 @@
31
31
  "test:unit": "jest --verbose"
32
32
  },
33
33
  "dependencies": {
34
- "date-fns": "2.29.2",
34
+ "date-fns": "2.29.3",
35
35
  "debug": "4.3.4",
36
36
  "fs-extra": "10.0.0",
37
37
  "knex": "1.0.7",
@@ -43,5 +43,5 @@
43
43
  "node": ">=14.19.1 <=18.x.x",
44
44
  "npm": ">=6.0.0"
45
45
  },
46
- "gitHead": "c0c3365ad801d088a6ab6c4eb95a014078429747"
46
+ "gitHead": "b852090f931cd21868c4016f24db2f9fdfc7a7ab"
47
47
  }