在 bookshelf.js 中保存多条记录
saving multiple records in bookshelf.js
如何在 bookshelf.js.
中保存多条记录
我们如何在不循环插入的情况下保存下面的数据
[
{name: 'Person1'},
{name: 'Person2'}
]
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...
});
请参阅评论:Invoke 现在已被 invokeThen 取代
accounts.invokeThen('save').then(function() {
// ... all models in the collection have been saved
});
我认为书架已更新,将 'invoke' 替换为 'invokeThen'。
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,但在我的情况下失败了。看来我们可以使用模型本身而不必创建一个单独的集合instance.Here,Employee 是一个模型而不是一个单独的集合对象。
Employee.collection()
.add(employees)
.invokeThen('save');
使用下面给出的 invokeThen 方法中的第三个参数来执行插入方法。
const payload = [{name: 'Person1'},
{name: 'Person2'}]
Users.collection(payload).invokeThen("save", null, { method: "insert" }).then(result => {
// further logics
})
希望对您有所帮助。
谢谢
如何在 bookshelf.js.
中保存多条记录我们如何在不循环插入的情况下保存下面的数据
[
{name: 'Person1'},
{name: 'Person2'}
]
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...
});
请参阅评论:Invoke 现在已被 invokeThen 取代
accounts.invokeThen('save').then(function() {
// ... all models in the collection have been saved
});
我认为书架已更新,将 'invoke' 替换为 'invokeThen'。
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,但在我的情况下失败了。看来我们可以使用模型本身而不必创建一个单独的集合instance.Here,Employee 是一个模型而不是一个单独的集合对象。
Employee.collection()
.add(employees)
.invokeThen('save');
使用下面给出的 invokeThen 方法中的第三个参数来执行插入方法。
const payload = [{name: 'Person1'},
{name: 'Person2'}]
Users.collection(payload).invokeThen("save", null, { method: "insert" }).then(result => {
// further logics
})
希望对您有所帮助。
谢谢