@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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entity-manager/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entity-manager/index.ts"],"names":[],"mappings":"AA0CA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAGnC,OAAO,EAAE,aAAa,EAAsB,MAAM,SAAS,CAAC;AAE5D,cAAc,SAAS,CAAC;AA2OxB,eAAO,MAAM,mBAAmB,GAAI,IAAI,QAAQ,KAAG,aAmnDlD,CAAC"}
|
|
@@ -766,28 +766,53 @@ const createEntityManager = (db)=>{
|
|
|
766
766
|
order: idx + 1
|
|
767
767
|
}))
|
|
768
768
|
];
|
|
769
|
-
const
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
769
|
+
const computeOrderMap = async ()=>{
|
|
770
|
+
const adjacentRelations = await this.createQueryBuilder(joinTable.name).where({
|
|
771
|
+
$or: [
|
|
772
|
+
{
|
|
773
|
+
[joinColumn.name]: id,
|
|
774
|
+
[idColumn.name]: {
|
|
775
|
+
$in: _.compact(cleanRelationData.connect?.map((r)=>r.position?.after || r.position?.before))
|
|
776
|
+
}
|
|
777
|
+
},
|
|
778
|
+
{
|
|
779
|
+
[joinColumn.name]: id,
|
|
780
|
+
order: this.createQueryBuilder(joinTable.name).min('order').where({
|
|
781
|
+
[joinColumn.name]: id
|
|
782
|
+
}).where(joinTable.on || {}).transacting(trx).getKnexQuery()
|
|
783
|
+
},
|
|
784
|
+
{
|
|
785
|
+
[joinColumn.name]: id,
|
|
786
|
+
order: this.createQueryBuilder(joinTable.name).max('order').where({
|
|
787
|
+
[joinColumn.name]: id
|
|
788
|
+
}).where(joinTable.on || {}).transacting(trx).getKnexQuery()
|
|
775
789
|
}
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
790
|
+
]
|
|
791
|
+
}).where(joinTable.on || {}).orderBy('order').transacting(trx).execute();
|
|
792
|
+
return relationsOrderer.relationsOrderer(// Merge id & __type to get a single id key
|
|
793
|
+
adjacentRelations.map(morphRelations.encodePolymorphicRelation({
|
|
794
|
+
idColumn: idColumn.name,
|
|
795
|
+
typeColumn: typeColumn.name
|
|
796
|
+
})), idColumn.name, 'order', cleanRelationData.options?.strict).connect(// Merge id & __type to get a single id key
|
|
797
|
+
(cleanRelationData.connect || []).map(morphRelations.encodePolymorphicRelation({
|
|
798
|
+
idColumn: 'id',
|
|
799
|
+
typeColumn: '__type'
|
|
800
|
+
}))).getOrderMap();
|
|
801
|
+
};
|
|
802
|
+
let orderMap = {};
|
|
803
|
+
if (hasConnect) {
|
|
804
|
+
orderMap = await computeOrderMap();
|
|
805
|
+
// The order column is unsigned on MySQL/MariaDB, so renumber from 1 to make room at the start
|
|
806
|
+
if (Object.values(orderMap).some((order)=>order < 0)) {
|
|
807
|
+
await morphRelations.cleanMorphToManyOrderColumn({
|
|
808
|
+
id,
|
|
809
|
+
joinTable,
|
|
810
|
+
db,
|
|
811
|
+
transaction: trx
|
|
812
|
+
});
|
|
813
|
+
orderMap = await computeOrderMap();
|
|
814
|
+
}
|
|
815
|
+
}
|
|
791
816
|
if (!_.isEmpty(idsToDelete)) {
|
|
792
817
|
const where = {
|
|
793
818
|
$or: idsToDelete.map((item)=>{
|
|
@@ -820,15 +845,6 @@ const createEntityManager = (db)=>{
|
|
|
820
845
|
...data.__pivot,
|
|
821
846
|
field: attributeName
|
|
822
847
|
}));
|
|
823
|
-
const orderMap = relationsOrderer.relationsOrderer(// Merge id & __type to get a single id key
|
|
824
|
-
adjacentRelations.map(morphRelations.encodePolymorphicRelation({
|
|
825
|
-
idColumn: idColumn.name,
|
|
826
|
-
typeColumn: typeColumn.name
|
|
827
|
-
})), idColumn.name, 'order', cleanRelationData.options?.strict).connect(// Merge id & __type to get a single id key
|
|
828
|
-
dataset.map(morphRelations.encodePolymorphicRelation({
|
|
829
|
-
idColumn: 'id',
|
|
830
|
-
typeColumn: '__type'
|
|
831
|
-
}))).getOrderMap();
|
|
832
848
|
rows.forEach((row)=>{
|
|
833
849
|
const rowId = row[idColumn.name];
|
|
834
850
|
const rowType = row[typeColumn.name];
|
|
@@ -1065,30 +1081,43 @@ const createEntityManager = (db)=>{
|
|
|
1065
1081
|
...relToAdd.__pivot
|
|
1066
1082
|
}));
|
|
1067
1083
|
if (relations.hasOrderColumn(attribute)) {
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1084
|
+
const computeOrderMap = async ()=>{
|
|
1085
|
+
// Get all adjacent relations and the one with the highest order
|
|
1086
|
+
const adjacentRelations = await this.createQueryBuilder(joinTable.name).where({
|
|
1087
|
+
$or: [
|
|
1088
|
+
{
|
|
1089
|
+
[joinColumn.name]: id,
|
|
1090
|
+
[inverseJoinColumn.name]: {
|
|
1091
|
+
$in: _.compact(resolvedConnect.map((r)=>r.position?.after || r.position?.before))
|
|
1092
|
+
}
|
|
1093
|
+
},
|
|
1094
|
+
{
|
|
1095
|
+
[joinColumn.name]: id,
|
|
1096
|
+
[orderColumnName]: this.createQueryBuilder(joinTable.name).min(orderColumnName).where({
|
|
1097
|
+
[joinColumn.name]: id
|
|
1098
|
+
}).where(joinTable.on || {}).transacting(trx).getKnexQuery()
|
|
1099
|
+
},
|
|
1100
|
+
{
|
|
1101
|
+
[joinColumn.name]: id,
|
|
1102
|
+
[orderColumnName]: this.createQueryBuilder(joinTable.name).max(orderColumnName).where({
|
|
1103
|
+
[joinColumn.name]: id
|
|
1104
|
+
}).where(joinTable.on || {}).transacting(trx).getKnexQuery()
|
|
1075
1105
|
}
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
}
|
|
1091
|
-
const orderMap = relationsOrderer.relationsOrderer(adjacentRelations, inverseJoinColumn.name, joinTable.orderColumnName, cleanRelationData.options?.strict).connect(resolvedConnect).getOrderMap();
|
|
1106
|
+
]
|
|
1107
|
+
}).where(joinTable.on || {}).orderBy(orderColumnName).transacting(trx).execute();
|
|
1108
|
+
return relationsOrderer.relationsOrderer(adjacentRelations, inverseJoinColumn.name, joinTable.orderColumnName, cleanRelationData.options?.strict).connect(resolvedConnect).getOrderMap();
|
|
1109
|
+
};
|
|
1110
|
+
let orderMap = await computeOrderMap();
|
|
1111
|
+
// Order columns are unsigned on MySQL/MariaDB, so renumber from 1 to make room at the start
|
|
1112
|
+
if (Object.values(orderMap).some((order)=>order < 0)) {
|
|
1113
|
+
await regularRelations.cleanOrderColumns({
|
|
1114
|
+
attribute,
|
|
1115
|
+
db,
|
|
1116
|
+
id,
|
|
1117
|
+
transaction: trx
|
|
1118
|
+
});
|
|
1119
|
+
orderMap = await computeOrderMap();
|
|
1120
|
+
}
|
|
1092
1121
|
insert.forEach((row)=>{
|
|
1093
1122
|
row[orderColumnName] = orderMap[row[inverseJoinColumn.name]];
|
|
1094
1123
|
});
|
|
@@ -1298,6 +1327,100 @@ const createEntityManager = (db)=>{
|
|
|
1298
1327
|
}
|
|
1299
1328
|
}
|
|
1300
1329
|
},
|
|
1330
|
+
/**
|
|
1331
|
+
* Deletes the relations of several entities of one model at once, with one statement per
|
|
1332
|
+
* attribute. An attribute whose other side keeps an order column (bidirectional) is handled
|
|
1333
|
+
* entity by entity, so that side gets renumbered.
|
|
1334
|
+
*/ async deleteManyRelations (uid, ids, options) {
|
|
1335
|
+
if (ids.length === 0) {
|
|
1336
|
+
return;
|
|
1337
|
+
}
|
|
1338
|
+
const { attributes } = db.metadata.get(uid);
|
|
1339
|
+
const { transaction: trx } = options ?? {};
|
|
1340
|
+
for (const attributeName of Object.keys(attributes)){
|
|
1341
|
+
const attribute = attributes[attributeName];
|
|
1342
|
+
if (attribute.type !== 'relation') {
|
|
1343
|
+
continue;
|
|
1344
|
+
}
|
|
1345
|
+
if (attribute.relation === 'morphOne' || attribute.relation === 'morphMany') {
|
|
1346
|
+
const { target, morphBy } = attribute;
|
|
1347
|
+
const targetAttribute = db.metadata.get(target).attributes[morphBy];
|
|
1348
|
+
if (targetAttribute.type === 'relation' && targetAttribute.relation === 'morphToOne') {
|
|
1349
|
+
const { idColumn, typeColumn } = targetAttribute.morphColumn;
|
|
1350
|
+
await this.createQueryBuilder(target).update({
|
|
1351
|
+
[idColumn.name]: null,
|
|
1352
|
+
[typeColumn.name]: null
|
|
1353
|
+
}).where({
|
|
1354
|
+
[idColumn.name]: {
|
|
1355
|
+
$in: ids
|
|
1356
|
+
},
|
|
1357
|
+
[typeColumn.name]: uid
|
|
1358
|
+
}).transacting(trx).execute();
|
|
1359
|
+
} else if (targetAttribute.type === 'relation' && targetAttribute.relation === 'morphToMany') {
|
|
1360
|
+
const { joinTable } = targetAttribute;
|
|
1361
|
+
const { idColumn, typeColumn } = joinTable.morphColumn;
|
|
1362
|
+
await this.createQueryBuilder(joinTable.name).delete().where({
|
|
1363
|
+
[idColumn.name]: {
|
|
1364
|
+
$in: ids
|
|
1365
|
+
},
|
|
1366
|
+
[typeColumn.name]: uid,
|
|
1367
|
+
...joinTable.on,
|
|
1368
|
+
field: attributeName
|
|
1369
|
+
}).transacting(trx).execute();
|
|
1370
|
+
}
|
|
1371
|
+
continue;
|
|
1372
|
+
}
|
|
1373
|
+
if (attribute.relation === 'morphToMany') {
|
|
1374
|
+
const { joinTable } = attribute;
|
|
1375
|
+
const { joinColumn } = joinTable;
|
|
1376
|
+
await this.createQueryBuilder(joinTable.name).delete().where({
|
|
1377
|
+
[joinColumn.name]: {
|
|
1378
|
+
$in: ids
|
|
1379
|
+
},
|
|
1380
|
+
...joinTable.on
|
|
1381
|
+
}).transacting(trx).execute();
|
|
1382
|
+
continue;
|
|
1383
|
+
}
|
|
1384
|
+
// do not need to delete links when using foreign keys
|
|
1385
|
+
if (db.dialect.usesForeignKeys()) {
|
|
1386
|
+
continue;
|
|
1387
|
+
}
|
|
1388
|
+
if ('joinColumn' in attribute && attribute.joinColumn && attribute.owner) {
|
|
1389
|
+
continue;
|
|
1390
|
+
}
|
|
1391
|
+
if ('joinColumn' in attribute && attribute.joinColumn && !attribute.owner) {
|
|
1392
|
+
const { target } = attribute;
|
|
1393
|
+
await this.createQueryBuilder(target).where({
|
|
1394
|
+
[attribute.joinColumn.referencedColumn]: {
|
|
1395
|
+
$in: ids
|
|
1396
|
+
}
|
|
1397
|
+
}).update({
|
|
1398
|
+
[attribute.joinColumn.referencedColumn]: null
|
|
1399
|
+
}).transacting(trx).execute();
|
|
1400
|
+
}
|
|
1401
|
+
if ('joinTable' in attribute && attribute.joinTable) {
|
|
1402
|
+
if (relations.hasInverseOrderColumn(attribute)) {
|
|
1403
|
+
for (const id of ids){
|
|
1404
|
+
await regularRelations.deleteRelations({
|
|
1405
|
+
id,
|
|
1406
|
+
attribute,
|
|
1407
|
+
db,
|
|
1408
|
+
relIdsToDelete: 'all',
|
|
1409
|
+
transaction: trx
|
|
1410
|
+
});
|
|
1411
|
+
}
|
|
1412
|
+
continue;
|
|
1413
|
+
}
|
|
1414
|
+
const { joinTable } = attribute;
|
|
1415
|
+
await this.createQueryBuilder(joinTable.name).delete().where({
|
|
1416
|
+
[joinTable.joinColumn.name]: {
|
|
1417
|
+
$in: ids
|
|
1418
|
+
},
|
|
1419
|
+
...joinTable.on
|
|
1420
|
+
}).transacting(trx).execute();
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
},
|
|
1301
1424
|
// TODO: add lifecycle events
|
|
1302
1425
|
async populate (uid, entity, populate) {
|
|
1303
1426
|
const entry = await this.findOne(uid, {
|