2022-12-08 17:17:11 +01:00
|
|
|
'use strict';
|
|
|
|
|
2023-01-17 15:07:27 +01:00
|
|
|
const pMap = require('p-map');
|
|
|
|
const { curry } = require('lodash/fp');
|
2022-12-20 12:00:49 +01:00
|
|
|
|
2022-12-08 17:17:11 +01:00
|
|
|
function pipeAsync(...methods) {
|
|
|
|
return async (data) => {
|
|
|
|
let res = data;
|
|
|
|
|
|
|
|
for (const method of methods) {
|
|
|
|
res = await method(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-17 15:07:27 +01:00
|
|
|
* @type { import('./async').MapAsync }
|
2022-12-08 17:17:11 +01:00
|
|
|
*/
|
2023-01-17 15:07:27 +01:00
|
|
|
const mapAsync = curry(pMap);
|
2022-12-08 17:17:11 +01:00
|
|
|
|
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) => {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-12-08 17:17:11 +01:00
|
|
|
module.exports = {
|
|
|
|
mapAsync,
|
2023-01-31 11:40:56 +01:00
|
|
|
mapAsyncDialects,
|
2022-12-08 17:17:11 +01:00
|
|
|
pipeAsync,
|
|
|
|
};
|