[英]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.