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

Add .pick() and .omit() methods #952

Closed
wants to merge 6 commits into from
Closed

Add .pick() and .omit() methods #952

wants to merge 6 commits into from

Conversation

dmarcelino
Copy link
Member

This PR is the same as #509, but targeting 0.11, props to @jasonsims. Please check #509 for the full discussion.

Original message:

This PR adds the ability to apply projections to waterline queries. It's an extension of the work accomplished in #162 by adding flexibility for how selection arguments are supplied as well as exclusions via .omit(). With this change, the adapters will always receive a standardized selection criteria object which they can process as needed for their specific query language. Here's an example of this object:

// User.find().pick('name', 'age').exec(cb);
{ where: null, pick: { name: 1, age: 1 } }

// User.find().omit('name', 'age').exec(cb);
{ where: null, omit: { name: 0, age: 0 } }

Usage examples:

// Include only 
User.find().pick('name', 'age').exec(cb);
User.find().pick(['name', 'age']).exec(cb);
User.find().pick({name: 1, age: 1}).exec(cb);

// Exclusion
User.find().omit('name', 'age').exec(cb);
User.find().omit(['name', 'age']).exec(cb);
User.find().omit({name: 0, age: 0}).exec(cb);

fixes #73

cc: @clarkorz, @mikermcneil, @mdunisch, @particlebanana, @sskyy, @devinivy, @tjwebb

jasonsims and others added 6 commits August 15, 2014 14:43
This commit adds a `pick` clause to the criteria object which allows for
including only specific model attributes within the query result. It accepts
the follwing syntax:
  `.pick(['attr1', 'attr2'])`
  `.pick('attr1', 'attr2')`
  `.pick({attr1: 1, attr2: 1})`
This commit adds a `omit` clause to the criteria object which allows for
excluding model attributes from the query result. It accepts the follwing
syntax:
  `.omit(['attr1', 'attr2'])`
  `.omit('attr1', 'attr2')`
  `.omit({attr1: 1, attr2: 1})`
@dmarcelino
Copy link
Member Author

#73 actually asks for a .select() method so we should probably follow @devinivy's suggestion on #509 (comment):

I think pick and select should be aliases.

Any volunteers for this?

@tjwebb
Copy link
Contributor

tjwebb commented Apr 21, 2015

re: http://stackoverflow.com/a/29769995/291180

@dmarcelino would you mind adding a quick sentence or two in the docs about how the select criteria works? I think I didn't know this feature existed either :)

@dmarcelino
Copy link
Member Author

@tjwebb, I can't believe you didn't know about it... to be honest I don't remember how I found out it existed. Sure, I'll look into adding it.

@tjwebb
Copy link
Contributor

tjwebb commented Apr 21, 2015

to be honest I don't remember how I found out it existed. Sure, I'll look into adding it.

Yea I sort of knew about it, but I've never used it and didn't really know the API. Is it unit tested?

@dmarcelino
Copy link
Member Author

I don't think it is, there are some tests that use it (quick search) but no particular unit test to explicitly test it.

@alexlenail
Copy link

Why isn't this a feature yet?

@weyert
Copy link

weyert commented Oct 31, 2015

Any news on this?

@randallmeeker
Copy link
Contributor

closed w/o comment?

@chacliff
Copy link

chacliff commented Jun 15, 2016

personally i think that this should be structured just like mongo structures it... if your going to use "select" or "pick"... not omit it should just be { field: 0 || 1 }, where 0 is to omit, and 1 is to include. by default if no selection is made everything is included. by default if a field is marked with 1 ID is still included unless explicitly marked 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
8 participants