Feathers: Sorting and selecting with API in objects

Created on 25 Mar 2016  路  3Comments  路  Source: feathersjs/feathers

Hi!

I am using feathersjs for my system as API. When my users do api call like: api.my.site/v1/items?token=BPGhLf9D9FEM?title=Hello

It seaches in database db.items.find({title: 'Hello'});

But my db design is bit different, TITLE is stored in data object

It should run this mongo query: db.items.find({'data.title': 'Hello'});

How can I make feathers search in my data object? Is there way to override the feathers where and sort queries?

Many thanks!

Question

All 3 comments

Hi @perminder-klair. You can modify the request data with Hooks before they are sent to the database. In your case like this:

app.service('items').before({
  find(hook) {
    const oldQuery = hook.params.query;
    const newQuery = {};
    Object.keys(oldQuery).forEach(key => {
      newQuery[`data.${key}`] = oldQuery[key];
    });

    hook.params.query = newQuery;
  }
});

I also answered this in Slack, so I think we can close.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andysay picture andysay  路  3Comments

arkenstan picture arkenstan  路  3Comments

Vincz picture Vincz  路  4Comments

huytran0605 picture huytran0605  路  3Comments

jordanbtucker picture jordanbtucker  路  4Comments