如何在bookshelf.js中保存多条记录。
如何在不使用循环和插入的情况下保存以下数据。
[
{name: 'Person1'},
{name: 'Person2'}
]
如何在bookshelf.js中保存多条记录。
如何在不使用循环和插入的情况下保存以下数据。
[
{name: 'Person1'},
{name: 'Person2'}
]
我认为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
});
我尝试使用forge并跟随invokeThen,但在我的情况下失败了。看起来我们可以使用模型本身,而无需创建单独的集合实例。这里,Employee是一个模型而不是单独的集合对象。
Employee.collection()
.add(employees)
.invokeThen('save');
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...
});
accounts.invokeThen('save').then(function() {
// ... all models in the collection have been saved
});
.insert([<items>])
。 - markconst payload = [{name: 'Person1'},
{name: 'Person2'}]
Users.collection(payload).invokeThen("save", null, { method: "insert" }).then(result => {
// further logics
})
希望这能有所帮助。
谢谢。