mirror of
https://github.com/strapi/strapi.git
synced 2025-11-03 11:25:17 +00:00
refactor deleteAllRelations
This commit is contained in:
parent
4ce09c5193
commit
3b0d6b6e7f
@ -26,8 +26,8 @@ const { isBidirectional, isManyToAny, isAnyToOne, isAnyToMany } = require('../me
|
||||
const {
|
||||
deletePreviousOneToAnyRelations,
|
||||
deletePreviousAnyToOneRelations,
|
||||
deleteAllRelations,
|
||||
} = require('./utils');
|
||||
deleteRelations,
|
||||
} = require('./regular-relations');
|
||||
|
||||
const toId = (value) => value.id || value;
|
||||
const toIds = (value) => castArray(value || []).map(toId);
|
||||
@ -723,7 +723,7 @@ const createEntityManager = (db) => {
|
||||
|
||||
// only delete relations
|
||||
if (isNull(cleanRelationData.set)) {
|
||||
await deleteAllRelations({ id, attribute, joinTable, db });
|
||||
await deleteRelations({ id, attribute, joinTable, db }, { relsToDelete: 'all' });
|
||||
} else {
|
||||
const isPartialUpdate = !has('set', cleanRelationData);
|
||||
let relIdsToaddOrMove;
|
||||
@ -740,7 +740,10 @@ const createEntityManager = (db) => {
|
||||
differenceWith(isEqual, cleanRelationData.disconnect, cleanRelationData.connect)
|
||||
);
|
||||
|
||||
await deleteAllRelations({ id, attribute, joinTable, onlyFor: relIdsToDelete, db });
|
||||
await deleteRelations(
|
||||
{ id, attribute, joinTable, db },
|
||||
{ relsToDelete: relIdsToDelete }
|
||||
);
|
||||
|
||||
// add/move
|
||||
let max;
|
||||
@ -833,7 +836,10 @@ const createEntityManager = (db) => {
|
||||
} else {
|
||||
// overwrite all relations
|
||||
relIdsToaddOrMove = toIds(cleanRelationData.set);
|
||||
await deleteAllRelations({ id, attribute, joinTable, except: relIdsToaddOrMove, db });
|
||||
await deleteRelations(
|
||||
{ id, attribute, joinTable, db },
|
||||
{ relsToDelete: 'all', relsToNotDelete: relIdsToaddOrMove }
|
||||
);
|
||||
|
||||
const currentMovingRels = await this.createQueryBuilder(joinTable.name)
|
||||
.select(select)
|
||||
@ -1025,7 +1031,7 @@ const createEntityManager = (db) => {
|
||||
if (attribute.joinTable) {
|
||||
const { joinTable } = attribute;
|
||||
|
||||
await deleteAllRelations({ id, attribute, joinTable, db });
|
||||
await deleteRelations({ id, attribute, joinTable, db }, { relsToDelete: 'all' });
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@ -110,16 +110,13 @@ const deletePreviousAnyToOneRelations = async ({ id, attribute, joinTable, relId
|
||||
};
|
||||
|
||||
// INVERSE ORDER UPDATE
|
||||
const deleteAllRelations = async ({
|
||||
id,
|
||||
attribute,
|
||||
joinTable,
|
||||
except = undefined,
|
||||
onlyFor = undefined,
|
||||
db,
|
||||
}) => {
|
||||
const deleteRelations = async (
|
||||
{ id, attribute, joinTable, db },
|
||||
{ relsToNotDelete = [], relsToDelete = [] }
|
||||
) => {
|
||||
const { joinColumn, inverseJoinColumn, orderColumnName, inverseOrderColumnName } = joinTable;
|
||||
const select = getSelect(joinTable, attribute);
|
||||
const all = relsToDelete === 'all';
|
||||
|
||||
if (isAnyToMany(attribute) || (isBidirectional(attribute) && isManyToAny(attribute))) {
|
||||
let lastId = 0;
|
||||
@ -131,8 +128,8 @@ const deleteAllRelations = async ({
|
||||
.where({
|
||||
[joinColumn.name]: id,
|
||||
id: { $gt: lastId },
|
||||
...(except ? { [inverseJoinColumn.name]: { $notIn: except } } : {}),
|
||||
...(onlyFor ? { [inverseJoinColumn.name]: { $in: onlyFor } } : {}),
|
||||
[inverseJoinColumn.name]: { $notIn: relsToNotDelete },
|
||||
...(all ? {} : { [inverseJoinColumn.name]: { $in: relsToDelete } }),
|
||||
})
|
||||
.where(joinTable.on || {})
|
||||
.orderBy('id')
|
||||
@ -186,8 +183,8 @@ const deleteAllRelations = async ({
|
||||
.delete()
|
||||
.where({
|
||||
[joinColumn.name]: id,
|
||||
...(except ? { [inverseJoinColumn.name]: { $notIn: except } } : {}),
|
||||
...(onlyFor ? { [inverseJoinColumn.name]: { $in: onlyFor } } : {}),
|
||||
[inverseJoinColumn.name]: { $notIn: relsToNotDelete },
|
||||
...(all ? {} : { [inverseJoinColumn.name]: { $in: relsToDelete } }),
|
||||
})
|
||||
.where(joinTable.on || {})
|
||||
.execute();
|
||||
@ -196,5 +193,5 @@ const deleteAllRelations = async ({
|
||||
module.exports = {
|
||||
deletePreviousOneToAnyRelations,
|
||||
deletePreviousAnyToOneRelations,
|
||||
deleteAllRelations,
|
||||
deleteRelations,
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user