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