使用MassiveJS进行REST放置/发布保存

时间:2016-07-11 17:06:45

标签: javascript rest massivejs

使用当前版本的MassiveJS和express for API调用。使用SAVE函数时,Massive需要更新列名列表,如下所示:

TextView.becomeFirstResponder

正如您所看到的,随着列列表越来越长,此代码变得越来越难以维护。所以我想知道是否有一种方法可以在一次调用中保存整个req.body,假设req.body键值与db列名称匹配。这样可以节省很多时间,而且可以维护得更多。

1 个答案:

答案 0 :(得分:2)

Massive isn't an ORM, so saving an "object" isn't the idea. If you want to update something you can do so directly using db.update and passing in the values you want updated as well as the id of the row. This will do a partial update for you.

As I mention in the comments, opening up a REST endpoint to update whatever a user sends in via POST is probably not a good idea, even if you do trust your user.

Finally: if you want to just pass along the form post you can:

router.put('/:id', function(req, res, next) {
  var supplier = {
    id: req.params.id;
  };
  supplier = _.extend(supplier, req.params.body);
  db.suppliers.save(
    supplier
    , function (err, result) {
      if (err) {
        return next(err);
      }
      return res.status(200).json({
        status: 'SUCCESS',
        message: 'Supplier has been saved'
      });
    })
});