在bookshelf.js中保存多个记录

问题描述 投票:3回答:2

如何在bookshelf.js中保存多个记录。

如何在不循环和插入的情况下保存以下数据

[
  {name: 'Person1'},
  {name: 'Person2'}
]
node.js bookshelf.js
2个回答
5
投票

我认为Bookshelf已经更新,用'invokeThen'替换'invoke'。

var Accounts = bookshelf.Collection.extend({
  model: Account
});
    
var accounts = Accounts.forge([
  {name: 'Person1'},
  {name: 'Person2'}
])	

accounts.invokeThen('save').then(function() {
  // ... all models in the collection have been saved
});

2
投票
var Promise = require('bluebird');
var Accounts = bookshelf.Collection.extend({
  model: Account
});

var accounts = Accounts.forge([
  {name: 'Person1'},
  {name: 'Person2'}
])

Promise.all(accounts.invoke('save')).then(function() {
  // collection models should now be saved...
});

Plz见注释:Invoke现在已被invokeThen取代

accounts.invokeThen('save').then(function() {
  // ... all models in the collection have been saved
});

0
投票

我尝试使用伪造跟随invokeThen,但它在我的情况下失败了。看来我们可以使用模型本身而无需创建单独的集合实例。这里,Employee是一个Model而不是一个单独的集合对象。

Employee.collection()
  .add(employees)
  .invokeThen('save');
© www.soinside.com 2019 - 2024. All rights reserved.