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

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

问题描述

以下是关于sequelize支持的功能的一些问题(续订项目网站),我想在清除之前决定是否或不使用它:


  1. 链接(效率):当链接多个查询时,这些收集到一个请求到数据库


  2. 链接(成功/错误):当链接多个查询时,何时是成功事件发射和错误发生了什么?只有所有操作成功,才会发出成功?


  3. 过滤关联:说一个 Crowd 对象具有 Crowd.hasMany(Person)的关系。你可以通过执行 crowd.getPersons()来获得所有相关的人,但可以选择它们的一个子集,例如 crowd.getPersons {where:{age:30}})


  4. 通过两个或更多步骤获取相关联的对象:说 对象作为关系 Crowd.hasMany(Person) Person 具有 Person.hasMany(Pet)的关系。可以通过执行 crowd.getPersons()。getPets()来获得人群中的所有宠物,如果是这样,则会作为多个请求发送


  5. Deep对象:我想定义一个人作为对象:

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

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


  6. 计算对象:向从对象的其他字段计算的对象添加字段?例如:

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

    因此 name.full 字段isn



解决方案

1:您可以使用超级批量处理QueryChainer。然而,每个命令将被分开执行。



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



3:Hmm我不是100%肯定,但imho它不支持。 p>

4:不可能,但更复杂,更不喜欢:

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

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



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

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


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. 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?

  2. 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)

  3. 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 }})?

  4. 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?

  5. "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)

  6. "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
        }
    });
    

    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.: You can use for super duper batch processing the QueryChainer. Nevertheless, every command will be executed separated.

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.: Hmm I'm not 100% sure, but imho it's not yet supported.

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.: Nope. But I also don't understand why you would do that.

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天全站免登陆