简体   繁体   中英

Designing of a RESTful API endpoint filter and search

I am in the process of developing some custom API endpoints (using loopback.io), on top of its existing CRUD endpoints.

In the past I've used some other Node RESTful API frameworks for prototyping, and really enjoyed the powerful filtering features they provide out of the box.

What I'd like to do is provide a similar set (or subset) of some kind of filtering for a custom endpoint. The endpoint just does a SQL query (with some JOINs) and returns an array of objects.

Is there any kind of standardized approach that I should use to design some filtering? For example, I may want to filter on fields of the base table, or filter on relations. I like the way loopback.io and sequelize allow relatively easy specification of includes to link related objects, as well as their filtering syntax.

How is this type of problem usually approached when a custom implementation is done?

As you probably noticed with CRUD endpoints, LoopBack provides querying out of the box via filter parameter. You can nicely experiment with it in API Explorer . If you want to expose querying for a custom remote method , just add filter as a parameter too.

example-model.js

module.exports = ExampleModel => {
  const search = async (filter = {}) => {
    return await ExampleModel.find(filter)
  }

  ExampleModel.remoteMethod('search', {
    description: 'Returns a set of ExampleModel based on provided query.',
    accepts: [
      {arg: 'filter', type: 'object', required: false}
    ],
    http: {path: '/search', verb: 'get'},
    returns: {root: true}
  })

  ExampleModel.search = search
}

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