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

[patch] Allow populates to use select/omit for singular associations #1615

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions lib/waterline/utils/query/forge-stage-two-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,10 @@ module.exports = function forgeStageTwoQuery(query, orm) {
// │ ├─┤├┤ │ ├┴┐ │ ├─┤├┤ ╠╦╝╠═╣╚═╗
// └─┘┴ ┴└─┘└─┘┴ ┴ ┴ ┴ ┴└─┘ ╩╚═╩ ╩╚═╝

// we perform criteria normalization for plural ("collection") associations
// and *SOME* singular ("model") associations (only allowing `select` or `omit`)
var performNormalizeCriteria = false;

// If this is a singular ("model") association, then it should always have
// an empty dictionary on the RHS. (For this type of association, there is
// always either exactly one associated record, or none of them.)
Expand All @@ -873,26 +877,34 @@ module.exports = function forgeStageTwoQuery(query, orm) {
if (_.isEqual(query.populates[populateAttrName], {})) {
query.populates[populateAttrName] = true;
}
// Otherwise, this simply must be `true`. Otherwise it's invalid.
// Otherwise, this simply must be `true` or the allowed `select`/`omit` clause. Otherwise it's invalid.
else {

if (query.populates[populateAttrName] !== true) {
throw buildUsageError(
'E_INVALID_POPULATES',
'Could not populate `'+populateAttrName+'`. '+
'This is a singular ("model") association, which means it never refers to '+
'more than _one_ associated record. So passing in subcriteria (i.e. as '+
'the second argument to `.populate()`) is not supported for this association, '+
'since it generally wouldn\'t make any sense. But that\'s the trouble-- it '+
'looks like some sort of a subcriteria (or something) _was_ provided!\n'+
'(Note that subcriterias consisting ONLY of `omit` or `select` are a special '+
'case that _does_ make sense. This usage will be supported in a future version '+
'of Waterline.)\n'+
'\n'+
'Here\'s what was passed in:\n'+
util.inspect(query.populates[populateAttrName], {depth: 5}),
query.using
);

// Allow populate for singular associations to only contain a `select` or
// `omit` clause.
var p = query.populates[populateAttrName];
if (_.isObject(p) && !_.isArray(p) && _.size(p)===1 && (_.isArray(p.select) || _.isArray(p.omit))) {
// normalization performed below
performNormalizeCriteria = true;
} else {
throw buildUsageError(
'E_INVALID_POPULATES',
'Could not populate `'+populateAttrName+'`. '+
'This is a singular ("model") association, which means it never refers to '+
'more than _one_ associated record. So passing in subcriteria (i.e. as '+
'the second argument to `.populate()`) is not supported for this association, '+
'since it generally wouldn\'t make any sense. But that\'s the trouble-- it '+
'looks like some sort of a subcriteria (or something) _was_ provided!\n'+
'(Note that subcriterias consisting ONLY of `omit` or `select` are a special '+
'case that _does_ make sense and is supported)\n'+
'\n'+
'Here\'s what was passed in:\n'+
util.inspect(query.populates[populateAttrName], {depth: 5}),
query.using
);
}//-•
}//-•

}//>-•
Expand All @@ -902,6 +914,13 @@ module.exports = function forgeStageTwoQuery(query, orm) {
// validate and fully-normalize the provided subcriteria.
else {

// normalization performed below
performNormalizeCriteria = true;

}//</else :: this is a plural ("collection") association>

if(performNormalizeCriteria) {

// For compatibility, interpet a subcriteria of `true` to mean that there
// is really no subcriteria at all, and that we should just use the default (`{}`).
// > This will be further expanded into a fully-formed criteria dictionary shortly.
Expand Down Expand Up @@ -1025,7 +1044,7 @@ module.exports = function forgeStageTwoQuery(query, orm) {



}//</else :: this is a plural ("collection") association>
}//</else :: if(performNormalizeCriteria)


});//</_.each() key in the `populates` dictionary>
Expand Down