diff --git a/packages/core/database/lib/entity-manager/index.js b/packages/core/database/lib/entity-manager/index.js index 2d091e919d..b6a7c72e19 100644 --- a/packages/core/database/lib/entity-manager/index.js +++ b/packages/core/database/lib/entity-manager/index.js @@ -211,17 +211,13 @@ const createEntityManager = (db) => { } const dataToInsert = processData(metadata, data, { withDefaults: true }); - let id; + + const res = await this.createQueryBuilder(uid).insert(dataToInsert).execute(); + + const id = res[0].id || res[0]; const trx = await strapi.db.transaction(); try { - const res = await this.createQueryBuilder(uid) - .insert(dataToInsert) - .transacting(trx) - .execute(); - - id = res[0].id || res[0]; - await this.attachRelations(uid, id, data, { transaction: trx }); await trx.commit(); @@ -293,18 +289,14 @@ const createEntityManager = (db) => { const { id } = entity; + const dataToUpdate = processData(metadata, data); + + if (!isEmpty(dataToUpdate)) { + await this.createQueryBuilder(uid).where({ id }).update(dataToUpdate).execute(); + } + const trx = await strapi.db.transaction(); try { - const dataToUpdate = processData(metadata, data); - - if (!isEmpty(dataToUpdate)) { - await this.createQueryBuilder(uid) - .where({ id }) - .update(dataToUpdate) - .transacting(trx) - .execute(); - } - await this.updateRelations(uid, id, data, { transaction: trx }); await trx.commit(); @@ -372,10 +364,10 @@ const createEntityManager = (db) => { const { id } = entity; + await this.createQueryBuilder(uid).where({ id }).delete().execute(); + const trx = await strapi.db.transaction(); try { - await this.createQueryBuilder(uid).where({ id }).delete().transacting(trx).execute(); - await this.deleteRelations(uid, id, { transaction: trx }); await trx.commit(); diff --git a/packages/core/strapi/lib/services/entity-service/components.js b/packages/core/strapi/lib/services/entity-service/components.js index 3ee9ea27b9..dbde6f9855 100644 --- a/packages/core/strapi/lib/services/entity-service/components.js +++ b/packages/core/strapi/lib/services/entity-service/components.js @@ -43,12 +43,9 @@ const createComponents = async (uid, data) => { throw new Error('Expected an array to create repeatable component'); } - // No Promise.all to avoid deadlocks - const components = []; - for (const value of componentValue) { - const compo = await createComponent(componentUID, value); - components.push(compo); - } + const components = await Promise.all( + componentValue.map((value) => createComponent(componentUID, value)) + ); componentBody[attributeName] = components.map(({ id }) => { return { @@ -80,18 +77,18 @@ const createComponents = async (uid, data) => { throw new Error('Expected an array to create repeatable component'); } - // No Promise.all to avoid deadlocks - componentBody[attributeName] = []; - for (const value of dynamiczoneValues) { - const { id } = await createComponent(value.__component, value); - componentBody[attributeName].push({ - id, - __component: value.__component, - __pivot: { - field: attributeName, - }, - }); - } + componentBody[attributeName] = await Promise.all( + dynamiczoneValues.map(async (value) => { + const { id } = await createComponent(value.__component, value); + return { + id, + __component: value.__component, + __pivot: { + field: attributeName, + }, + }; + }) + ); continue; } @@ -140,12 +137,9 @@ const updateComponents = async (uid, entityToUpdate, data) => { throw new Error('Expected an array to create repeatable component'); } - // No Promise.all to avoid deadlocks - const components = []; - for (const value of componentValue) { - const compo = await updateOrCreateComponent(componentUID, value); - components.push(compo); - } + const components = await Promise.all( + componentValue.map((value) => updateOrCreateComponent(componentUID, value)) + ); componentBody[attributeName] = components.filter(_.negate(_.isNil)).map(({ id }) => { return { @@ -179,18 +173,19 @@ const updateComponents = async (uid, entityToUpdate, data) => { throw new Error('Expected an array to create repeatable component'); } - // No Promise.all to avoid deadlocks - componentBody[attributeName] = []; - for (const value of dynamiczoneValues) { - const { id } = await updateOrCreateComponent(value.__component, value); - componentBody[attributeName].push({ - id, - __component: value.__component, - __pivot: { - field: attributeName, - }, - }); - } + componentBody[attributeName] = await Promise.all( + dynamiczoneValues.map(async (value) => { + const { id } = await updateOrCreateComponent(value.__component, value); + + return { + id, + __component: value.__component, + __pivot: { + field: attributeName, + }, + }; + }) + ); continue; } @@ -292,14 +287,14 @@ const deleteComponents = async (uid, entityToDelete, { loadComponents = true } = if (attribute.type === 'component') { const { component: componentUID } = attribute; - for (const subValue of _.castArray(value)) { - await deleteComponent(componentUID, subValue); - } + await Promise.all( + _.castArray(value).map((subValue) => deleteComponent(componentUID, subValue)) + ); } else { // delete dynamic zone components - for (const subValue of _.castArray(value)) { - await deleteComponent(subValue.__component, subValue); - } + await Promise.all( + _.castArray(value).map((subValue) => deleteComponent(subValue.__component, subValue)) + ); } continue;