mikec / kalamata

Extensible REST API for Express + Bookshelf.js
MIT License
41 stars 12 forks source link

How to fetch only certain columns? #1

Open hbzhang opened 8 years ago

hbzhang commented 8 years ago

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!

mikec commented 8 years ago

@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

mintyPT commented 7 years ago

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?