Dialect-Specific Things

Underlying Connector Libraries

MySQL

The underlying connector library used by Sequelize for MySQL is the mysql2 npm package (version 1.5.2 or higher).

You can provide custom options to it using the dialectOptions in the Sequelize constructor:

  1. const sequelize = new Sequelize('database', 'username', 'password', {
  2. dialect: 'mysql',
  3. dialectOptions: {
  4. // Your mysql2 options here
  5. }
  6. })

MariaDB

The underlying connector library used by Sequelize for MariaDB is the mariadb npm package.

You can provide custom options to it using the dialectOptions in the Sequelize constructor:

  1. const sequelize = new Sequelize('database', 'username', 'password', {
  2. dialect: 'mariadb',
  3. dialectOptions: {
  4. // Your mariadb options here
  5. // connectTimeout: 1000
  6. }
  7. });

SQLite

The underlying connector library used by Sequelize for SQLite is the sqlite3 npm package (version 4.0.0 or above).

You specify the storage file in the Sequelize constructor with the storage option (use :memory: for an in-memory SQLite instance).

You can provide custom options to it using the dialectOptions in the Sequelize constructor:

  1. const sequelize = new Sequelize('database', 'username', 'password', {
  2. dialect: 'sqlite',
  3. storage: 'path/to/database.sqlite' // or ':memory:'
  4. dialectOptions: {
  5. // Your sqlite3 options here
  6. }
  7. });

PostgreSQL

The underlying connector library used by Sequelize for PostgreSQL is the pg npm package (version 7.0.0 or above). The module pg-hstore is also necessary.

You can provide custom options to it using the dialectOptions in the Sequelize constructor:

  1. const sequelize = new Sequelize('database', 'username', 'password', {
  2. dialect: 'postgres',
  3. dialectOptions: {
  4. // Your pg options here
  5. }
  6. });

To connect over a unix domain socket, specify the path to the socket directory in the host option. The socket path must start with /.

  1. const sequelize = new Sequelize('database', 'username', 'password', {
  2. dialect: 'postgres',
  3. host: '/path/to/socket_directory'
  4. });

MSSQL

The underlying connector library used by Sequelize for MSSQL is the tedious npm package (version 6.0.0 or above).

You can provide custom options to it using dialectOptions.options in the Sequelize constructor:

  1. const sequelize = new Sequelize('database', 'username', 'password', {
  2. dialect: 'postgres',
  3. dialectOptions: {
  4. // Observe the need for this nested `options` field for MSSQL
  5. options: {
  6. // Your tedious options here
  7. useUTC: false,
  8. dateFirst: 1
  9. }
  10. }
  11. });

MSSQL Domain Account

In order to connect with a domain account, use the following format.

  1. const sequelize = new Sequelize('database', null, null, {
  2. dialect: 'mssql',
  3. dialectOptions: {
  4. authentication: {
  5. type: 'ntlm',
  6. options: {
  7. domain: 'yourDomain',
  8. userName: 'username',
  9. password: 'password'
  10. }
  11. },
  12. options: {
  13. instanceName: 'SQLEXPRESS'
  14. }
  15. }
  16. })

Data type: TIMESTAMP WITHOUT TIME ZONE - PostgreSQL only

If you are working with the PostgreSQL TIMESTAMP WITHOUT TIME ZONE and you need to parse it to a different timezone, please use the pg library’s own parser:

  1. require('pg').types.setTypeParser(1114, stringValue => {
  2. return new Date(stringValue + '+0000');
  3. // e.g., UTC offset. Use any offset that you would like.
  4. });

Data type: ARRAY(ENUM) - PostgreSQL only

Array(Enum) type requireS special treatment. Whenever Sequelize will talk to the database, it has to typecast array values with ENUM name.

So this enum name must follow this pattern enum_<table_name>_<col_name>. If you are using sync then correct name will automatically be generated.

Table Hints - MSSQL only

The tableHint option can be used to define a table hint. The hint must be a value from TableHints and should only be used when absolutely necessary. Only a single table hint is currently supported per query.

Table hints override the default behavior of MSSQL query optimizer by specifing certain options. They only affect the table or view referenced in that clause.

  1. const { TableHints } = require('sequelize');
  2. Project.findAll({
  3. // adding the table hint NOLOCK
  4. tableHint: TableHints.NOLOCK
  5. // this will generate the SQL 'WITH (NOLOCK)'
  6. })

Index Hints - MySQL/MariaDB only

The indexHints option can be used to define index hints. The hint type must be a value from IndexHints and the values should reference existing indexes.

Index hints override the default behavior of the MySQL query optimizer.

  1. const { IndexHints } = require("sequelize");
  2. Project.findAll({
  3. indexHints: [
  4. { type: IndexHints.USE, values: ['index_project_on_name'] }
  5. ],
  6. where: {
  7. id: {
  8. [Op.gt]: 623
  9. },
  10. name: {
  11. [Op.like]: 'Foo %'
  12. }
  13. }
  14. });

The above will generate a MySQL query that looks like this:

  1. SELECT * FROM Project USE INDEX (index_project_on_name) WHERE name LIKE 'FOO %' AND id > 623;

Sequelize.IndexHints includes USE, FORCE, and IGNORE.

See Issue #9421 for the original API proposal.

Engines - MySQL/MariaDB only

The default engine for a model is InnoDB.

You can change the engine for a model with the engine option (e.g., to MyISAM):

  1. const Person = sequelize.define('person', { /* attributes */ }, {
  2. engine: 'MYISAM'
  3. });

Like every option for the definition of a model, this setting can also be changed globally with the define option of the Sequelize constructor:

  1. const sequelize = new Sequelize(db, user, pw, {
  2. define: { engine: 'MYISAM' }
  3. })

Table comments - MySQL/MariaDB/PostgreSQL only

You can specify a comment for a table when defining the model:

  1. class Person extends Model {}
  2. Person.init({ /* attributes */ }, {
  3. comment: "I'm a table comment!",
  4. sequelize
  5. })

The comment will be set when calling sync().