2018-11-22 16:40:52 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loaders.js service
|
|
|
|
*
|
|
|
|
* @description: A set of functions similar to controller's actions to avoid code duplication.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
const DataLoader = require('dataloader');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
loaders: {},
|
|
|
|
|
2019-01-05 18:14:00 +01:00
|
|
|
initializeLoader: function() {
|
2019-01-16 17:26:15 +01:00
|
|
|
this.resetLoaders();
|
|
|
|
|
2019-01-05 18:14:00 +01:00
|
|
|
// Create loaders for each relational field (exclude core models).
|
|
|
|
Object.keys(strapi.models)
|
|
|
|
.filter(model => model !== 'core_store')
|
|
|
|
.forEach(model => {
|
|
|
|
(strapi.models[model].associations || []).forEach(association => this.createLoader(association.collection || association.model, association.plugin));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Reproduce the same pattern for each plugin.
|
|
|
|
Object.keys(strapi.plugins).forEach(plugin => {
|
|
|
|
Object.keys(strapi.plugins[plugin].models).forEach(model => {
|
|
|
|
(strapi.plugins[plugin].models[model].associations || []).forEach(association => this.createLoader(association.collection || association.model, association.plugin));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-01-22 13:47:44 +01:00
|
|
|
resetLoaders: function () {
|
2019-01-16 17:26:15 +01:00
|
|
|
this.loaders = {};
|
|
|
|
},
|
|
|
|
|
2019-01-05 18:14:00 +01:00
|
|
|
createLoader: function(model, plugin) {
|
|
|
|
const name = plugin ? `${plugin}__${model}`: model;
|
|
|
|
|
2019-01-22 13:47:44 +01:00
|
|
|
// Exclude polymorphic from loaders.
|
|
|
|
if (name === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-16 17:26:15 +01:00
|
|
|
if (this.loaders[name]) {
|
|
|
|
return this.loaders[name];
|
|
|
|
}
|
|
|
|
|
2019-01-05 18:14:00 +01:00
|
|
|
this.loaders[name] = new DataLoader(keys => {
|
2018-11-22 16:40:52 +01:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
// Extract queries from keys and merge similar queries.
|
|
|
|
const { queries, map } = this.extractQueries(model, _.cloneDeep(keys));
|
|
|
|
// Run queries in parallel.
|
|
|
|
const results = await Promise.all(queries.map((query) => this.makeQuery(model, query)));
|
|
|
|
// Use to match initial queries order.
|
2018-11-22 17:33:51 +01:00
|
|
|
const data = this.mapData(model, keys, map, results);
|
|
|
|
|
|
|
|
resolve(data);
|
2018-11-22 16:40:52 +01:00
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, {
|
|
|
|
cacheKeyFn: (key) => {
|
|
|
|
return _.isObjectLike(key) ? JSON.stringify(_.cloneDeep(key)) : key;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
mapData: function(model, originalMap, map, results) {
|
|
|
|
// Use map to re-dispatch data correctly based on initial keys.
|
|
|
|
return originalMap.map((query, index) => {
|
|
|
|
// Find the index of where we should extract the results.
|
|
|
|
const indexResults = map.findIndex(queryMap => queryMap.indexOf(index) !== -1);
|
|
|
|
const data = results[indexResults];
|
|
|
|
|
|
|
|
// Retrieving referring model.
|
|
|
|
const ref = this.retrieveModel(model, query.options.source);
|
2018-11-22 18:32:33 +01:00
|
|
|
|
|
|
|
if (query.single) {
|
|
|
|
// Return object instead of array for one-to-many relationship.
|
2018-11-23 17:46:43 +01:00
|
|
|
return data.find(entry => entry[ref.primaryKey].toString() === (query.params[ref.primaryKey] || '').toString());
|
2018-11-22 18:32:33 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 17:16:28 +01:00
|
|
|
// Generate constant for skip parameters.
|
|
|
|
// Note: we shouldn't support both way of doing this kind of things in the future.
|
2019-02-13 21:26:37 +01:00
|
|
|
const skip = query.options._start || query.options._skip || 0;
|
|
|
|
const limit = _.get(query, 'options._limit', 100); // Take into account the limit if its equal 0
|
2018-11-22 19:57:26 +01:00
|
|
|
|
2019-01-15 17:16:28 +01:00
|
|
|
// Extracting ids from original request to map with query results.
|
2019-02-02 22:16:14 +01:00
|
|
|
const ids = this.extractIds(query);
|
|
|
|
const ast = ref.associations.find(ast => ast.alias === ids.alias);
|
|
|
|
const astModel = this.retrieveModel(ast.model || ast.collection, ast.plugin);
|
2018-11-22 16:40:52 +01:00
|
|
|
|
2018-12-06 18:11:53 +01:00
|
|
|
return data
|
|
|
|
.filter(entry => entry !== undefined)
|
2019-02-02 22:16:14 +01:00
|
|
|
.filter(entry => {
|
|
|
|
const aliasEntry = entry[ids.alias];
|
|
|
|
if (_.isArray(aliasEntry)) {
|
|
|
|
return _.find(
|
|
|
|
aliasEntry,
|
|
|
|
value => value[astModel.primaryKey].toString() === ids.value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const entryValue = aliasEntry[astModel.primaryKey].toString();
|
|
|
|
return entryValue === ids.value;
|
|
|
|
})
|
2019-02-02 13:33:26 +01:00
|
|
|
.slice(skip, skip + limit);
|
2018-11-22 16:40:52 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-02-02 22:16:14 +01:00
|
|
|
extractIds: (query) => {
|
2019-01-15 17:16:28 +01:00
|
|
|
const alias = _.first(Object.keys(query.options.query));
|
2019-02-02 22:16:14 +01:00
|
|
|
const value = query.options.query[alias].toString();
|
2019-01-15 17:16:28 +01:00
|
|
|
return {
|
|
|
|
alias,
|
2019-02-02 22:16:14 +01:00
|
|
|
value,
|
2019-01-15 17:16:28 +01:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2018-11-22 16:40:52 +01:00
|
|
|
makeQuery: async function(model, query = {}) {
|
2018-11-27 18:48:37 +01:00
|
|
|
if (_.isEmpty(query.ids)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-02-02 22:16:14 +01:00
|
|
|
// Retrieving referring model.
|
|
|
|
const ref = this.retrieveModel(model, query.options.source);
|
|
|
|
const ast = ref.associations.find(ast => ast.alias === query.alias);
|
|
|
|
|
2018-11-22 19:57:26 +01:00
|
|
|
const params = {
|
2018-11-22 16:40:52 +01:00
|
|
|
...query.options,
|
2019-02-02 22:16:14 +01:00
|
|
|
populate: ast ? [query.alias] : [], // Avoid useless population for performance reason
|
2019-02-02 13:33:26 +01:00
|
|
|
query: {},
|
2019-02-13 21:26:37 +01:00
|
|
|
_start: 0, // Don't apply start or skip
|
|
|
|
_limit: -1, // Don't apply a limit
|
2018-11-22 19:57:26 +01:00
|
|
|
};
|
|
|
|
|
2019-02-02 13:33:26 +01:00
|
|
|
params.query[`${query.alias}_in`] = _.chain(query.ids)
|
|
|
|
.filter(id => !_.isEmpty(id) || _.isInteger(id)) // Only keep valid ids
|
|
|
|
.map(id => id.toString()) // convert ids to string
|
|
|
|
.uniq() // Remove redundant ids
|
|
|
|
.value();
|
2018-11-22 19:57:26 +01:00
|
|
|
|
|
|
|
// Run query and remove duplicated ID.
|
|
|
|
const request = await strapi.plugins['content-manager'].services['contentmanager'].fetchAll({ model }, params);
|
2018-11-27 18:48:37 +01:00
|
|
|
|
2018-11-22 16:40:52 +01:00
|
|
|
return request && request.toJSON ? request.toJSON() : request;
|
|
|
|
},
|
|
|
|
|
|
|
|
retrieveModel: function(model, source) {
|
|
|
|
// Retrieve refering model.
|
|
|
|
return source ?
|
|
|
|
strapi.plugins[source].models[model]:
|
|
|
|
strapi.models[model];
|
|
|
|
},
|
|
|
|
|
|
|
|
extractQueries: function(model, keys) {
|
|
|
|
const queries = [];
|
|
|
|
const map = [];
|
|
|
|
|
|
|
|
keys.forEach((current, index) => {
|
|
|
|
// Extract query options.
|
2019-01-15 17:16:28 +01:00
|
|
|
// Note: the `single` means that we've only one entry to fetch.
|
2018-11-22 19:57:26 +01:00
|
|
|
const { single = false, params = {}, association } = current;
|
2018-11-22 18:32:33 +01:00
|
|
|
const { query = {}, ...options } = current.options;
|
|
|
|
|
2018-11-22 16:40:52 +01:00
|
|
|
// Retrieving referring model.
|
|
|
|
const ref = this.retrieveModel(model, options.source);
|
|
|
|
|
|
|
|
// Find similar query.
|
|
|
|
const indexQueries = queries.findIndex(query => _.isEqual(query.options, options));
|
|
|
|
|
2019-01-15 17:16:28 +01:00
|
|
|
// Generate array of IDs to fetch.
|
|
|
|
const ids = [];
|
|
|
|
|
|
|
|
// Only one entry to fetch.
|
|
|
|
if (single) {
|
|
|
|
ids.push(params[ref.primaryKey]);
|
|
|
|
} else {
|
|
|
|
ids.push(query[association.via]);
|
|
|
|
}
|
2018-11-22 19:57:26 +01:00
|
|
|
|
2018-11-22 16:40:52 +01:00
|
|
|
if (indexQueries !== -1) {
|
|
|
|
// Push to the same query the new IDs to fetch.
|
2018-11-22 19:57:26 +01:00
|
|
|
queries[indexQueries].ids.push(...ids);
|
2018-11-22 16:40:52 +01:00
|
|
|
map[indexQueries].push(index);
|
|
|
|
} else {
|
|
|
|
// Create new query in the query.
|
|
|
|
queries.push({
|
2018-11-22 19:57:26 +01:00
|
|
|
ids,
|
|
|
|
options,
|
2018-11-23 17:33:41 +01:00
|
|
|
alias: _.first(Object.keys(query)) || ref.primaryKey
|
2018-11-22 16:40:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
map[queries.length - 1 > 0 ? queries.length - 1 : 0] = [];
|
|
|
|
map[queries.length - 1].push(index);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
queries,
|
|
|
|
map
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|