简体   繁体   中英

Feathers: Customizing find() for two different queries

I'm using FeathersJS and ReactJs to build an application and now I am stuck in a certain point.

The user interface I'm working on is a table and I have a navigation bar to control the data exhibited, like in the image.

我的用户界面

In the select users may choose how many results they are going to see in a page. The navigation buttons move among different pages. Every time one of these thing change I calculate

let skip = (this.state.page - 1) * this.state.limit;
let url = '/users?$sort[name]=1&$skip=' + skip + '&$limit=' + this.state.limit;

and then make a REST call to Feathers backend. This is working perfectly well.

The red arrow indicates my problem.

When the user types part of a name in this input field I need to search the Users table to find all users with the typed string in the name, whatever the position and preferably case insensitive. In this case I am creating an url like

let url = '/users?name=<typed input'>;

and then making the REST call.

It happens that this call will try to find a name equal to, and this is not what I need. Then I am assuming I'll have to customize my find() method to perform differently when it receives the two different sets of parameters, like in

users.class.js

const { Service } = require('feathers-sequelize');

exports.Users = class Users extends Service {
    find(data,params) {
        if (data.query.name) {
            // Do something to get the search I need
        }
        return super.find(data,params);
    }
};

But I really don't know to proceed from this point on.

Should I implement a Sequelize raw query inside this if ? What would be the best way to get the data I need?

This isn't really a Feathers question, or even a Sequelize question, but a database function. It depends on what your database is, For MySQL. you can use the LIKE command to search for a substring, Google how to make it case insensitive. if need: For the Sequelize query I believe something like this would work:

query = {
    name: { $like: '%searchterm%'}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM