From a6f4afeda6b0f08a7aab7dc5c84c1a284944bbca Mon Sep 17 00:00:00 2001 From: Marc Date: Wed, 29 Mar 2023 13:02:06 +0200 Subject: [PATCH] refactor: change cloneRelations cloneAttrs format --- packages/core/database/lib/entity-manager/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/core/database/lib/entity-manager/index.js b/packages/core/database/lib/entity-manager/index.js index 1d19c3e317..b72594db37 100644 --- a/packages/core/database/lib/entity-manager/index.js +++ b/packages/core/database/lib/entity-manager/index.js @@ -399,11 +399,12 @@ const createEntityManager = (db) => { const trx = await strapi.db.transaction(); try { const cloneAttrs = Object.entries(metadata.attributes).reduce((acc, [attrName, attr]) => { + // TODO: handle components in the db layer if (attr.type === 'relation' && attr.joinTable && !attr.component) { - acc[attrName] = true; + acc.push(attrName); } return acc; - }, {}); + }, []); await this.cloneRelations(uid, id, cloneId, data, { cloneAttrs, transaction: trx.get() }); await trx.commit(); @@ -1241,18 +1242,17 @@ const createEntityManager = (db) => { * @param {object} opt * @param {object} opt.cloneAttrs - key value pair of attributes to clone * @param {object} opt.transaction - transaction to use - * @example cloneRelations('user', 3, 1, { cloneAttrs: { friends: true }}) - * @example cloneRelations('post', 5, 2, { cloneAttrs: { comments: true, likes: true } }) + * @example cloneRelations('user', 3, 1, { cloneAttrs: ["comments"]}) + * @example cloneRelations('post', 5, 2, { cloneAttrs: ["comments", "likes"] }) */ - async cloneRelations(uid, targetId, sourceId, data, { cloneAttrs = {}, transaction }) { + async cloneRelations(uid, targetId, sourceId, data, { cloneAttrs = [], transaction }) { const { attributes } = db.metadata.get(uid); if (!attributes) { return; } - await mapAsync(Object.entries(cloneAttrs), async ([attrName, shouldClone]) => { - if (!shouldClone) return; + await mapAsync(cloneAttrs, async (attrName) => { const attribute = attributes[attrName]; if (attribute.type !== 'relation') {