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:
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?
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)
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 }})
?
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
}
});
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
}
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With