kalamata icon indicating copy to clipboard operation
kalamata copied to clipboard

How to fetch only certain columns?

Open hbzhang opened this issue 10 years ago • 2 comments

Thanks for this good tool. Wonder how to only fetch certain columns? I have tried the following:

api.beforeGetPeople(execPeopleQuery);

function execPeopleQuery(req, res, model) { model.fetch({columns: ['id','name']}); }

But the results still returns all the columns.

Thanks!

hbzhang avatar Sep 22 '15 16:09 hbzhang

@hbzhang I think this would work:

api.afterGetPeople(execPeopleQuery);

function execPeopleQuery(req, res, model) {
  res.send({
    id: model.get('id'),
    name: model.get('name')
  });
}

you would have to use the afterGetPeople hook in this case

not an ideal solution though. It might be better to add support for this using query parameters, something like /people/123?include=id,name

mikec avatar Oct 09 '15 21:10 mikec

This could be useful to process every model on every route. Imagine everytime all you want is to send the id and name, and hide every other fields.

Maybe another hook to process models before they are sent would be a nice addition. What do you think?

mintyPT avatar Mar 03 '17 07:03 mintyPT