Adapt Loader service to the new API

This commit is contained in:
Kamal Bennani 2019-02-02 13:33:26 +01:00 committed by Alexandre Bodin
parent 2bdbf12b0a
commit 19c696ff25

View File

@ -85,15 +85,22 @@ module.exports = {
// Generate constant for skip parameters. // Generate constant for skip parameters.
// Note: we shouldn't support both way of doing this kind of things in the future. // Note: we shouldn't support both way of doing this kind of things in the future.
const skip = query.options.start || query.options.skip; const skip = query.options.start || query.options.skip || 0;
const limit = query.options.limit || 100;
// Extracting ids from original request to map with query results. // Extracting ids from original request to map with query results.
const ids = this.extractIds(query, ref); const ids = this.extractIds(query, ref);
if (!_.isArray(ids)) { if (!_.isArray(ids)) {
const ast = ref.associations.find(ast => ast.alias === ids.alias);
const astModel = this.retrieveModel(ast.model, ast.plugin);
return data return data
.filter(entry => entry[ids.alias].toString() === ids.value.toString()) .filter(entry => {
.slice(skip, skip + query.options.limit); const entryValue = entry[ids.alias][astModel.primaryKey];
return entryValue.toString() === ids.value.toString();
})
.slice(skip, skip + limit);
} }
// Critical: don't touch this part until you truly understand what you're doing. // Critical: don't touch this part until you truly understand what you're doing.
@ -102,7 +109,7 @@ module.exports = {
return data return data
.filter(entry => entry !== undefined) .filter(entry => entry !== undefined)
.filter(entry => ids.map(id => id.toString()).includes(entry[ref.primaryKey].toString())) .filter(entry => ids.map(id => id.toString()).includes(entry[ref.primaryKey].toString()))
.slice(skip, skip + query.options.limit); .slice(skip, skip + limit);
}); });
}, },
@ -125,31 +132,19 @@ module.exports = {
return []; return [];
} }
const ref = this.retrieveModel(model, query.options.source);
// Construct parameters object sent to the Content Manager service. // Construct parameters object sent to the Content Manager service.
// We are faking the `start`, `skip` and `limit` argument because it doesn't make sense because we are merging different requests in one.
// Note: we're trying to avoid useless populate for performances. Please be careful if you're updating this part. // Note: we're trying to avoid useless populate for performances. Please be careful if you're updating this part.
const populate = ref.associations
.filter(association => !association.dominant && _.isEmpty(association.model))
.map(association => association.alias);
const params = { const params = {
...query.options, ...query.options,
populate, populate: [], // Avoid useless population for performance reason
query: query.options.where || {}, query: {},
start: 0,
skip: 0,
limit: 100,
}; };
params.query[query.alias] = _.uniq(query.ids.filter(x => !_.isEmpty(x) || _.isInteger(x)).map(x => x.toString())); params.query[`${query.alias}_in`] = _.chain(query.ids)
.filter(id => !_.isEmpty(id) || _.isInteger(id)) // Only keep valid ids
if (['id', '_id'].includes(query.alias)) { .map(id => id.toString()) // convert ids to string
// However, we're applying a limit based on the number of entries we've to fetch. .uniq() // Remove redundant ids
// We'll apply the real `skip`, `start` and `limit` parameters during the mapping above. .value();
params.limit = params.query[query.alias].length;
}
// Run query and remove duplicated ID. // Run query and remove duplicated ID.
const request = await strapi.plugins['content-manager'].services['contentmanager'].fetchAll({ model }, params); const request = await strapi.plugins['content-manager'].services['contentmanager'].fetchAll({ model }, params);