@strapi/database 5.59.0 → 5.61.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/dist/entity-manager/index.d.ts.map +1 -1
- package/dist/entity-manager/index.js +176 -53
- package/dist/entity-manager/index.js.map +1 -1
- package/dist/entity-manager/index.mjs +179 -56
- package/dist/entity-manager/index.mjs.map +1 -1
- package/dist/entity-manager/morph-relations.d.ts +10 -1
- package/dist/entity-manager/morph-relations.d.ts.map +1 -1
- package/dist/entity-manager/morph-relations.js +36 -0
- package/dist/entity-manager/morph-relations.js.map +1 -1
- package/dist/entity-manager/morph-relations.mjs +36 -1
- package/dist/entity-manager/morph-relations.mjs.map +1 -1
- package/dist/entity-manager/regular-relations.d.ts.map +1 -1
- package/dist/entity-manager/regular-relations.js +6 -1
- package/dist/entity-manager/regular-relations.js.map +1 -1
- package/dist/entity-manager/regular-relations.mjs +8 -3
- package/dist/entity-manager/regular-relations.mjs.map +1 -1
- package/dist/entity-manager/types.d.ts +3 -0
- package/dist/entity-manager/types.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/lifecycles/index.d.ts +1 -0
- package/dist/lifecycles/index.d.ts.map +1 -1
- package/dist/lifecycles/index.js +29 -1
- package/dist/lifecycles/index.js.map +1 -1
- package/dist/lifecycles/index.mjs +29 -1
- package/dist/lifecycles/index.mjs.map +1 -1
- package/dist/lifecycles/subscribers/timestamps.js +2 -2
- package/dist/lifecycles/subscribers/timestamps.js.map +1 -1
- package/dist/lifecycles/subscribers/timestamps.mjs +2 -2
- package/dist/lifecycles/subscribers/timestamps.mjs.map +1 -1
- package/package.json +8 -8
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { pick, has, isEmpty,
|
|
1
|
+
import { pick, has, isEmpty, differenceWith, uniqBy, difference, map, isPlainObject, isString, isNumber, castArray, compact, isObject, uniqWith, isEqual } from 'lodash';
|
|
2
2
|
import { isScalarAttribute, isRelationalAttribute } from '../utils/types.mjs';
|
|
3
3
|
import { createField } from '../fields/index.mjs';
|
|
4
4
|
import createQueryBuilder from '../query/query-builder.mjs';
|
|
5
5
|
import { createRepository } from './entity-repository.mjs';
|
|
6
|
-
import { deleteRelatedMorphOneRelationsAfterMorphToManyUpdate, encodePolymorphicRelation, encodePolymorphicId } from './morph-relations.mjs';
|
|
7
|
-
import {
|
|
6
|
+
import { cleanMorphToManyOrderColumn, deleteRelatedMorphOneRelationsAfterMorphToManyUpdate, encodePolymorphicRelation, encodePolymorphicId } from './morph-relations.mjs';
|
|
7
|
+
import { hasInverseOrderColumn, hasOrderColumn, isAnyToOne, isBidirectional, isOneToAny } from '../metadata/relations.mjs';
|
|
8
8
|
import '../utils/identifiers/index.mjs';
|
|
9
9
|
import { deleteRelations, cleanOrderColumns, deletePreviousOneToAnyRelations, deletePreviousAnyToOneRelations } from './regular-relations.mjs';
|
|
10
10
|
import { relationsOrderer } from './relations-orderer.mjs';
|
|
@@ -764,28 +764,53 @@ const createEntityManager = (db)=>{
|
|
|
764
764
|
order: idx + 1
|
|
765
765
|
}))
|
|
766
766
|
];
|
|
767
|
-
const
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
767
|
+
const computeOrderMap = async ()=>{
|
|
768
|
+
const adjacentRelations = await this.createQueryBuilder(joinTable.name).where({
|
|
769
|
+
$or: [
|
|
770
|
+
{
|
|
771
|
+
[joinColumn.name]: id,
|
|
772
|
+
[idColumn.name]: {
|
|
773
|
+
$in: compact(cleanRelationData.connect?.map((r)=>r.position?.after || r.position?.before))
|
|
774
|
+
}
|
|
775
|
+
},
|
|
776
|
+
{
|
|
777
|
+
[joinColumn.name]: id,
|
|
778
|
+
order: this.createQueryBuilder(joinTable.name).min('order').where({
|
|
779
|
+
[joinColumn.name]: id
|
|
780
|
+
}).where(joinTable.on || {}).transacting(trx).getKnexQuery()
|
|
781
|
+
},
|
|
782
|
+
{
|
|
783
|
+
[joinColumn.name]: id,
|
|
784
|
+
order: this.createQueryBuilder(joinTable.name).max('order').where({
|
|
785
|
+
[joinColumn.name]: id
|
|
786
|
+
}).where(joinTable.on || {}).transacting(trx).getKnexQuery()
|
|
773
787
|
}
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
788
|
+
]
|
|
789
|
+
}).where(joinTable.on || {}).orderBy('order').transacting(trx).execute();
|
|
790
|
+
return relationsOrderer(// Merge id & __type to get a single id key
|
|
791
|
+
adjacentRelations.map(encodePolymorphicRelation({
|
|
792
|
+
idColumn: idColumn.name,
|
|
793
|
+
typeColumn: typeColumn.name
|
|
794
|
+
})), idColumn.name, 'order', cleanRelationData.options?.strict).connect(// Merge id & __type to get a single id key
|
|
795
|
+
(cleanRelationData.connect || []).map(encodePolymorphicRelation({
|
|
796
|
+
idColumn: 'id',
|
|
797
|
+
typeColumn: '__type'
|
|
798
|
+
}))).getOrderMap();
|
|
799
|
+
};
|
|
800
|
+
let orderMap = {};
|
|
801
|
+
if (hasConnect) {
|
|
802
|
+
orderMap = await computeOrderMap();
|
|
803
|
+
// The order column is unsigned on MySQL/MariaDB, so renumber from 1 to make room at the start
|
|
804
|
+
if (Object.values(orderMap).some((order)=>order < 0)) {
|
|
805
|
+
await cleanMorphToManyOrderColumn({
|
|
806
|
+
id,
|
|
807
|
+
joinTable,
|
|
808
|
+
db,
|
|
809
|
+
transaction: trx
|
|
810
|
+
});
|
|
811
|
+
orderMap = await computeOrderMap();
|
|
812
|
+
}
|
|
813
|
+
}
|
|
789
814
|
if (!isEmpty(idsToDelete)) {
|
|
790
815
|
const where = {
|
|
791
816
|
$or: idsToDelete.map((item)=>{
|
|
@@ -818,15 +843,6 @@ const createEntityManager = (db)=>{
|
|
|
818
843
|
...data.__pivot,
|
|
819
844
|
field: attributeName
|
|
820
845
|
}));
|
|
821
|
-
const orderMap = relationsOrderer(// Merge id & __type to get a single id key
|
|
822
|
-
adjacentRelations.map(encodePolymorphicRelation({
|
|
823
|
-
idColumn: idColumn.name,
|
|
824
|
-
typeColumn: typeColumn.name
|
|
825
|
-
})), idColumn.name, 'order', cleanRelationData.options?.strict).connect(// Merge id & __type to get a single id key
|
|
826
|
-
dataset.map(encodePolymorphicRelation({
|
|
827
|
-
idColumn: 'id',
|
|
828
|
-
typeColumn: '__type'
|
|
829
|
-
}))).getOrderMap();
|
|
830
846
|
rows.forEach((row)=>{
|
|
831
847
|
const rowId = row[idColumn.name];
|
|
832
848
|
const rowType = row[typeColumn.name];
|
|
@@ -1063,30 +1079,43 @@ const createEntityManager = (db)=>{
|
|
|
1063
1079
|
...relToAdd.__pivot
|
|
1064
1080
|
}));
|
|
1065
1081
|
if (hasOrderColumn(attribute)) {
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1082
|
+
const computeOrderMap = async ()=>{
|
|
1083
|
+
// Get all adjacent relations and the one with the highest order
|
|
1084
|
+
const adjacentRelations = await this.createQueryBuilder(joinTable.name).where({
|
|
1085
|
+
$or: [
|
|
1086
|
+
{
|
|
1087
|
+
[joinColumn.name]: id,
|
|
1088
|
+
[inverseJoinColumn.name]: {
|
|
1089
|
+
$in: compact(resolvedConnect.map((r)=>r.position?.after || r.position?.before))
|
|
1090
|
+
}
|
|
1091
|
+
},
|
|
1092
|
+
{
|
|
1093
|
+
[joinColumn.name]: id,
|
|
1094
|
+
[orderColumnName]: this.createQueryBuilder(joinTable.name).min(orderColumnName).where({
|
|
1095
|
+
[joinColumn.name]: id
|
|
1096
|
+
}).where(joinTable.on || {}).transacting(trx).getKnexQuery()
|
|
1097
|
+
},
|
|
1098
|
+
{
|
|
1099
|
+
[joinColumn.name]: id,
|
|
1100
|
+
[orderColumnName]: this.createQueryBuilder(joinTable.name).max(orderColumnName).where({
|
|
1101
|
+
[joinColumn.name]: id
|
|
1102
|
+
}).where(joinTable.on || {}).transacting(trx).getKnexQuery()
|
|
1073
1103
|
}
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
}
|
|
1089
|
-
const orderMap = relationsOrderer(adjacentRelations, inverseJoinColumn.name, joinTable.orderColumnName, cleanRelationData.options?.strict).connect(resolvedConnect).getOrderMap();
|
|
1104
|
+
]
|
|
1105
|
+
}).where(joinTable.on || {}).orderBy(orderColumnName).transacting(trx).execute();
|
|
1106
|
+
return relationsOrderer(adjacentRelations, inverseJoinColumn.name, joinTable.orderColumnName, cleanRelationData.options?.strict).connect(resolvedConnect).getOrderMap();
|
|
1107
|
+
};
|
|
1108
|
+
let orderMap = await computeOrderMap();
|
|
1109
|
+
// Order columns are unsigned on MySQL/MariaDB, so renumber from 1 to make room at the start
|
|
1110
|
+
if (Object.values(orderMap).some((order)=>order < 0)) {
|
|
1111
|
+
await cleanOrderColumns({
|
|
1112
|
+
attribute,
|
|
1113
|
+
db,
|
|
1114
|
+
id,
|
|
1115
|
+
transaction: trx
|
|
1116
|
+
});
|
|
1117
|
+
orderMap = await computeOrderMap();
|
|
1118
|
+
}
|
|
1090
1119
|
insert.forEach((row)=>{
|
|
1091
1120
|
row[orderColumnName] = orderMap[row[inverseJoinColumn.name]];
|
|
1092
1121
|
});
|
|
@@ -1296,6 +1325,100 @@ const createEntityManager = (db)=>{
|
|
|
1296
1325
|
}
|
|
1297
1326
|
}
|
|
1298
1327
|
},
|
|
1328
|
+
/**
|
|
1329
|
+
* Deletes the relations of several entities of one model at once, with one statement per
|
|
1330
|
+
* attribute. An attribute whose other side keeps an order column (bidirectional) is handled
|
|
1331
|
+
* entity by entity, so that side gets renumbered.
|
|
1332
|
+
*/ async deleteManyRelations (uid, ids, options) {
|
|
1333
|
+
if (ids.length === 0) {
|
|
1334
|
+
return;
|
|
1335
|
+
}
|
|
1336
|
+
const { attributes } = db.metadata.get(uid);
|
|
1337
|
+
const { transaction: trx } = options ?? {};
|
|
1338
|
+
for (const attributeName of Object.keys(attributes)){
|
|
1339
|
+
const attribute = attributes[attributeName];
|
|
1340
|
+
if (attribute.type !== 'relation') {
|
|
1341
|
+
continue;
|
|
1342
|
+
}
|
|
1343
|
+
if (attribute.relation === 'morphOne' || attribute.relation === 'morphMany') {
|
|
1344
|
+
const { target, morphBy } = attribute;
|
|
1345
|
+
const targetAttribute = db.metadata.get(target).attributes[morphBy];
|
|
1346
|
+
if (targetAttribute.type === 'relation' && targetAttribute.relation === 'morphToOne') {
|
|
1347
|
+
const { idColumn, typeColumn } = targetAttribute.morphColumn;
|
|
1348
|
+
await this.createQueryBuilder(target).update({
|
|
1349
|
+
[idColumn.name]: null,
|
|
1350
|
+
[typeColumn.name]: null
|
|
1351
|
+
}).where({
|
|
1352
|
+
[idColumn.name]: {
|
|
1353
|
+
$in: ids
|
|
1354
|
+
},
|
|
1355
|
+
[typeColumn.name]: uid
|
|
1356
|
+
}).transacting(trx).execute();
|
|
1357
|
+
} else if (targetAttribute.type === 'relation' && targetAttribute.relation === 'morphToMany') {
|
|
1358
|
+
const { joinTable } = targetAttribute;
|
|
1359
|
+
const { idColumn, typeColumn } = joinTable.morphColumn;
|
|
1360
|
+
await this.createQueryBuilder(joinTable.name).delete().where({
|
|
1361
|
+
[idColumn.name]: {
|
|
1362
|
+
$in: ids
|
|
1363
|
+
},
|
|
1364
|
+
[typeColumn.name]: uid,
|
|
1365
|
+
...joinTable.on,
|
|
1366
|
+
field: attributeName
|
|
1367
|
+
}).transacting(trx).execute();
|
|
1368
|
+
}
|
|
1369
|
+
continue;
|
|
1370
|
+
}
|
|
1371
|
+
if (attribute.relation === 'morphToMany') {
|
|
1372
|
+
const { joinTable } = attribute;
|
|
1373
|
+
const { joinColumn } = joinTable;
|
|
1374
|
+
await this.createQueryBuilder(joinTable.name).delete().where({
|
|
1375
|
+
[joinColumn.name]: {
|
|
1376
|
+
$in: ids
|
|
1377
|
+
},
|
|
1378
|
+
...joinTable.on
|
|
1379
|
+
}).transacting(trx).execute();
|
|
1380
|
+
continue;
|
|
1381
|
+
}
|
|
1382
|
+
// do not need to delete links when using foreign keys
|
|
1383
|
+
if (db.dialect.usesForeignKeys()) {
|
|
1384
|
+
continue;
|
|
1385
|
+
}
|
|
1386
|
+
if ('joinColumn' in attribute && attribute.joinColumn && attribute.owner) {
|
|
1387
|
+
continue;
|
|
1388
|
+
}
|
|
1389
|
+
if ('joinColumn' in attribute && attribute.joinColumn && !attribute.owner) {
|
|
1390
|
+
const { target } = attribute;
|
|
1391
|
+
await this.createQueryBuilder(target).where({
|
|
1392
|
+
[attribute.joinColumn.referencedColumn]: {
|
|
1393
|
+
$in: ids
|
|
1394
|
+
}
|
|
1395
|
+
}).update({
|
|
1396
|
+
[attribute.joinColumn.referencedColumn]: null
|
|
1397
|
+
}).transacting(trx).execute();
|
|
1398
|
+
}
|
|
1399
|
+
if ('joinTable' in attribute && attribute.joinTable) {
|
|
1400
|
+
if (hasInverseOrderColumn(attribute)) {
|
|
1401
|
+
for (const id of ids){
|
|
1402
|
+
await deleteRelations({
|
|
1403
|
+
id,
|
|
1404
|
+
attribute,
|
|
1405
|
+
db,
|
|
1406
|
+
relIdsToDelete: 'all',
|
|
1407
|
+
transaction: trx
|
|
1408
|
+
});
|
|
1409
|
+
}
|
|
1410
|
+
continue;
|
|
1411
|
+
}
|
|
1412
|
+
const { joinTable } = attribute;
|
|
1413
|
+
await this.createQueryBuilder(joinTable.name).delete().where({
|
|
1414
|
+
[joinTable.joinColumn.name]: {
|
|
1415
|
+
$in: ids
|
|
1416
|
+
},
|
|
1417
|
+
...joinTable.on
|
|
1418
|
+
}).transacting(trx).execute();
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
},
|
|
1299
1422
|
// TODO: add lifecycle events
|
|
1300
1423
|
async populate (uid, entity, populate) {
|
|
1301
1424
|
const entry = await this.findOne(uid, {
|