@strapi/database 5.59.0 → 5.60.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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entity-manager/index.ts"],"names":[],"mappings":"AAyCA,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,aA6+ClD,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entity-manager/index.ts"],"names":[],"mappings":"AAyCA,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,aAwlDlD,CAAC"}
@@ -1298,6 +1298,100 @@ const createEntityManager = (db)=>{
1298
1298
  }
1299
1299
  }
1300
1300
  },
1301
+ /**
1302
+ * Deletes the relations of several entities of one model at once, with one statement per
1303
+ * attribute. An attribute whose other side keeps an order column (bidirectional) is handled
1304
+ * entity by entity, so that side gets renumbered.
1305
+ */ async deleteManyRelations (uid, ids, options) {
1306
+ if (ids.length === 0) {
1307
+ return;
1308
+ }
1309
+ const { attributes } = db.metadata.get(uid);
1310
+ const { transaction: trx } = options ?? {};
1311
+ for (const attributeName of Object.keys(attributes)){
1312
+ const attribute = attributes[attributeName];
1313
+ if (attribute.type !== 'relation') {
1314
+ continue;
1315
+ }
1316
+ if (attribute.relation === 'morphOne' || attribute.relation === 'morphMany') {
1317
+ const { target, morphBy } = attribute;
1318
+ const targetAttribute = db.metadata.get(target).attributes[morphBy];
1319
+ if (targetAttribute.type === 'relation' && targetAttribute.relation === 'morphToOne') {
1320
+ const { idColumn, typeColumn } = targetAttribute.morphColumn;
1321
+ await this.createQueryBuilder(target).update({
1322
+ [idColumn.name]: null,
1323
+ [typeColumn.name]: null
1324
+ }).where({
1325
+ [idColumn.name]: {
1326
+ $in: ids
1327
+ },
1328
+ [typeColumn.name]: uid
1329
+ }).transacting(trx).execute();
1330
+ } else if (targetAttribute.type === 'relation' && targetAttribute.relation === 'morphToMany') {
1331
+ const { joinTable } = targetAttribute;
1332
+ const { idColumn, typeColumn } = joinTable.morphColumn;
1333
+ await this.createQueryBuilder(joinTable.name).delete().where({
1334
+ [idColumn.name]: {
1335
+ $in: ids
1336
+ },
1337
+ [typeColumn.name]: uid,
1338
+ ...joinTable.on,
1339
+ field: attributeName
1340
+ }).transacting(trx).execute();
1341
+ }
1342
+ continue;
1343
+ }
1344
+ if (attribute.relation === 'morphToMany') {
1345
+ const { joinTable } = attribute;
1346
+ const { joinColumn } = joinTable;
1347
+ await this.createQueryBuilder(joinTable.name).delete().where({
1348
+ [joinColumn.name]: {
1349
+ $in: ids
1350
+ },
1351
+ ...joinTable.on
1352
+ }).transacting(trx).execute();
1353
+ continue;
1354
+ }
1355
+ // do not need to delete links when using foreign keys
1356
+ if (db.dialect.usesForeignKeys()) {
1357
+ continue;
1358
+ }
1359
+ if ('joinColumn' in attribute && attribute.joinColumn && attribute.owner) {
1360
+ continue;
1361
+ }
1362
+ if ('joinColumn' in attribute && attribute.joinColumn && !attribute.owner) {
1363
+ const { target } = attribute;
1364
+ await this.createQueryBuilder(target).where({
1365
+ [attribute.joinColumn.referencedColumn]: {
1366
+ $in: ids
1367
+ }
1368
+ }).update({
1369
+ [attribute.joinColumn.referencedColumn]: null
1370
+ }).transacting(trx).execute();
1371
+ }
1372
+ if ('joinTable' in attribute && attribute.joinTable) {
1373
+ if (relations.hasInverseOrderColumn(attribute)) {
1374
+ for (const id of ids){
1375
+ await regularRelations.deleteRelations({
1376
+ id,
1377
+ attribute,
1378
+ db,
1379
+ relIdsToDelete: 'all',
1380
+ transaction: trx
1381
+ });
1382
+ }
1383
+ continue;
1384
+ }
1385
+ const { joinTable } = attribute;
1386
+ await this.createQueryBuilder(joinTable.name).delete().where({
1387
+ [joinTable.joinColumn.name]: {
1388
+ $in: ids
1389
+ },
1390
+ ...joinTable.on
1391
+ }).transacting(trx).execute();
1392
+ }
1393
+ }
1394
+ },
1301
1395
  // TODO: add lifecycle events
1302
1396
  async populate (uid, entity, populate) {
1303
1397
  const entry = await this.findOne(uid, {