节点 Bookshelf.js 如何检查字段是否存在于 table

Node Bookshelf.js how to check if a field exist on table

我需要验证 http 请求的所有查询参数,knex 或 bookshelf 有做这样事情的功能吗?

这是我的代码示例:

var validateModelQuery = function(Model , query) {
for(var att in query) {
    if(!Model.has(att)) //does not exist
        return false;
}
return true;
};

/

router.get('/customers', function(req, res, next) {
if (!validateModelQuery(Customer , req.query)) {
    res.status(400);
Customer.where(req.query).fetchAll()
......

你们有什么建议来验证请求参数或主体,因为在每个路由上手动编写所有字段确实是一种糟糕的编码。

knex 提供 hasColumn 函数,可用于检查列是否已存在。