rename id cloneId to targetId sourceId

This commit is contained in:
Marc-Roig 2023-03-23 10:44:12 +01:00 committed by Josh
parent e970451c9e
commit 2e7e8b6813
3 changed files with 16 additions and 15 deletions

View File

@ -111,8 +111,8 @@ const createRepository = (uid, db) => {
return db.entityManager.deleteRelations(uid, id);
},
cloneRelations(id, cloneId, params) {
return db.entityManager.cloneRelations(uid, id, cloneId, params);
cloneRelations(targetId, sourceId, params) {
return db.entityManager.cloneRelations(uid, targetId, sourceId, params);
},
populate(entity, populate) {

View File

@ -1178,14 +1178,15 @@ const createEntityManager = (db) => {
/**
*
* @param {string} uid - uid of the entity to clone
* @param {number} id - id of the entity to clone
* @param {number} cloneId - id of the cloned entity
* @param {number} targetId - id of the entity to clone into
* @param {number} sourceId - id of the entity to clone from
* @param {object} opt
* @param {object} opt.cloneAttrs - key value pair of attributes to clone
* @param {object} opt.transaction - transaction to use
* @example cloneRelations('article', 1, 2, { cloneAttrs: { categories: true } })
* @example cloneRelations('user', 3, 1, { cloneAttrs: { friends: true }})
* @example cloneRelations('post', 5, 2, { cloneAttrs: { comments: true, likes: true } })
*/
async cloneRelations(uid, id, cloneId, { cloneAttrs = {}, transaction }) {
async cloneRelations(uid, targetId, sourceId, { cloneAttrs = {}, transaction }) {
const { attributes } = db.metadata.get(uid);
if (!attributes) {
@ -1213,9 +1214,9 @@ const createEntityManager = (db) => {
}
if (isOneToAny(attribute) && isBidirectional(attribute)) {
await replaceRegularRelations({ id, cloneId, attribute, transaction });
await replaceRegularRelations({ targetId, sourceId, attribute, transaction });
} else {
await cloneRegularRelations({ id, cloneId, attribute, transaction });
await cloneRegularRelations({ targetId, sourceId, attribute, transaction });
}
});
},

View File

@ -2,15 +2,15 @@
const { cleanInverseOrderColumn } = require('../../regular-relations');
const replaceRegularRelations = async ({ id, cloneId, attribute, transaction: trx }) => {
const replaceRegularRelations = async ({ targetId, sourceId, attribute, transaction: trx }) => {
const { joinTable } = attribute;
const { joinColumn, inverseJoinColumn } = joinTable;
// We are effectively stealing the relation from the cloned entity
await strapi.db.entityManager
.createQueryBuilder(joinTable.name)
.update({ [joinColumn.name]: id })
.where({ [joinColumn.name]: cloneId })
.update({ [joinColumn.name]: targetId })
.where({ [joinColumn.name]: sourceId })
// TODO: Exclude some relations from being replaced
// .where({ $not: { [inverseJoinColumn.name]: relationsToDeleteIds } })
.onConflict([joinColumn.name, inverseJoinColumn.name])
@ -19,7 +19,7 @@ const replaceRegularRelations = async ({ id, cloneId, attribute, transaction: tr
.execute();
};
const cloneRegularRelations = async ({ id, cloneId, attribute, transaction: trx }) => {
const cloneRegularRelations = async ({ targetId, sourceId, attribute, transaction: trx }) => {
const { joinTable } = attribute;
const { joinColumn, inverseJoinColumn, orderColumnName, inverseOrderColumnName } = joinTable;
const connection = strapi.db.getConnection();
@ -33,11 +33,11 @@ const cloneRegularRelations = async ({ id, cloneId, attribute, transaction: trx
const selectStatement = connection
.select(
// Override joinColumn with the new id
{ [joinColumn.name]: id },
{ [joinColumn.name]: targetId },
// The rest of columns will be the same
...columns.slice(1)
)
.where(joinColumn.name, cloneId)
.where(joinColumn.name, sourceId)
// TODO: Exclude some relations from being replaced
// .where({ $not: { [inverseJoinColumn.name]: relationsToDeleteIds } })
.from(joinTable.name)
@ -60,7 +60,7 @@ const cloneRegularRelations = async ({ id, cloneId, attribute, transaction: trx
// Clean the inverse order column
if (inverseOrderColumnName) {
await cleanInverseOrderColumn({
id,
targetId,
attribute,
trx,
});