AbstractInstantiates sequelize.
The options to connect to the database are specific to your dialect. Please refer to the documentation of your dialect on https://sequelize.org to learn about the options you can use.
import { PostgresDialect } from '@sequelize/postgres';
// with database, username, and password in the options object
const sequelize = new Sequelize({ database, user, password, dialect: PostgresDialect });
// with url
import { MySqlDialect } from '@sequelize/mysql';
const sequelize = new Sequelize({
dialect: MySqlDialect,
url: 'mysql://localhost:3306/database',
})
// option examples
import { MsSqlDialect } from '@sequelize/mssql';
const sequelize = new Sequelize('database', 'username', 'password', {
// the dialect of the database
// It is a Dialect class exported from the dialect package
dialect: MsSqlDialect,
// custom host;
host: 'my.server.tld',
// for postgres, you can also specify an absolute path to a directory
// containing a UNIX socket to connect over
// host: '/sockets/psql_sockets'.
// custom port;
port: 12345,
// disable logging or provide a custom logging function; default: console.log
logging: false,
// This option is specific to MySQL and MariaDB
socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock',
// the storage engine for sqlite
// - default ':memory:'
storage: 'path/to/database.sqlite',
// disable inserting undefined values as NULL
// - default: false
omitNull: true,
// A flag that defines if connection should be over ssl or not
// Dialect-dependent, check the dialect documentation
ssl: true,
// Specify options, which are used when sequelize.define is called.
// The following example:
// define: { timestamps: false }
// is basically the same as:
// Model.init(attributes, { timestamps: false });
// sequelize.define(name, attributes, { timestamps: false });
// so defining the timestamps for each model will be not necessary
define: {
underscored: false,
freezeTableName: false,
charset: 'utf8',
collate: 'utf8_general_ci'
timestamps: true
},
// similar for sync: you can define this to always force sync for models
sync: { force: true },
// pool configuration used to pool database connections
pool: {
max: 5,
idle: 30000,
acquire: 60000,
},
// isolation level of each transaction
// defaults to dialect default
isolationLevel: IsolationLevel.REPEATABLE_READ
})
ReadonlydialectReadonlymodelsReadonlyoptionsReadonlypoolReadonlyrawThe options that were used to create this Sequelize instance. These are an unmodified copy of the options passed to the constructor. They are not normalized or validated.
Mostly available for cloning the Sequelize instance. For other uses, we recommend using options instead.
StaticaddStaticafterStaticbeforeStatichasStatichasStaticremoveStaticrunThe QueryGenerator instance, dialect dependant.
The QueryInterface instance, dialect dependant.
StatichooksClose all connections used by this sequelize instance, and free all references so the instance can be garbage collected.
Normally this is done on process exit, so you only need to call this method if you are creating multiple instances, and want to garbage collect some of them.
Alias of AbstractQueryInterface#createSchema
Name of the schema
Optionaloptions: CreateSchemaOptionsA slower alternative to truncate that uses DELETE FROM instead of TRUNCATE, but which works with foreign key constraints in dialects that don't support TRUNCATE CASCADE (postgres), or temporarily disabling foreign key constraints (mysql, mariadb, sqlite).
Optionaloptions: Omit<DestroyOptions<any>, "truncate" | "where" | "limit">Optionaloptions: QiDropAllSchemasOptionsAlias of AbstractQueryInterface#dropSchema
Optionaloptions: QueryRawOptionsEscape value to be used in raw SQL.
If you are using this to use the value in a sql.literal, consider using sql instead, which automatically escapes interpolated values.
The value to escape
Optionaloptions: EscapeOptionsOptionaloptions: QueryRawOptionsReturns the transaction that is associated to the current asynchronous operation. This method returns undefined if no transaction is active in the current asynchronous operation, or if the Sequelize "disableClsTransactions" option is true.
Throws if the database version hasn't been loaded yet. It is automatically loaded the first time Sequelize connects to your database.
You can use Sequelize#authenticate to cause a first connection.
current version of the dialect that is internally loaded
Optionaloptions: QiListSchemasOptionsUse AbstractQueryInterface#listSchemas instead
We highly recommend using Sequelize#transaction instead. If you really want to use the manual solution, don't forget to commit or rollback your transaction once you are done with it.
Transactions started by this method are not automatically passed to queries. You must pass the transaction object manually, even if the Sequelize "disableClsTransactions" option is false.
Optionaloptions: TransactionOptionsStart a managed transaction: Sequelize will create a transaction, pass it to your callback, and commit it once the promise returned by your callback resolved, or execute a rollback if the promise rejects.
try {
await sequelize.transaction(() => {
const user = await User.findOne(...);
await user.update(...);
});
// By now, the transaction has been committed
} catch {
// If the transaction callback threw an error, the transaction has been rolled back
}
By default, Sequelize uses AsyncLocalStorage to automatically pass the transaction to all queries executed inside the callback (unless you already pass one or set the transaction option to null).
This can be disabled by setting the Sequelize "disableClsTransactions" option to true. You will then need to pass transactions to your queries manually.
const sequelize = new Sequelize({
// ...
disableClsTransactions: true,
})
await sequelize.transaction(transaction => {
// transactions are not automatically passed around anymore, you need to do it yourself:
const user = await User.findOne(..., { transaction });
await user.update(..., { transaction });
});
If you want to manage your transaction yourself, use startUnmanagedTransaction.
Async callback during which the transaction will be active
Transaction Options
Async callback during which the transaction will be active
Truncate all models registered in this instance. This is done by calling Model.truncate on each model.
Optionaloptions: SequelizeTruncateOptionsThe options passed to Model.truncate, plus "withoutForeignKeyChecks".
Validate a value against a field specification
The value to validate
The DataType to validate against
This is a temporary class used to progressively migrate the Sequelize class to TypeScript by slowly moving its functions here. Always use Sequelize instead.