NodeJS 的续集:是否支持这些功能? [英] sequelize for NodeJS: are these features supported?

查看:18
本文介绍了NodeJS 的续集:是否支持这些功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一些关于 sequelize (sequelize project site) 支持的功能的问题,我想在决定是否或不使用它:

Here are some questions about features supported by sequelize (sequelize project site) that I would like to clear up before deciding whether or not to use it:

  1. 链接(效率):链接多个查询时,这些查询是收集到一个对数据库的请求中(作为一批操作),还是每个单独发送?

  1. Chaining (efficiency): when chaining multiple queries, are these collected into one request to the database (as a batch of operations), or is each one sent separately?

链接(成功/错误):链接多个查询时,何时发出成功事件以及发生错误时会发生什么?只有当 all 操作成功时才​​会发出成功"?如果出现错误,是否会回滚所有操作(即是否将链式操作视为事务)

Chaining (success/error): when chaining multiple queries, when is the success event emitted and what happens on error? Is "success" emitted only if all operations succeeded? And if there was an error, does it rollback all operations (i.e. are the chained operations treated as a transaction)

过滤关联:假设 Crowd 对象具有关系 Crowd.hasMany(Person).您可以通过执行 crowd.getPersons() 来获取所有相关人员,但是否可以选择其中的一个子集,例如 crowd.getPersons({where: { age: 30 }})?

Filtering associations: Say a Crowd object has the relation Crowd.hasMany(Person). You can get all the associated people by executing crowd.getPersons(), but is it possible to select a subset of them, like crowd.getPersons({where: { age: 30 }})?

获取通过两个或多个步骤关联的关联对象:将 Crowd 对象称为关系 Crowd.hasMany(Person)Person 有关系 Person.hasMany(Pet).是否有可能通过执行类似 crowd.getPersons().getPets() 之类的方法来获取人群中的所有宠物,如果是这样,它会作为多个请求发送到数据库,或者只是一个请求?

Getting associated objects that are related by two or more steps: Say a Crowd object as the relation Crowd.hasMany(Person) and Person has the relation Person.hasMany(Pet). Is it possible to get all the pets of people in a crowd by executing something like crowd.getPersons().getPets(), and if so does this get sent as multiple request to the database, or just one request?

深"对象:我想定义一个人为对象:

"Deep" object: I want to define a person as the object:

sequelize.define('Person', {
    name: {
        first: <a string>,
        last: <a string>
    }
});

这是允许的吗?(请注意,名称不会是数据库表的列,而是第一个和最后一个)

Is this allowed? (Note that name is not going to be a column of the database table, but first and last will be)

计算"对象:是否可以在对象的其他字段中添加字段?例如:

"Calculated" object: Is it possible to add a field to the object that is calculated from other fields of the object? For example:

sequelize.define('Person', {
    name: {
        first: <a string>,
        last: <a string>,
        full: <name.first + ' ' + name.last> // <-- this field
    }
});

所以 name.full 字段实际上并没有存储在数据库中(这很浪费空间),而只是从其他两个计算出来的?

So that the name.full field isn't actually stored in the database (which is a waste of space) but rather just calculated from the other two?

推荐答案

1.:可以使用QueryChainer进行超级duper批处理.尽管如此,每个命令都会分开执行.

1.: You can use for super duper batch processing the QueryChainer. Nevertheless, every command will be executed separated.

2.:使用QueryChainer,只有在一切正常的情况下才会触发成功事件.如果发生一个或多个错误,则触发错误.绑定方法的第一个参数将是一个错误数组.

2.: Using the QueryChainer, the success event will only be triggered if everything was fine. Error is triggered if there occured one or multiple errors. The first param of the bound method will be an array of errors.

3.:嗯,我不是 100% 确定,但恕我直言,它还不支持.

3.: Hmm I'm not 100% sure, but imho it's not yet supported.

4.: 不,不可能,但更复杂,更不花哨:

4.: Nope not possible but more complicated and less fancy with:

crowd.getPeople().success(function(people) {
  people.forEach(function(person){
    person.getPets().success... // you have to collect them on your own
  })
})

5.:不.但我也不明白你为什么要这么做.

5.: Nope. But I also don't understand why you would do that.

6.: 是的,看看这个 http://sequelizejs.com/docs/1.7.8/models#expansion-of-models 和:

6.: Yep, check this http://sequelizejs.com/docs/1.7.8/models#expansion-of-models and:

Person = sequelize.define('Person', {foo:Sequelize.STRING}, {
  instanceMethods: {
    fullname: function() {
      return this.firstName + ' ' + this.lastName
    }
  }
})

这篇关于NodeJS 的续集:是否支持这些功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆