From cf88d212f960ebf41e5deeca3e2b869633eda5c3 Mon Sep 17 00:00:00 2001 From: Marc-Roig Date: Tue, 31 Jan 2023 11:40:56 +0100 Subject: [PATCH] introduce use of mapAsync --- .../lib/services/entity-service/components.js | 23 +--------------- packages/core/utils/lib/async.js | 26 +++++++++++++++++++ .../i18n/server/services/localizations.js | 13 +++++----- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/packages/core/strapi/lib/services/entity-service/components.js b/packages/core/strapi/lib/services/entity-service/components.js index e853aa436e..32ef7a7f94 100644 --- a/packages/core/strapi/lib/services/entity-service/components.js +++ b/packages/core/strapi/lib/services/entity-service/components.js @@ -6,6 +6,7 @@ const { has, prop, omit, toString, pipe, assign } = require('lodash/fp'); const { contentTypes: contentTypesUtils } = require('@strapi/utils'); const { ApplicationError } = require('@strapi/utils').errors; const { getComponentAttributes } = require('@strapi/utils').contentTypes; +const { mapAsyncDialects } = require('@strapi/utils').async; const omitComponentData = (contentType, data) => { const { attributes } = contentType; @@ -16,28 +17,6 @@ const omitComponentData = (contentType, data) => { return omit(componentAttributes, data); }; -/** - * Because of the way mysql works, making parallel requests can cause deadlocks. - * This function will run the requests sequentially if mysql is used. - */ -const mapAsyncDialects = async (array, func) => { - const results = []; - - switch (strapi.db.dialect.client) { - case 'mysql': { - for (const elm of array) { - results.push(await func(elm)); - } - break; - } - default: - results.push(...(await Promise.all(array.map(func)))); - break; - } - - return results; -}; - // NOTE: we could generalize the logic to allow CRUD of relation directly in the DB layer const createComponents = async (uid, data) => { const { attributes = {} } = strapi.getModel(uid); diff --git a/packages/core/utils/lib/async.js b/packages/core/utils/lib/async.js index 4ceb986a5b..ce77330527 100644 --- a/packages/core/utils/lib/async.js +++ b/packages/core/utils/lib/async.js @@ -20,7 +20,33 @@ function pipeAsync(...methods) { */ const mapAsync = curry(pMap); +/** + * Because of how mysql works, making parallel requests can cause deadlocks. + * This function will run the requests sequentially if mysql is used. Else, + * it will encapsulate them in a Promise.all. + * + * @type { import('./async').MapAsync } + */ +const mapAsyncDialects = async (array, func) => { + const results = []; + + switch (strapi.db.dialect.client) { + case 'mysql': { + mapAsync(array, async (elm) => { + results.push(await func(elm)); + }); + break; + } + default: + results.push(...(await Promise.all(array.map(func)))); + break; + } + + return results; +}; + module.exports = { mapAsync, + mapAsyncDialects, pipeAsync, }; diff --git a/packages/plugins/i18n/server/services/localizations.js b/packages/plugins/i18n/server/services/localizations.js index 96bed4dbab..d798557a57 100644 --- a/packages/plugins/i18n/server/services/localizations.js +++ b/packages/plugins/i18n/server/services/localizations.js @@ -2,6 +2,7 @@ const { prop, isNil, isEmpty } = require('lodash/fp'); +const { mapAsyncDialects } = require('@strapi/utils').async; const { getService } = require('../utils'); /** @@ -32,9 +33,9 @@ const syncLocalizations = async (entry, { model }) => { return strapi.query(model.uid).update({ where: { id }, data: { localizations } }); }; - for (const localization of entry.localizations) { - await updateLocalization(localization.id); - } + await mapAsyncDialects(entry.localizations, (localization) => + updateLocalization(localization.id) + ); } }; @@ -58,9 +59,9 @@ const syncNonLocalizedAttributes = async (entry, { model }) => { return strapi.entityService.update(model.uid, id, { data: nonLocalizedAttributes }); }; - for (const localization of entry.localizations) { - await updateLocalization(localization.id); - } + await mapAsyncDialects(entry.localizations, (localization) => + updateLocalization(localization.id) + ); } };