This post summarizes changes in Sequelize v1.2.1. It is highly recommended to read this post due to some changes in default values and interfaces. There are also some minor, but very cool new features.
In Sequelize v1.2.1 I’ve refactored the not so officially communicated possibility to define Sequelize-wide options, which are passed to the Models in multiple cases. I will explain the usage in the following, for now let’s see how it was before and how it has changed:
var sequelize = new Sequelize(database, user, password, {
queryOptions: {},
defineOptions: {/*
here you can pass options, which you want to be defined for all models.
e.g. underscored: true, which will turn all model usages into underscore usage.
look into the model definition options for more information
*/},
syncOptions: {/*
also not really senseful atm, but you could do this:
force: true —> this will
*/}
}
The new API is this way:
var sequelize = new Sequelize(database, user, password, {
query: {},
define: {},
sync: {}
}
And here is what you can do with it:
var sequelize = new Sequelize(database, user, password, {
queryOptions: { /* this has no further sense so far */ },
defineOptions: {/*
here you can pass options, which you want to be defined for all models.
e.g. underscored: true, which will turn all model usages into underscore usage.
look into the model definition options for more information
*/},
syncOptions: {/*
also not really senseful atm, but you could do this:
force: true —> this will always drop the table before creating it again, when calling sync
*/}
}
Here is more realistic use case:
// Instead of doing this…
var Person = sequelize.define(‘Person’, {/*attributes*/}, { underscored: true })
var Task = sequelize.define(‘Task’, {/*attributes*/}, { underscored: true })
// …you can do this:
var sequelize = new Sequelize(db, user, pw, { define: {underscored: true} })
var Person = sequelize.define(‘Person’, {/*attributes*/})
var Task = sequelize.define(‘Task’, {/*attributes*/})
Hope you like it :)
When setting up travis I found out that there were no way to define the charset of the tables. Well, you can now do this but I guess this is just the beginning. I basically needed this because travis is has UTF8 as default charset, which is OK but has broken all the tests. Please drop me a line if you are using UTF8 with sequelize, because I had no luck with it. I forced all tests to create latin1 charsets.
If you to specify a non-default-charset, here is how you can do it:
// charset = latin1
var sequelize = new Sequelize(db, user, pw, { define: {charset: ‘latin1’} })
// charset = utf8; collate = utf8_general_ci; I guess this will break everything :D
var sequelize = new Sequelize(db, user, pw, { define: {charset: ‘latin1’, collate: ‘utf8_general_ci’} }
Of course you can also define this for each model.
Someone requested InnoDB as default engine. Because I don’t mind, I just did so. If you want to change it, feel free to do so:
var sequelize = new Sequelize(db, user, pw, { define: {engine: ‘MYISAM’} })
Feel free to drop me a line with your thoughts about this.
I just added the possibility to search in the database for multiple specific values:
Model.findAll({ where: { id: [1,2,3] } }).on(‘success’, function(models) {
// models will contain all entries with id 1, 2 or 3
// the query will do this: SELECT * FROM Model WHERE id IN (1,2,3)
})
And that’s it :)