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