Sequelize will setup a connection pool on initialization so you should ideally only ever create one instance per database.
host: 'localhost',
dialect: 'mysql'|'mariadb'|'sqlite'|'postgres'|'mssql',
pool: {
max: 5,
idle: 10000
},
// SQLite only
});
// Or you can simply use a connection uri
var sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname');
The Sequelize constructor takes a whole slew of options that are available via the .
Many more options can be found in the Model API reference
The Sequelize constructor takes a define
option which will be used as the default options for all defined models.
var sequelize = new Sequelize('connectionUri', {
timestamps: false // true by default
}
var User = sequelize.define('user', {}); // timestamps is false by default
var Post = sequelize.define('post', {}, {
timestamps: true // timestamps will now be true
});
Basically a promise represents a value which will be present at some point - "I promise you I will give you a result or an error at some point". This means that
will never work! This is because user
is a promise object, not a data row from the DB. The right way to do it is:
User.findOne().then(function (user) {
console.log(user.get('firstName'));