javascript - 如何使用 bookshelf 根据 API url 中的查询字符串中传递的参数过滤数据?

标签 javascript node.js rest express bookshelf.js

我使用 Bookshelf 在 Express 中创建了 REST API,它以 JSON 形式获取所有贡献者的列表

说出API:

GET example.com/contributors

我需要为API制作过滤器和分页, 过滤器选项可以是任意的

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

所以它会返回所有status=1且type='pro'的数据,

我可以使用 where 子句获取过滤后的数据

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

我在看什么?

但是这里必须手动指定“where”键值。 由于这些过滤器并不总是固定的,而是取决于客户端想要根据需要过滤数据的字段设置的条件,那么如何使用 bookshelf 在 Express Node api 中实现此结构?

这是 REST API 获取过滤器数据的正确方法还是需要根据 REST API 最佳实践实现的其他方式?

代码

routes/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

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

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
};

最佳答案

我已经使用了 where 对象,但仍然不确定这是否是编写 REST API 过滤器的正确方法

因为可以有许多其他查询参数用于对数据进行排序,偏移量和限制,需要根据查询中传递的条件和字段在 Controller 中检查和实现。

services/contributor-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);
        });
};

关于javascript - 如何使用 bookshelf 根据 API url 中的查询字符串中传递的参数过滤数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40564355/

相关文章:

javascript - 在 Javascript 中使用 Regex 删除 BBCode

node.js - 查找未邀请任何用户的用户

javascript - 使用管道从异步等待 https 请求构建文件

node.js - 字符串列表的变异变量 "$_v0_data"得到无效值 Graphql Node.js

javascript - 使用 javascript 打开下载的 zip 文件时出现问题

api - 如何防止对 RESTful 数据服务的蛮力攻击

javascript - 更新javascript确认框样式

javascript - 为什么 Google 跟踪代码管理器不在某些页面上包含标签?

javascript - 从鼠标点击捕获 X Y 坐标 (javascript)

node.js - 无法在nodejs中请求文件夹