Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/select #82

Merged
merged 2 commits into from
Feb 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion lib/configuration/hooks/blueprints/actionUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ module.exports = {
*/

populateEach: function (query, _ctx, model) {
const selectedAttributes = _.get(this.parseSelect(_ctx), 'select');
let shouldPopulate = strapi.config.blueprints.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
// need this to provide flexibility in the request param. This way both
Expand All @@ -34,7 +46,7 @@ module.exports = {
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 (aliasFilter) {
Expand Down Expand Up @@ -236,5 +248,17 @@ module.exports = {
skip = +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;
}
};
2 changes: 1 addition & 1 deletion lib/configuration/hooks/blueprints/actions/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function find(_ctx) {
const Model = actionUtil.parseModel(_ctx);

// Init the query.
let query = Model.find()
let query = Model.find(actionUtil.parseSelect(_ctx))
.where(actionUtil.parseCriteria(_ctx))
.limit(actionUtil.parseLimit(_ctx))
.skip(actionUtil.parseSkip(_ctx))
Expand Down
8 changes: 7 additions & 1 deletion lib/configuration/hooks/blueprints/actions/findOne.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ module.exports = function findOne(_ctx) {
// Locate and validate the required `id` parameter.
const pk = actionUtil.requirePk(_ctx);

// Build criteria object
const criteria = {};
criteria[Model.primaryKey] = pk;

_.merge(criteria, actionUtil.parseSelect(_ctx));

// Init the query.
let query = Model.findOne(pk);
let query = Model.findOne(criteria);
query = actionUtil.populateEach(query, _ctx, Model);
query.exec(function found(err, matchingRecord) {
if (err) {
Expand Down