如何从BookshelfJS集合更新所有结果模型?

时间:2017-05-10 19:30:24

标签: bookshelf.js

例如,有一个属性为sold_out的产品列表,我想更新该集合中每个项目的字段。

在此特定示例中,我们假设我想将字段sold_out = false设置为该字段设置为true的所有项目:

Product.where({sold_out: true})
.fetchAll()
.then(soldOutCollection => {
  return Promise.all(product => {
    return product.save({sold_out: false})
  })
})

这样可行,但它会在集合中的每个项目上触发一次查询。

有没有办法一次更新所有项目(只触发一个查询)?

PS:我试图避免直接使用knex.js

1 个答案:

答案 0 :(得分:4)

我认为如果你想以这种方式更新,你可能想尝试这个:

Product
.where({sold_out: true})
.save(
    {sold_out: false}, 
    {method: 'update', patch: true}
 )
相关问题