Basic usage

    This will save the passed database credentials and provide all further methods.

    Furthermore you can specify a non-default host/port:

    1. dialect: 'mysql',
    2. host: "my.server.tld",
    3. port: 9821,
    4. })

    If you just don't have a password:

    1. const sequelize = new Sequelize({
    2. database: 'db_name',
    3. username: 'username',
    4. password: null,
    5. dialect: 'mysql'
    6. });

    You can also use a connection string:

    1. const sequelize = new Sequelize('mysql://user:pass@example.com:9821/db_name', {
    2. // Look to the next section for possible options
    3. })

    Besides the host and the port, Sequelize comes with a whole bunch of options. Here they are:

    Hint: You can also define a custom function for the logging part. Just pass a function. The first parameter will be the string that is logged.

    Sequelize supports read replication, i.e. having multiple servers that you can connect to when you want to do a SELECT query. When you do read replication, you specify one or more servers to act as read replicas, and one server to act as the write master, which handles all writes and updates and propagates them to the replicas (note that the actual replication process is not handled by Sequelize, but should be set up by database backend).

    1. const sequelize = new Sequelize('database', null, null, {
    2. dialect: 'mysql',
    3. port: 3306
    4. replication: {
    5. read: [
    6. { host: '8.8.8.8', username: 'read-username', password: 'some-password' },
    7. { host: '9.9.9.9', username: 'another-username', password: null }
    8. ],
    9. write: { host: '1.1.1.1', username: 'write-username', password: 'any-password' }
    10. },
    11. pool: { // If you want to override the options used for the read/write pool you can do so here
    12. max: 20,
    13. idle: 30000
    14. },

    Sequelize uses a pool to manage connections to your replicas. Internally Sequelize will maintain two pools created using pool configuration.

    If you want to modify these, you can pass pool as an options when instantiating Sequelize, as shown above.

    Each write or useMaster: true query will use write pool. For SELECT read pool will be used. Read replica are switched using a basic round robin scheduling.

    With the release of Sequelize 1.6.0, the library got independent from specific dialects. This means, that you'll have to add the respective connector library to your project yourself.

    In order to get Sequelize working nicely together with MySQL, you'll need to installmysql2@^1.0.0-rc.10or higher. Once that's done you can use it like this:

    1. const sequelize = new Sequelize('database', 'username', 'password', {
    2. dialect: 'mysql'
    3. })

    Note: You can pass options directly to dialect library by setting thedialectOptions parameter. See for examples (currently only mysql is supported).

    For SQLite compatibility you'll need. Configure Sequelize like this:

    1. const sequelize = new Sequelize('database', 'username', 'password', {
    2. // sqlite! now!
    3. dialect: 'sqlite',
    4. // the storage engine for sqlite
    5. // - default ':memory:'
    6. storage: 'path/to/database.sqlite'
    7. })

    The library for PostgreSQL ispg@^5.0.0 || ^6.0.0 You'll just need to define the dialect:

    1. const sequelize = new Sequelize('database', 'username', 'password', {
    2. // gimme postgres, please!
    3. dialect: 'postgres'
    4. })

    The library for MSSQL istedious@^1.7.0 You'll just need to define the dialect:

    1. const sequelize = new Sequelize('database', 'username', 'password', {
    2. dialect: 'mssql'
    3. })

    As there are often use cases in which it is just easier to execute raw / already prepared SQL queries, you can utilize the function sequelize.query.

    Here is how it works:

    1. // Arguments for raw queries
    2. sequelize.query('your query', [, options])
    3. // Quick example
    4. sequelize.query("SELECT * FROM myTable").then(myTableRows => {
    5. console.log(myTableRows)
    6. })
    7. // If you want to return sequelize instances use the model options.
    8. // This allows you to easily map a query to a predefined model for sequelize e.g:
    9. sequelize
    10. .query('SELECT * FROM projects', { model: Projects })
    11. .then(projects => {
    12. // Each record will now be mapped to the project's model.
    13. console.log(projects)
    14. })
    15. // Options is an object with the following keys:
    16. .query('SELECT 1', {
    17. // Will get called for every SQL query that gets send
    18. // to the server.
    19. logging: console.log,
    20. // If plain is true, then sequelize will only return the first
    21. // record of the result set. In case of false it will all records.
    22. plain: false,
    23. // Set this to true if you don't have a model definition for your query.
    24. raw: false,
    25. // The type of query you are executing. The query type affects how results are formatted before they are passed back.
    26. type: Sequelize.QueryTypes.SELECT
    27. })
    28. // Note the second argument being null!
    29. // Even if we declared a callee here, the raw: true would
    30. // supersede and return a raw object.
    31. sequelize
    32. .query('SELECT * FROM projects', { raw: true })
    33. .then(projects => {
    34. console.log(projects)
    35. })

    Replacements in a query can be done in two different ways, either usingnamed parameters (starting with :), or unnamed, represented by a ?

    The syntax used depends on the replacements option passed to the function:

    • If an array is passed, ? will be replaced in the order that they appear in the array
    • If an object is passed, :key will be replaced with the keys from that object.If the object contains keys not found in the query or vice versa, an exceptionwill be thrown.

    One note: If the attribute names of the table contain dots, the resulting objects will be nested:

    1. sequelize.query('select 1 as `foo.bar.baz`').then(rows => {
    2. console.log(JSON.stringify(rows))
    3. /*
    4. [{
    5. "foo": {
    6. "bar": {
    7. "baz": 1
    8. }
    9. }
    10. }]
    11. */