简体   繁体   English

如何使用书架根据API网址中查询字符串中传递的参数过滤数据?

[英]How to use bookshelf for filtering the data based on parameters passed in query string in API url?

I have created the REST API in Express using Bookshelf which fetches the list of all contributors as JSON 我已经使用书架在Express中创建了REST API,该书架将所有贡献者的列表作为JSON获取

Say API : 说API:

GET example.com/contributors

I need to make the filter and pagination for the API, The filter options can be any 我需要为API进行过滤和分页,过滤器选项可以是任何

GET example.com/contributors?status=1&type=pro

So it will return all the data whose status =1 and type = 'pro', 因此它将返回状态为= 1且类型='pro'的所有数据,

I am able to get this filtered data using where clause 我可以使用where子句获取此过滤数据

Model.Contributors.forge()
.query({where: {status: query_params.status, type: query_params.type}})
.fetch()

What I am looking ? 我在看什么?

But here the 'where' key values have to be specified manually. 但是在这里,“ where”键值必须手动指定。 As these filters are not always fixed but depends upon the condition client want to put based on the field the data need to be filtered, how can this structure be implemented in express node api using bookshelf? 由于这些过滤器并不总是固定的,而是取决于客户端基于字段要放置的条件而需要过滤数据,因此如何使用书架在Express Node api中实现此结构?

or 要么

Is this right approach for REST API to get the filter data or some other way it need to be implemented as per REST API best practices? REST API是否需要此正确方法来获取过滤器数据,或者是否需要按照REST API最佳实践来实现它的其他某种方式?

Code

routes/contributors.js 路线/ contributors.js

var Contributor_Service = require('./../services/contributor-service');

var express = require('express');
var router = express.Router();

    /* GET all the contributors listing. */
    router.get('/', function (req, res, next) {
        Contributor_Service.listAllContributors(req.query, function (err, data) {
            if (err) {
                res.status(500).json({error: true, data: {message: err.message}});
            } else {
                res.json({error: false, data: data.toJSON()});
            }
        });

    });

module.exports = router;

services/contributor-service.js 服务/撰稿service.js

var Model = require('./../models/Contributor');
var express = require('express');

var listAllContributors = function ( query_params, callback) {

    // for (var x in query_params) {
       // console.log(query_params[x]);
       // console.log(x);
    //}
    Model.Contributors
        .forge()
         .query({where: {status: query_params.status, type: query_params.type}})
        .fetch()
        .then(function (collection) {
            return callback(null, collection);
        })
        .catch(function (err) {
            return callback(err, null);
        });
};

models/Contributor.js 车型/ Contributor.js

var Bookshelf = require('../../dbconfig').bookshelf;

var Contributor = Bookshelf.Model.extend({
    tableName: 'users'
});
var Contributors = Bookshelf.Collection.extend({
    model: Contributor
});

module.exports = {
    Contributor: Contributor,
    Contributors: Contributors
};

I have used a where object but still not sure if this is right way to write the REST API filter 我使用了where对象,但仍不确定这是否是编写REST API过滤器的正确方法

as there can be many other query parameter for sorting the data, offset and limit, which need to be checked and implemented in controller based on condition and fields passed in query. 因为可能有许多其他查询参数可用于对数据,偏移量和限制进行排序,因此需要根据查询中传递的条件和字段在控制器中检查和实现这些参数。

services/contributor-service.js 服务/撰稿service.js

var Model = require('./../models/Contributor');
var express = require('express');

var listAllContributors = function ( query_params, callback) {

var whereObj = {};

    for (var x in query_params) {
        whereObj[x] =  query_params[x];

    Model.Contributors
        .forge()
        .query({where: whereObj})
        .fetch()
        .then(function (collection) {
            return callback(null, collection);
        })
        .catch(function (err) {
            return callback(err, null);
        });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我很困惑是否应该在后端使用 URL 路径参数或查询参数进行过滤? - I'm confused whether I should use URL path parameters or query parameters for filtering on the back-end? Bookshelf.JS |如何在查询中使用where和orWhere - Bookshelf.JS | How to use where and orWhere in query 根据传递的url参数单击一个表单按钮 - click a form button based on the url parameters passed 如何通过云开发套件 (CDK) 在 API 网关上为“集成请求”定义“URL 查询字符串参数” - How to define “URL Query String Parameters” for “Integration Request” on API Gateway via Cloud Development Kit (CDK) 如何使查询在书架上为空 - how make query is null on bookshelf 基于 URL/查询参数的重定向 - Redirect based on URL/Query Parameters 获取在javascript文件中传递的查询字符串参数 - Fetching query string parameters passed in a javascript file 如何检索在asp文件的url中传递的参数 - how to retrieve the parameters that are passed in the url of a asp file 如何为嵌套的REST API构建查询URL参数? - How to build a query url parameters for the nested REST API? 如何基于Url中的查询字符串以编程方式更改Url - How to programmatically change the Url based on a query string in Url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM