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

allow $select in params query when get #450

Closed
beeplin opened this issue Nov 3, 2016 · 6 comments
Closed

allow $select in params query when get #450

beeplin opened this issue Nov 3, 2016 · 6 comments
Assignees

Comments

@beeplin
Copy link
Contributor

beeplin commented Nov 3, 2016

The service method get allows a params to be passed in, but currently just ignores it, as @daffl explained in here:

By default get just returns a record by its id, adding a query wouldn't really change anything about that.

But in fact $select in the query does change something. It would pluck the record and only returns the fields you need. This is particularly useful when you have a client-side cache of the record and want to see if the record is expired: you can just do:

service.get(id, {query: {$select: ['updatedAt']}}
.then (data) => {
  // check if data.updatedAt is newer than the local updatedAt 
} 

By doing this we could just transfer a small part of the record (only the id and the updatedAt time), and then be able to check if the cache is expired. If does, call get again to get the whole record; if not, just use the cache.

But currently, when calling get for the first time, it always return the whole record (might be very very large), making the client-side cache totally useless.

@daffl
Copy link
Member

daffl commented Nov 3, 2016

Indeed. There is several issues for this (feathersjs-ecosystem/feathers-service-tests#25 being the starting point). The current workaround for this is a three line hook:

const pick = require('lodash.pick');

app.service('myservice').after(function(hook) {
  if(hook.params.query.$select) {
    hook.result = pick(hook.result, hook.params.query.$select);
  }  
});

@beeplin
Copy link
Contributor Author

beeplin commented Nov 3, 2016

hmmm.. does lodash.pick support dot notation of fields?

I would go with this workaround for now, but to be frankly it's painful to add this hook to all services in an app. I understand that making this change to get is a huge work since all database adapters must be modified and tested. I am wondering if there are any schedules for this?

@daffl
Copy link
Member

daffl commented Nov 11, 2016

This will be released for all adapters shortly. But I just wanted to point out that it is now pretty easy to add a hook to all services with application hooks in feathers-hook v1.6.0.

@beeplin
Copy link
Contributor Author

beeplin commented Nov 11, 2016

cool~!

@daffl
Copy link
Member

daffl commented Nov 15, 2016

This has now been merged and released as a minor version in all official adapters.

@daffl daffl closed this as completed Nov 15, 2016
@lock
Copy link

lock bot commented Feb 7, 2019

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants