mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 02:44:55 +00:00
introduce use of mapAsync
This commit is contained in:
parent
3a4e811756
commit
cf88d212f9
@ -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);
|
||||
|
||||
@ -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,
|
||||
};
|
||||
|
||||
@ -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)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user