From 2260b84f49ae98703872daac9fe72686c9f4a7bc Mon Sep 17 00:00:00 2001 From: Salman Date: Mon, 12 Sep 2022 18:59:17 +0530 Subject: [PATCH] fix: import relation --- server/config/type.js | 70 ++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/server/config/type.js b/server/config/type.js index 2e8b73f..4f797f6 100644 --- a/server/config/type.js +++ b/server/config/type.js @@ -79,15 +79,24 @@ const ConfigType = class ConfigType { this.relations.map(({ relationName }) => delete query[relationName]); const newEntity = await queryAPI.create({ data: query }); - // Create relation entities. - await Promise.all(this.relations.map(async ({ queryString, relationName, parentName }) => { + await Promise.all(this.relations.map(async ({ queryString, relationName, relationSortFields }) => { const relationQueryApi = strapi.query(queryString); - await Promise.all(configContent[relationName].map(async (relationEntity) => { - const relationQuery = { ...relationEntity, [parentName]: newEntity }; - await relationQueryApi.create({ data: relationQuery }); - })); - })); + const existingRows = await relationQueryApi.findMany({ + where: { + $and: relationSortFields.map((field) => ({ [field]: { $in: configContent[relationName].map(c => c[field] ) } })) + } + }) + + // Create relation entities. + await queryAPI.update({ + where: { id: newEntity.id }, + data: { + ...configContent, + [relationName]: existingRows.map(r => r.id) + } + }); + })) } else { // Config does exist in DB --> update config in DB // Format JSON fields. configContent = sanitizeConfig(configContent); @@ -95,6 +104,7 @@ const ConfigType = class ConfigType { this.jsonFields.map((field) => query[field] = JSON.stringify(configContent[field])); // Update entity. + // TODO: See if this is redundaunt now? this.relations.map(({ relationName }) => delete query[relationName]); const entity = await queryAPI.update({ where: combinedUidWhereFilter, data: query }); @@ -105,42 +115,20 @@ const ConfigType = class ConfigType { configContent = sanitizeConfig(configContent, relationName, relationSortFields); const configToAdd = difference(configContent[relationName], existingConfig[relationName], relationSortFields); - const configToDelete = difference(existingConfig[relationName], configContent[relationName], relationSortFields); - const configToUpdate = same(configContent[relationName], existingConfig[relationName], relationSortFields); - await Promise.all(configToDelete.map(async (config) => { - const whereClause = {}; - relationSortFields.map((sortField) => { - whereClause[sortField] = config[sortField]; - }); - await relationQueryApi.delete({ - where: { - ...whereClause, - [parentName]: entity.id, - }, - }); - })); + const existingRows = await relationQueryApi.findMany({ + where: { + $and: relationSortFields.map((field) => ({ [field]: { $in: configToAdd.map(c => c[field] ) } })) + } + }) - await Promise.all(configToAdd.map(async (config) => { - await relationQueryApi.create({ - data: { ...config, [parentName]: entity.id }, - }); - })); - - await Promise.all(configToUpdate.map(async (config, index) => { - const whereClause = {}; - relationSortFields.map((sortField) => { - whereClause[sortField] = config[sortField]; - }); - - await relationQueryApi.update({ - where: { - ...whereClause, - [parentName]: entity.id, - }, - data: { ...config, [parentName]: entity.id }, - }); - })); + await queryAPI.update({ + where: combinedUidWhereFilter, + data: { + ...configContent, + [relationName]: existingRows.map(r => r.id) + } + }); })); } }