45 lines
876 B
JavaScript
Raw Normal View History

'use strict';
const pMap = require('p-map');
const { curry } = require('lodash/fp');
function pipeAsync(...methods) {
return async (data) => {
let res = data;
for (const method of methods) {
res = await method(res);
}
return res;
};
}
/**
* @type { import('./async').MapAsync }
*/
const mapAsync = curry(pMap);
2023-01-31 11:40:56 +01:00
/**
* 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) => {
switch (strapi.db.dialect.client) {
case 'mysql': {
2023-01-31 11:43:23 +01:00
return mapAsync(array, func);
2023-01-31 11:40:56 +01:00
}
default:
2023-01-31 11:43:23 +01:00
return Promise.all(array.map(func));
2023-01-31 11:40:56 +01:00
}
};
module.exports = {
mapAsync,
2023-01-31 11:40:56 +01:00
mapAsyncDialects,
pipeAsync,
};