@strapi/database 4.6.0-beta.1 → 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.
- 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 +12 -3
- package/lib/entity-manager/regular-relations.js +8 -2
- 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.js +2 -0
- package/lib/migrations/index.js +1 -1
- package/package.json +3 -3
|
@@ -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
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
13
|
-
|
|
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
|
-
|
|
27
|
-
|
|
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
|
-
|
|
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
|
-
|
|
46
|
-
|
|
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
|
-
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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(
|
|
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();
|
|
@@ -296,7 +296,9 @@ const cleanOrderColumnsForInnoDB = async ({
|
|
|
296
296
|
FROM :joinTableName: b
|
|
297
297
|
WHERE a.:orderColumnName: >= b.:orderColumnName: AND a.:joinColumnName: = b.:joinColumnName: AND a.:joinColumnName: = :id
|
|
298
298
|
) AS src_order
|
|
299
|
-
FROM :joinTableName: a
|
|
299
|
+
FROM :joinTableName: a
|
|
300
|
+
WHERE a.:joinColumnName: = :id
|
|
301
|
+
`,
|
|
300
302
|
{
|
|
301
303
|
tempOrderTableName,
|
|
302
304
|
joinTableName: joinTable.name,
|
|
@@ -338,7 +340,9 @@ const cleanOrderColumnsForInnoDB = async ({
|
|
|
338
340
|
.map(() => '?')
|
|
339
341
|
.join(', ')})
|
|
340
342
|
) AS inv_order
|
|
341
|
-
FROM ?? a
|
|
343
|
+
FROM ?? a
|
|
344
|
+
WHERE a.?? IN (${inverseRelIds.map(() => '?').join(', ')})
|
|
345
|
+
`,
|
|
342
346
|
[
|
|
343
347
|
tempInvOrderTableName,
|
|
344
348
|
joinTable.name,
|
|
@@ -349,6 +353,8 @@ const cleanOrderColumnsForInnoDB = async ({
|
|
|
349
353
|
inverseJoinColumn.name,
|
|
350
354
|
...inverseRelIds,
|
|
351
355
|
joinTable.name,
|
|
356
|
+
inverseJoinColumn.name,
|
|
357
|
+
...inverseRelIds,
|
|
352
358
|
]
|
|
353
359
|
)
|
|
354
360
|
.transacting(trx);
|
|
@@ -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.js
CHANGED
package/lib/migrations/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/database",
|
|
3
|
-
"version": "4.6.0-beta.
|
|
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.
|
|
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": "
|
|
46
|
+
"gitHead": "b852090f931cd21868c4016f24db2f9fdfc7a7ab"
|
|
47
47
|
}
|