简体   繁体   English

用书架查询

[英]Querying with Bookshelf

I recently started using Bookshelf. 我最近开始使用书架。 Examples in the documentation aren't very clear to me. 我对文档中的示例并不十分清楚。 This is one such example: 这是一个这样的例子:

var knex = require('knex')({
  client: 'mysql',
  connection: process.env.MYSQL_DATABASE_CONNECTION
});
var bookshelf = require('bookshelf')(knex);

var User = bookshelf.Model.extend({
  tableName: 'users',
  posts: function() {
    return this.hasMany(Posts);
  }
});

var Posts = bookshelf.Model.extend({
  tableName: 'messages',
  tags: function() {
    return this.belongsToMany(Tag);
  }
});

var Tag = bookshelf.Model.extend({
  tableName: 'tags'
})

User.where('id', 1).fetch({
  withRelated: ['posts.tags']
}).then(function(user) {
  console.log(user.related('posts').toJSON());
}).catch(function(err) {
  console.error(err);
});

After the creation of three models (User, Posts, and Tag) - there is a query. 创建三个模型(用户,帖子和标签)后,将出现一个查询。

  1. What exactly is the meaning of this query? 此查询的确切含义是什么?

     User.where('id', 1).fetch({withRelated: ['posts.tags']}).then(function(user) { console.log(user.related('posts').toJSON()); }).catch(function(err) { console.error(err); }); 
  2. What are withRelated , 'posts.tags' , user.related('posts') ? 什么是withRelated'posts.tags'user.related('posts') Can anyone tell me in simplest form, what those terms are and where they come from? 谁能以最简单的形式告诉我,这些术语是什么以及它们来自何处?

  3. Finally, what is the meaning of the complete query? 最后,完整查询的含义是什么?

It's all answered in the documentation, although not everything in the exact same place, so I can understand your confusion. 文档中都回答了所有问题,尽管并非所有内容都完全相同,所以我可以理解您的困惑。 You really have to read the whole thing to better understand it. 您确实必须阅读整个内容才能更好地理解它。

From the documentation of Model.fetch() : Model.fetch()文档中

withRelated : Relations to be retrieved with Model instance . withRelated :要通过Model实例检索的关系。 Either one or more relation names (...) A single property, or an array of properties can be specified as a value for the withRelated property. 一个或多个关系名称(...)可以将一个属性或属性数组指定为withRelated属性的值。

From Collection.fetch() : Collection.fetch()

The withRelated option may be specified to fetch the models of the collection, eager loading any specified relations named on the model. 可以指定withRelated选项来获取集合的模型,以加载模型上命名的任何指定关系。

From Collection.load() : Collection.load()

... Nested eager loads can be specified by separating the nested relations with . 可以通过使用分隔嵌套关系来指定嵌套的紧急负载 .

From Model.related() : Model.related()

The related method returns a specified relation loaded on the relations hash on the model, or calls the associated relation method and adds it to the relations hash if one exists and has not yet been loaded. 相关方法返回在模型上的关系哈希中加载的指定关系,或者调用关联的关系方法,如果存在且尚未加载,则将其添加到关系哈希中。

All of these are involved in the eager loading of relations on models. 所有这些都涉及到模型上关系的迫切需求。

What these methods do is load some data from a table that is related to the parent model's table in some way. 这些方法的作用是以某种方式从与父模型的表相关的表中加载一些数据。 In the example you posted, the User model has some Posts and the Posts themselves have some tags, so the relations go something like: 在您发布的示例中,“用户”模型有一些“帖子”,而“帖子”本身也有一些标签,因此关系如下:

User
  |_ Post
       |_ Tag

These are expressed as methods on each model, for example, posts: function() { ... } in the User model. 这些表示为每个模型上的方法,例如,User模型中的posts: function() { ... }

When you specify a relation to eager load using withRelated , you use these method names. 当使用withRelated指定与withRelated加载的关系时,将使用这些方法名称。 Furthermore you can eager load deeply nested relations by separating them with a . 此外,您可以通过使用分隔它们来渴望加载深层嵌套的关系. , and that's what you're seeing in the example. ,这就是您在示例中看到的内容。

So, putting everything together what that example is doing is searching for the User with id = 1 and also retrieving all the posts that belong to that user as well as all the tags that belong to all the posts that belong to the user. 因此,将示例进行的所有操作组合在一起就是搜索id = 1的User,并检索属于该用户的所有帖子以及属于该用户的所有帖子的所有标签。

Then to access these related objects you use the related('something') method of the model. 然后,使用模型的related('something')方法访问这些相关对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM