如何与书架和 knex 中的 join table 交互?

How to interact with a join table in bookshelf and knex?

我想使用书架在连接 table 中创建一个条目。我按照书架网站 here 上的说明进行操作。 (使用书架进行模型实例化,使用 knex 创建 tables)。

有没有办法在创建新书后向 authors_books table 添加值而不创建 authors_books model/collection?

我应该添加什么:

//Books references the Books collection that holds the Book models

var book = new Book({
      title: title,
      base_url: req.headers.origin
    });

    book.save().then(function(newBook) {
      Books.add(newLink);
      res.send(200, newBook);
    });

当然可以,很简单。

您需要两个模型,Book 和 Author。两者都定义了 belongsToMany 关系,并且还定义了 withPivotwithPivot 列出联结 table 中的属性,请在此处查看更多信息:http://bookshelfjs.org/#Model-withPivot

之后,您调用 updatePivot,仅更新 author_book table 中的属性。