Getting started

    Setting up a connection

    Sequelize will setup a connection pool on initialization so you should ideally only ever create one instance per database if you're connecting to the DB from a single process. If you're connecting to the DB from multiple processes, you'll have to create one instance per process, but each instance should have a maximum connection pool size of "max connection pool size divided by number of instances". So, if you wanted a max connection pool size of 90 and you had 3 worker processes, each process's instance should have a max connection pool size of 30.

    1. const sequelize = new Sequelize('database', 'username', 'password', {
    2. host: 'localhost',
    3. dialect: 'mysql'|'sqlite'|'postgres'|'mssql',
    4. operatorsAliases: false,
    5. pool: {
    6. max: 5,
    7. min: 0,
    8. acquire: 30000,
    9. },
    10. // SQLite only
    11. storage: 'path/to/database.sqlite'
    12. });
    13. // Or you can simply use a connection uri
    14. const sequelize = new Sequelize('postgres://user:pass@example.com:5432/dbname');

    The Sequelize constructor takes a whole slew of options that are available via the .

    You can use the .authenticate() function like this to test the connection.

    1. sequelize
    2. .authenticate()
    3. .then(() => {
    4. console.log('Connection has been established successfully.');
    5. })
    6. .catch(err => {
    7. console.error('Unable to connect to the database:', err);
    8. });

    Your first model

    Models are defined with sequelize.define('name', {attributes}, {options}).

    1. User.findAll().then(users => {
    2. })

    You can read more about finder functions on models like .findAll() at or how to do specific queries like WHERE and JSONB at Querying.

    The Sequelize constructor takes a define option which will be used as the default options for all defined models.

    1. define: {
    2. timestamps: false // true by default
    3. }
    4. });
    5. const User = sequelize.define('user', {}); // timestamps is false by default
    6. const Post = sequelize.define('post', {}, {
    7. timestamps: true // timestamps will now be true
    8. });

    Promises

    Sequelize uses Bluebird promises to control async control-flow.

    Note:Sequelize use independent copy of Bluebird instance. You can access it using Sequelize.Promise if you want to set any Bluebird specific options

    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:

    1. User.findOne().then(user => {
    2. console.log(user.get('firstName'));
    3. });

    When your environment or transpiler supports this will work but only in the body of an async function:

    1. user = await User.findOne()

    Once you've got the hang of what promises are and how they work, use the as your go-to tool. In particular, you'll probably be using .all a lot.