@strapi/database 4.6.0-beta.1 → 4.6.0
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/__tests__/index.test.js +93 -0
- package/lib/dialects/postgresql/index.js +10 -2
- package/lib/entity-manager/__tests__/relations-orderer.test.js +147 -68
- package/lib/entity-manager/__tests__/sort-connect-array.test.js +79 -0
- package/lib/entity-manager/index.js +15 -6
- package/lib/entity-manager/regular-relations.js +133 -120
- package/lib/entity-manager/relations-orderer.js +120 -21
- package/lib/errors/index.js +2 -0
- package/lib/errors/invalid-relation.js +14 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +47 -5
- package/lib/metadata/relations.js +4 -1
- package/lib/migrations/index.js +1 -1
- package/lib/query/query-builder.js +10 -1
- package/lib/tests/transactions.test.api.js +308 -0
- package/lib/transaction-context.js +17 -0
- package/lib/validations/index.js +20 -0
- package/lib/validations/relations/bidirectional.js +89 -0
- package/lib/validations/relations/index.js +14 -0
- package/package.json +4 -4
|
@@ -198,38 +198,73 @@ 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 (here it's MySQL 5)
|
|
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
|
+
|
|
201
234
|
switch (strapi.db.dialect.client) {
|
|
202
235
|
case 'mysql':
|
|
203
|
-
|
|
236
|
+
// Here it's MariaDB and MySQL 8
|
|
237
|
+
await db
|
|
238
|
+
.getConnection()
|
|
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);
|
|
204
252
|
break;
|
|
253
|
+
/*
|
|
254
|
+
UPDATE
|
|
255
|
+
:joinTable: as a,
|
|
256
|
+
(
|
|
257
|
+
SELECT
|
|
258
|
+
id,
|
|
259
|
+
ROW_NUMBER() OVER ( PARTITION BY :joinColumn: ORDER BY :orderColumn:) AS src_order,
|
|
260
|
+
ROW_NUMBER() OVER ( PARTITION BY :inverseJoinColumn: ORDER BY :inverseOrderColumn:) AS inv_order
|
|
261
|
+
FROM :joinTable:
|
|
262
|
+
WHERE :joinColumn: = :id OR :inverseJoinColumn: IN (:inverseRelIds)
|
|
263
|
+
) AS b
|
|
264
|
+
SET :orderColumn: = b.src_order, :inverseOrderColumn: = b.inv_order
|
|
265
|
+
WHERE b.id = a.id;
|
|
266
|
+
*/
|
|
205
267
|
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
|
-
|
|
233
268
|
const joinTableName = addSchema(joinTable.name);
|
|
234
269
|
|
|
235
270
|
// raw query as knex doesn't allow updating from a subquery
|
|
@@ -249,17 +284,17 @@ const cleanOrderColumns = async ({ id, attribute, db, inverseRelIds, transaction
|
|
|
249
284
|
.transacting(trx);
|
|
250
285
|
|
|
251
286
|
/*
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
287
|
+
UPDATE :joinTable: as a
|
|
288
|
+
SET :orderColumn: = b.src_order, :inverseOrderColumn: = b.inv_order
|
|
289
|
+
FROM (
|
|
290
|
+
SELECT
|
|
291
|
+
id,
|
|
292
|
+
ROW_NUMBER() OVER ( PARTITION BY :joinColumn: ORDER BY :orderColumn:) AS src_order,
|
|
293
|
+
ROW_NUMBER() OVER ( PARTITION BY :inverseJoinColumn: ORDER BY :inverseOrderColumn:) AS inv_order
|
|
294
|
+
FROM :joinTable:
|
|
295
|
+
WHERE :joinColumn: = :id OR :inverseJoinColumn: IN (:inverseRelIds)
|
|
296
|
+
) AS b
|
|
297
|
+
WHERE b.id = a.id;
|
|
263
298
|
*/
|
|
264
299
|
}
|
|
265
300
|
}
|
|
@@ -267,9 +302,9 @@ const cleanOrderColumns = async ({ id, attribute, db, inverseRelIds, transaction
|
|
|
267
302
|
|
|
268
303
|
/*
|
|
269
304
|
* Ensure that orders are following a 1, 2, 3 sequence, without gap.
|
|
270
|
-
* The use of a
|
|
305
|
+
* The use of a session variable instead of a window function makes the query compatible with MySQL 5
|
|
271
306
|
*/
|
|
272
|
-
const
|
|
307
|
+
const cleanOrderColumnsForOldDatabases = async ({
|
|
273
308
|
id,
|
|
274
309
|
attribute,
|
|
275
310
|
db,
|
|
@@ -279,90 +314,68 @@ const cleanOrderColumnsForInnoDB = async ({
|
|
|
279
314
|
const { joinTable } = attribute;
|
|
280
315
|
const { joinColumn, inverseJoinColumn, orderColumnName, inverseOrderColumnName } = joinTable;
|
|
281
316
|
|
|
282
|
-
const
|
|
283
|
-
const randomHex = randomBytes(16).toString('hex');
|
|
317
|
+
const randomSuffix = `${new Date().valueOf()}_${randomBytes(16).toString('hex')}`;
|
|
284
318
|
|
|
285
319
|
if (hasOrderColumn(attribute) && id) {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
// raw query as knex doesn't allow updating from a subquery
|
|
311
|
-
// https://github.com/knex/knex/issues/2504
|
|
312
|
-
await db.connection
|
|
313
|
-
.raw(
|
|
314
|
-
`UPDATE ?? as a, (SELECT * FROM ??) AS b
|
|
315
|
-
SET ?? = b.src_order
|
|
316
|
-
WHERE a.id = b.id`,
|
|
317
|
-
[joinTable.name, tempOrderTableName, orderColumnName]
|
|
318
|
-
)
|
|
319
|
-
.transacting(trx);
|
|
320
|
-
} finally {
|
|
321
|
-
await db.connection.raw(`DROP TABLE IF EXISTS ??`, [tempOrderTableName]).transacting(trx);
|
|
322
|
-
}
|
|
320
|
+
// raw query as knex doesn't allow updating from a subquery
|
|
321
|
+
// https://github.com/knex/knex/issues/2504
|
|
322
|
+
const orderVar = `order_${randomSuffix}`;
|
|
323
|
+
await db.connection.raw(`SET @${orderVar} = 0;`).transacting(trx);
|
|
324
|
+
await db.connection
|
|
325
|
+
.raw(
|
|
326
|
+
`UPDATE :joinTableName: as a, (
|
|
327
|
+
SELECT id, (@${orderVar}:=@${orderVar} + 1) AS src_order
|
|
328
|
+
FROM :joinTableName:
|
|
329
|
+
WHERE :joinColumnName: = :id
|
|
330
|
+
ORDER BY :orderColumnName:
|
|
331
|
+
) AS b
|
|
332
|
+
SET :orderColumnName: = b.src_order
|
|
333
|
+
WHERE a.id = b.id
|
|
334
|
+
AND a.:joinColumnName: = :id`,
|
|
335
|
+
{
|
|
336
|
+
joinTableName: joinTable.name,
|
|
337
|
+
orderColumnName,
|
|
338
|
+
joinColumnName: joinColumn.name,
|
|
339
|
+
id,
|
|
340
|
+
}
|
|
341
|
+
)
|
|
342
|
+
.transacting(trx);
|
|
323
343
|
}
|
|
324
344
|
|
|
325
345
|
if (hasInverseOrderColumn(attribute) && !isEmpty(inverseRelIds)) {
|
|
326
|
-
const
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
WHERE a.id = b.id`,
|
|
360
|
-
[joinTable.name, tempInvOrderTableName, inverseOrderColumnName]
|
|
361
|
-
)
|
|
362
|
-
.transacting(trx);
|
|
363
|
-
} finally {
|
|
364
|
-
await db.connection.raw(`DROP TABLE IF EXISTS ??`, [tempInvOrderTableName]).transacting(trx);
|
|
365
|
-
}
|
|
346
|
+
const orderVar = `order_${randomSuffix}`;
|
|
347
|
+
const columnVar = `col_${randomSuffix}`;
|
|
348
|
+
await db.connection.raw(`SET @${orderVar} = 0;`).transacting(trx);
|
|
349
|
+
await db.connection
|
|
350
|
+
.raw(
|
|
351
|
+
`UPDATE ?? as a, (
|
|
352
|
+
SELECT
|
|
353
|
+
id,
|
|
354
|
+
@${orderVar}:=CASE WHEN @${columnVar} = ?? THEN @${orderVar} + 1 ELSE 1 END AS inv_order,
|
|
355
|
+
@${columnVar}:=?? ??
|
|
356
|
+
FROM ?? a
|
|
357
|
+
WHERE ?? IN(${inverseRelIds.map(() => '?').join(', ')})
|
|
358
|
+
ORDER BY ??, ??
|
|
359
|
+
) AS b
|
|
360
|
+
SET ?? = b.inv_order
|
|
361
|
+
WHERE a.id = b.id
|
|
362
|
+
AND a.?? IN(${inverseRelIds.map(() => '?').join(', ')})`,
|
|
363
|
+
[
|
|
364
|
+
joinTable.name,
|
|
365
|
+
inverseJoinColumn.name,
|
|
366
|
+
inverseJoinColumn.name,
|
|
367
|
+
inverseJoinColumn.name,
|
|
368
|
+
joinTable.name,
|
|
369
|
+
inverseJoinColumn.name,
|
|
370
|
+
...inverseRelIds,
|
|
371
|
+
inverseJoinColumn.name,
|
|
372
|
+
joinColumn.name,
|
|
373
|
+
inverseOrderColumnName,
|
|
374
|
+
inverseJoinColumn.name,
|
|
375
|
+
...inverseRelIds,
|
|
376
|
+
]
|
|
377
|
+
)
|
|
378
|
+
.transacting(trx);
|
|
366
379
|
}
|
|
367
380
|
};
|
|
368
381
|
|
|
@@ -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
|
|
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',
|
|
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 =
|
|
47
|
-
return { idx, relation:
|
|
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
|
-
|
|
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 =
|
|
173
|
+
idx = computedRelations.length;
|
|
76
174
|
}
|
|
77
175
|
|
|
78
176
|
// Insert the relation in the array
|
|
79
|
-
|
|
177
|
+
computedRelations.splice(idx, 0, r);
|
|
80
178
|
};
|
|
81
179
|
|
|
82
180
|
return {
|
|
83
181
|
disconnect(relations) {
|
|
84
|
-
|
|
182
|
+
castArray(relations).forEach((relation) => {
|
|
85
183
|
removeRelation(relation);
|
|
86
184
|
});
|
|
87
185
|
return this;
|
|
88
186
|
},
|
|
89
187
|
connect(relations) {
|
|
90
|
-
|
|
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
|
-
`
|
|
195
|
+
`There was a problem connecting relation with id ${
|
|
196
|
+
relation.id
|
|
197
|
+
} at position ${JSON.stringify(
|
|
99
198
|
relation.position
|
|
100
|
-
)} is
|
|
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
|
|
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 _(
|
|
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 };
|
package/lib/errors/index.js
CHANGED
|
@@ -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
|
@@ -163,6 +163,15 @@ export interface Database {
|
|
|
163
163
|
connection: Knex;
|
|
164
164
|
|
|
165
165
|
query<T extends keyof AllTypes>(uid: T): QueryFromContentType<T>;
|
|
166
|
+
transaction(
|
|
167
|
+
cb?: (params: {
|
|
168
|
+
trx: Knex.Transaction;
|
|
169
|
+
rollback: () => Promise<void>;
|
|
170
|
+
commit: () => Promise<void>;
|
|
171
|
+
}) => Promise<unknown>
|
|
172
|
+
):
|
|
173
|
+
| Promise<unknown>
|
|
174
|
+
| { get: () => Knex.Transaction; rollback: () => Promise<void>; commit: () => Promise<void> };
|
|
166
175
|
}
|
|
167
176
|
export class Database implements Database {
|
|
168
177
|
static transformContentTypes(contentTypes: any[]): ModelConfig[];
|
package/lib/index.js
CHANGED
|
@@ -8,9 +8,11 @@ const { createMigrationsProvider } = require('./migrations');
|
|
|
8
8
|
const { createLifecyclesProvider } = require('./lifecycles');
|
|
9
9
|
const createConnection = require('./connection');
|
|
10
10
|
const errors = require('./errors');
|
|
11
|
+
const transactionCtx = require('./transaction-context');
|
|
11
12
|
|
|
12
13
|
// TODO: move back into strapi
|
|
13
14
|
const { transformContentTypes } = require('./utils/content-types');
|
|
15
|
+
const { validateDatabase } = require('./validations');
|
|
14
16
|
|
|
15
17
|
class Database {
|
|
16
18
|
constructor(config) {
|
|
@@ -20,6 +22,8 @@ class Database {
|
|
|
20
22
|
connection: {},
|
|
21
23
|
settings: {
|
|
22
24
|
forceMigration: true,
|
|
25
|
+
runMigrations: true,
|
|
26
|
+
...config.settings,
|
|
23
27
|
},
|
|
24
28
|
...config,
|
|
25
29
|
};
|
|
@@ -47,6 +51,44 @@ class Database {
|
|
|
47
51
|
return this.entityManager.getRepository(uid);
|
|
48
52
|
}
|
|
49
53
|
|
|
54
|
+
async transaction(cb) {
|
|
55
|
+
const notNestedTransaction = !transactionCtx.get();
|
|
56
|
+
const trx = notNestedTransaction ? await this.connection.transaction() : transactionCtx.get();
|
|
57
|
+
|
|
58
|
+
async function commit() {
|
|
59
|
+
if (notNestedTransaction) {
|
|
60
|
+
await trx.commit();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function rollback() {
|
|
65
|
+
if (notNestedTransaction) {
|
|
66
|
+
await trx.rollback();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (!cb) {
|
|
70
|
+
return {
|
|
71
|
+
commit,
|
|
72
|
+
rollback,
|
|
73
|
+
get() {
|
|
74
|
+
return trx;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return transactionCtx.run(trx, async () => {
|
|
80
|
+
try {
|
|
81
|
+
const callbackParams = { trx, commit, rollback };
|
|
82
|
+
const res = await cb(callbackParams);
|
|
83
|
+
await commit();
|
|
84
|
+
return res;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
await rollback();
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
50
92
|
getConnection(tableName) {
|
|
51
93
|
const schema = this.connection.getSchemaName();
|
|
52
94
|
const connection = tableName ? this.connection(tableName) : this.connection;
|
|
@@ -58,10 +100,6 @@ class Database {
|
|
|
58
100
|
return schema ? trx.schema.withSchema(schema) : trx.schema;
|
|
59
101
|
}
|
|
60
102
|
|
|
61
|
-
transaction() {
|
|
62
|
-
return this.connection.transaction();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
103
|
queryBuilder(uid) {
|
|
66
104
|
return this.entityManager.createQueryBuilder(uid);
|
|
67
105
|
}
|
|
@@ -74,7 +112,11 @@ class Database {
|
|
|
74
112
|
|
|
75
113
|
// TODO: move into strapi
|
|
76
114
|
Database.transformContentTypes = transformContentTypes;
|
|
77
|
-
Database.init = async (config) =>
|
|
115
|
+
Database.init = async (config) => {
|
|
116
|
+
const db = new Database(config);
|
|
117
|
+
await validateDatabase(db);
|
|
118
|
+
return db;
|
|
119
|
+
};
|
|
78
120
|
|
|
79
121
|
module.exports = {
|
|
80
122
|
Database,
|
|
@@ -16,6 +16,8 @@ const isAnyToMany = (attribute) => ['oneToMany', 'manyToMany'].includes(attribut
|
|
|
16
16
|
const isBidirectional = (attribute) => hasInversedBy(attribute) || hasMappedBy(attribute);
|
|
17
17
|
const isOwner = (attribute) => !isBidirectional(attribute) || hasInversedBy(attribute);
|
|
18
18
|
const shouldUseJoinTable = (attribute) => attribute.useJoinTable !== false;
|
|
19
|
+
const getJoinTableName = (tableName, attributeName) =>
|
|
20
|
+
_.snakeCase(`${tableName}_${attributeName}_links`);
|
|
19
21
|
|
|
20
22
|
/**
|
|
21
23
|
* Creates a oneToOne relation metadata
|
|
@@ -397,7 +399,7 @@ const createJoinTable = (metadata, { attributeName, attribute, meta }) => {
|
|
|
397
399
|
throw new Error(`Unknown target ${attribute.target}`);
|
|
398
400
|
}
|
|
399
401
|
|
|
400
|
-
const joinTableName =
|
|
402
|
+
const joinTableName = getJoinTableName(meta.tableName, attributeName);
|
|
401
403
|
|
|
402
404
|
const joinColumnName = _.snakeCase(`${meta.singularName}_id`);
|
|
403
405
|
let inverseJoinColumnName = _.snakeCase(`${targetMeta.singularName}_id`);
|
|
@@ -560,4 +562,5 @@ module.exports = {
|
|
|
560
562
|
isAnyToMany,
|
|
561
563
|
hasOrderColumn,
|
|
562
564
|
hasInverseOrderColumn,
|
|
565
|
+
getJoinTableName,
|
|
563
566
|
};
|
package/lib/migrations/index.js
CHANGED