mirror of
https://github.com/strapi/strapi.git
synced 2025-11-15 01:28:07 +00:00
Add select attribute in blueprints
This commit is contained in:
parent
d3ce773ef9
commit
ab4416c2b2
@ -21,8 +21,20 @@ module.exports = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
populateEach: function (query, _ctx, model) {
|
populateEach: function (query, _ctx, model) {
|
||||||
|
const selectedAttributes = _.get(this.parseSelect(_ctx), 'select');
|
||||||
let shouldPopulate = strapi.config.blueprints.populate;
|
let shouldPopulate = strapi.config.blueprints.populate;
|
||||||
let aliasFilter = (_ctx.request.query && _ctx.request.query.populate) || (_ctx.request.body && _ctx.request.body.populate);
|
let aliasFilter = (_ctx.request.query && _ctx.request.query.populate) || (_ctx.request.body && _ctx.request.body.populate);
|
||||||
|
let associationToPopulate;
|
||||||
|
|
||||||
|
if (!_.isUndefined(selectedAttributes)) {
|
||||||
|
associationToPopulate = _.remove(_.map(model.associations, function (association) {
|
||||||
|
return _.includes(selectedAttributes, association.alias) ? association : undefined;
|
||||||
|
}), function (n) {
|
||||||
|
return !_.isUndefined(n);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
associationToPopulate = model.associations;
|
||||||
|
}
|
||||||
|
|
||||||
// Convert the string representation of the filter list to an array. We
|
// Convert the string representation of the filter list to an array. We
|
||||||
// need this to provide flexibility in the request param. This way both
|
// need this to provide flexibility in the request param. This way both
|
||||||
@ -34,7 +46,7 @@ module.exports = {
|
|||||||
aliasFilter = (aliasFilter) ? aliasFilter.split(',') : [];
|
aliasFilter = (aliasFilter) ? aliasFilter.split(',') : [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return _(model.associations).reduce(function populateEachAssociation(query, association) {
|
return _(associationToPopulate).reduce(function populateEachAssociation(query, association) {
|
||||||
|
|
||||||
// If an alias filter was provided, override the blueprint config.
|
// If an alias filter was provided, override the blueprint config.
|
||||||
if (aliasFilter) {
|
if (aliasFilter) {
|
||||||
@ -236,5 +248,17 @@ module.exports = {
|
|||||||
skip = +skip;
|
skip = +skip;
|
||||||
}
|
}
|
||||||
return skip;
|
return skip;
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse select params.
|
||||||
|
*
|
||||||
|
* @param {Object} _ctx
|
||||||
|
*/
|
||||||
|
|
||||||
|
parseSelect: function (_ctx) {
|
||||||
|
_ctx.options = _ctx.options || {};
|
||||||
|
const select = _ctx.request.query.hasOwnProperty('select') ? _.pick(_ctx.request.query, 'select') : {};
|
||||||
|
return select;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -23,7 +23,7 @@ module.exports = function find(_ctx) {
|
|||||||
const Model = actionUtil.parseModel(_ctx);
|
const Model = actionUtil.parseModel(_ctx);
|
||||||
|
|
||||||
// Init the query.
|
// Init the query.
|
||||||
let query = Model.find()
|
let query = Model.find(actionUtil.parseSelect(_ctx))
|
||||||
.where(actionUtil.parseCriteria(_ctx))
|
.where(actionUtil.parseCriteria(_ctx))
|
||||||
.limit(actionUtil.parseLimit(_ctx))
|
.limit(actionUtil.parseLimit(_ctx))
|
||||||
.skip(actionUtil.parseSkip(_ctx))
|
.skip(actionUtil.parseSkip(_ctx))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user