gpt4 book ai didi

javascript - 如何在 Bookshelf.js 中进行嵌套插入

转载 作者:数据小太阳 更新时间:2023-10-29 04:44:06 24 4
gpt4 key购买 nike

如何将这样的对象插入到 Book 和 Page 两个表中

var book = {
name: 'Hello',
author: 'World',
pages: [{
pagetitle: 'intro',
book: 8
}, {
pagetitle: 'chaptessr1',
book: 8
}]
};

最佳答案

我认为您可能正在寻找某种捷径,但认为没有捷径:

var Promise = require('bluebird');
var Book = bookshelf.Model.extend({
tableName: 'books'
});
var Page = bookshelf.Model.extend({
tableName: 'pages'
});
var Pages = bookshelf.Collection.extend({
model: Page
});

Book.forge({name: 'Hello', author: 'World'}).save()
.then(function(book) {
var pages = Pages.forge([
{pagetitle: 'intro', book: book.id},
{pagetitle: 'chatessr1', book: book.id}
]);

return pages.invokeThen('save', null);
}).then(function(){
// now all the pages and the book should be saved
});

关于javascript - 如何在 Bookshelf.js 中进行嵌套插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25884867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com