Creates a new SequelizeManager instance.
Configuration settings for the manager
A similar dummy variable that doesn't exist on the real object. Do not try to access this in real code.
A similar dummy variable that doesn't exist on the real object. Do not try to access this in real code.
A dummy variable that doesn't exist on the real object. This exists so Typescript can infer the type of the attributes in static functions. Don't try to access this!
An AND query
Each argument will be joined by AND
Creates a object representing a call to the cast function.
The value to cast
The type to cast it to
Creates a object representing a column in the DB. This is often useful in conjunction with
sequelize.fn, since raw string arguments to fn will be escaped.
The name of the column
ReadonlyconfigFinal config that is used by sequelize.
ReadonlyconnectionCreates a object representing a database function. This can be used in search queries, both in where and
order parts, and as default values in column definitions. If you want to refer to columns in your
function, you should use sequelize.col, so that the columns are properly interpreted as columns and
not a strings.
Convert a user's username to upper case
instance.update({
username: self.sequelize.fn('upper', self.sequelize.col('username'))
})
The function you want to call
All further arguments will be passed as arguments to the function
Creates an object representing nested where conditions for postgres's json data-type.
A hash containing strings/numbers or other nested hash, a string using dot notation or a string using postgres json syntax.
Optionalvalue: string | number | booleanAn optional value to compare against. Produces a string of the form "
Creates a object representing a literal, i.e. something that will not be escaped.
ReadonlymodelReadonlymodelsDictionary of all models linked with this instance.
ProtectednameThe name of this manager instance.
An OR query
Each argument will be joined by OR
A reference to Sequelize constructor from sequelize. Useful for accessing DataTypes, Errors etc.
A way of specifying "attr = condition".
Can be used as a replacement for the POJO syntax (e.g. where: { name: 'Lily' }) when you need to compare a column that the POJO syntax cannot represent.
The left side of the comparison.
The comparison operator to use. If unspecified, defaults to Op.eq.
The right side of the comparison. Its value depends on the used operator. See WhereOperators for information about what value is valid for each operator.
// Using an attribute as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(User.rawAttributes.firstName, Op.eq, 'Lily');
// Using a column name as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(col('first_name'), Op.eq, 'Lily');
A way of specifying "attr = condition".
Can be used as a replacement for the POJO syntax (e.g. where: { name: 'Lily' }) when you need to compare a column that the POJO syntax cannot represent.
The left side of the comparison.
The comparison operator to use. If unspecified, defaults to Op.eq.
The right side of the comparison. Its value depends on the used operator. See WhereOperators for information about what value is valid for each operator.
// Using an attribute as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(User.rawAttributes.firstName, Op.eq, 'Lily');
// Using a column name as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(col('first_name'), Op.eq, 'Lily');
A way of specifying "attr = condition".
Can be used as a replacement for the POJO syntax (e.g. where: { name: 'Lily' }) when you need to compare a column that the POJO syntax cannot represent.
The left side of the comparison.
The right side of the comparison. Its value depends on the used operator. See WhereOperators for information about what value is valid for each operator.
// Using an attribute as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(User.rawAttributes.firstName, Op.eq, 'Lily');
// Using a column name as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(col('first_name'), Op.eq, 'Lily');
StaticandAn AND query
An AND query
Each argument will be joined by AND
StaticcastCreates a object representing a call to the cast function.
Creates a object representing a call to the cast function.
The value to cast
The type to cast it to
StaticcolCreates a object representing a column in the DB. This is often useful in conjunction with
sequelize.fn, since raw string arguments to fn will be escaped.
Creates a object representing a column in the DB. This is often useful in conjunction with
sequelize.fn, since raw string arguments to fn will be escaped.
The name of the column
StaticfnCreates a object representing a database function. This can be used in search queries, both in where and
order parts, and as default values in column definitions. If you want to refer to columns in your
function, you should use sequelize.col, so that the columns are properly interpreted as columns and
not a strings.
Convert a user's username to upper case
instance.update({
username: self.sequelize.fn('upper', self.sequelize.col('username'))
})
Creates a object representing a database function. This can be used in search queries, both in where and
order parts, and as default values in column definitions. If you want to refer to columns in your
function, you should use sequelize.col, so that the columns are properly interpreted as columns and
not a strings.
Convert a user's username to upper case
instance.update({
username: self.sequelize.fn('upper', self.sequelize.col('username'))
})
The function you want to call
All further arguments will be passed as arguments to the function
StaticjsonCreates an object representing nested where conditions for postgres's json data-type.
Creates an object representing nested where conditions for postgres's json data-type.
A hash containing strings/numbers or other nested hash, a string using dot notation or a string using postgres json syntax.
Optionalvalue: string | number | booleanAn optional value to compare against. Produces a string of the form "
StaticliteralCreates a object representing a literal, i.e. something that will not be escaped.
Creates a object representing a literal, i.e. something that will not be escaped.
StaticorAn OR query
An OR query
Each argument will be joined by OR
Static ReadonlytypeIdentifier for the manager type.
StaticwhereA way of specifying attr = condition.
The attr can either be an object taken from Model.rawAttributes (for example Model.rawAttributes.id
or
Model.rawAttributes.name). The attribute should be defined in your model definition. The attribute can
also be an object from one of the sequelize utility functions (sequelize.fn, sequelize.col etc.)
For string attributes, use the regular { where: { attr: something }} syntax. If you don't want your
string to be escaped, use sequelize.literal.
A way of specifying "attr = condition".
Can be used as a replacement for the POJO syntax (e.g. where: { name: 'Lily' }) when you need to compare a column that the POJO syntax cannot represent.
The left side of the comparison.
The comparison operator to use. If unspecified, defaults to Op.eq.
The right side of the comparison. Its value depends on the used operator. See WhereOperators for information about what value is valid for each operator.
// Using an attribute as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(User.rawAttributes.firstName, Op.eq, 'Lily');
// Using a column name as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(col('first_name'), Op.eq, 'Lily');
A way of specifying "attr = condition".
Can be used as a replacement for the POJO syntax (e.g. where: { name: 'Lily' }) when you need to compare a column that the POJO syntax cannot represent.
The left side of the comparison.
The comparison operator to use. If unspecified, defaults to Op.eq.
The right side of the comparison. Its value depends on the used operator. See WhereOperators for information about what value is valid for each operator.
// Using an attribute as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(User.rawAttributes.firstName, Op.eq, 'Lily');
// Using a column name as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(col('first_name'), Op.eq, 'Lily');
A way of specifying "attr = condition".
Can be used as a replacement for the POJO syntax (e.g. where: { name: 'Lily' }) when you need to compare a column that the POJO syntax cannot represent.
The left side of the comparison.
The right side of the comparison. Its value depends on the used operator. See WhereOperators for information about what value is valid for each operator.
// Using an attribute as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(User.rawAttributes.firstName, Op.eq, 'Lily');
// Using a column name as the left operand.
// Equal to: WHERE first_name = 'Lily'
where(col('first_name'), Op.eq, 'Lily');
Add a hook to the model
A hook that is run after creating instances in bulk
A callback function that is called with instances, options
A hook that is run after creating instances in bulk
A callback function that is called with instances, options
A hook that is run after destroying instances in bulk
A callback function that is called with options
A hook that is run after destroying instances in bulk
A callback function that is called with options
A hook that is run after sequelize.sync call
A callback function that is called with options passed to sequelize.sync
A hook that is run after sequelize.sync call
A callback function that is called with options passed to sequelize.sync
A hook that is run after updating instances in bulk
A callback function that is called with options
A hook that is run after updating instances in bulk
A callback function that is called with options
A hook that is run after creating a single instance
A callback function that is called with attributes, options
A hook that is run after creating a single instance
A callback function that is called with attributes, options
A hook that is run after a define call
A callback function that is called with factory
A hook that is run after a define call
A callback function that is called with factory
A hook that is run after destroying a single instance
A callback function that is called with instance, options
A hook that is run after destroying a single instance
A callback function that is called with instance, options
A hook that is run after a find (select) query
A callback function that is called with instance(s), options
A hook that is run after a find (select) query
A callback function that is called with instance(s), options
A hook that is run after Sequelize() call
A callback function that is called with sequelize
A hook that is run after Sequelize() call
A callback function that is called with sequelize
A hook that is run after Model.sync call
A callback function that is called with options passed to Model.sync
A hook that is run after Model.sync call
A callback function that is called with options passed to Model.sync
A hook that is run after updating a single instance
A callback function that is called with instance, options
A hook that is run after updating a single instance
A callback function that is called with instance, options
A hook that is run after validation
A callback function that is called with instance, options
A hook that is run after validation
A callback function that is called with instance, options
Test the connection by trying to authenticate
Optionaloptions: QueryOptionsQuery Options for authentication
A hook that is run before creating instances in bulk
A callback function that is called with instances, options
A hook that is run before creating instances in bulk
A callback function that is called with instances, options
A hook that is run before destroying instances in bulk
A callback function that is called with options
A hook that is run before destroying instances in bulk
A callback function that is called with options
A hook that is run before sequelize.sync call
A callback function that is called with options passed to sequelize.sync
A hook that is run before sequelize.sync call
A callback function that is called with options passed to sequelize.sync
A hook that is run after updating instances in bulk
A callback function that is called with options
A hook that is run after updating instances in bulk
A callback function that is called with options
A hook that is run before creating a single instance
A callback function that is called with attributes, options
A hook that is run before creating a single instance
A callback function that is called with attributes, options
A hook that is run before a define call
A callback function that is called with attributes, options
A hook that is run before a define call
A callback function that is called with attributes, options
A hook that is run before destroying a single instance
A callback function that is called with instance, options
A hook that is run before destroying a single instance
A callback function that is called with instance, options
A hook that is run before a find (select) query
A callback function that is called with options
A hook that is run before a find (select) query
A callback function that is called with options
A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded
A callback function that is called with options
A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded
A callback function that is called with options
A hook that is run before a find (select) query, after all option parsing is complete
A callback function that is called with options
A hook that is run before a find (select) query, after all option parsing is complete
A callback function that is called with options
A hook that is run before Sequelize() call
A callback function that is called with config, options
A hook that is run before Sequelize() call
A callback function that is called with config, options
A hook that is run before Model.sync call
A callback function that is called with options passed to Model.sync
A hook that is run before Model.sync call
A callback function that is called with options passed to Model.sync
A hook that is run before updating a single instance
A callback function that is called with instance, options
A hook that is run before updating a single instance
A callback function that is called with instance, options
A hook that is run before validation
A callback function that is called with instance, options
A hook that is run before validation
A callback function that is called with instance, options
Close 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.
Create a new database schema.
Note,that this is a schema in the postgres sense of the word, not a database table. In mysql and sqlite, this command will do nothing.
Name of the schema
Options supplied
Returns the database version
Define a new model, representing a table in the DB.
The table columns are defined by the hash that is given as the second argument. Each attribute of the hash represents a column. A short table definition might look like this:
class MyModel extends Model {}
MyModel.init({
columnA: {
type: Sequelize.BOOLEAN,
validate: {
is: ["[a-z]",'i'], // will only allow letters
max: 23, // only allow values <= 23
isIn: {
args: [['en', 'zh']],
msg: "Must be English or Chinese"
}
},
field: 'column_a'
// Other attributes here
},
columnB: Sequelize.STRING,
columnC: 'MY VERY OWN COLUMN TYPE'
}, { sequelize })
sequelize.models.modelName // The model will now be available in models under the name given to define
As shown above, column definitions can be either strings, a reference to one of the datatypes that are predefined on the Sequelize constructor, or an object that allows you to specify both the type of the column, and other attributes such as default values, foreign key constraints and custom setters and getters.
For a list of possible data types, see https://sequelize.org/master/en/latest/docs/models-definition/#data-types
For more about getters and setters, see https://sequelize.org/master/en/latest/docs/models-definition/#getters-setters
For more about instance and class methods, see https://sequelize.org/master/en/latest/docs/models-definition/#expansion-of-models
For more about validation, see https://sequelize.org/master/en/latest/docs/models-definition/#validations
The name of the model. The model will be stored in sequelize.models under this name
An object, where each attribute is a column of the table. Each column can be either a DataType, a string or a type-description object, with the properties described below:
Optionaloptions: ModelOptions<M>These options are merged with the default define options provided to the Sequelize constructor
Drop all tables defined through this sequelize instance. This is done by calling Model.drop on each model
Optionaloptions: DropOptionsThe options passed to each call to Model.drop
Drop all schemas
Note,that this is a schema in the postgres sense of the word, not a database table. In mysql and sqlite, this is the equivalent of drop all tables.
Options supplied
Drop a single schema
Note,that this is a schema in the postgres sense of the word, not a database table. In mysql and sqlite, this drop a table matching the schema name
Name of the schema
Options supplied
Escape value.
Value that needs to be escaped
Returns the database name.
Returns the specified dialect.
Fetches a model that has already been defined in this Sequelize instance.
The ModelStatic type, defaults to ModelStatic
The name of the model to retrieve
The requested model class
Fetches a model that has already been defined in this Sequelize instance.
The Model type, defaults to Model
The name of the model to retrieve
The requested model class
Fetches a model that has already been defined in this Sequelize instance.
The model attributes type
The creation attributes type
The name of the model to retrieve
The requested model class
Returns an instance of QueryInterface.
Performs a comprehensive health check on the Sequelize connection. Tests connectivity by authenticating with the database and gathering connection information.
A promise that resolves to a detailed health check result including:
const health = await manager.healthCheck();
if (health.healthy) {
console.log(`Sequelize is healthy. Latency: ${health.details.latency}`);
console.log(`Database: ${health.details.dialect} v${health.details.databaseVersion}`);
console.log(`Models: ${health.details.models?.join(', ')}`);
} else {
console.error(`Sequelize is unhealthy: ${health.message}`);
}
Checks if the Sequelize connection is active and can authenticate with the database.
A promise that resolves to true if authentication succeeds, false otherwise
This method tests the connection by running the authenticate() method, which verifies that the database credentials are correct and the database is accessible. For a more comprehensive check with additional metrics, use healthCheck.
Checks whether a model with the given name is defined
The name of a model defined with Sequelize.define
Fetch a Model which is already defined
The name of a model defined with Sequelize.define
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Query options
Execute a query on the DB, optionally bypassing all the Sequelize goodness.
By default, the function will return two arguments: an array of results, and a metadata object,
containing number of affected rows etc. Use const [results, meta] = await ... to access the results.
If you are running a type of query where you don't need the metadata, for example a SELECT query, you
can pass in a query type to make sequelize format the results:
const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring
const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring
Optionaloptions: QueryOptions | QueryOptionsWithType<RAW>Query options
Get the fn for random based on the dialect
Execute a query which would set an environment or user variable. The variables are set per connection, so this function needs a transaction.
Only works for MySQL.
object with multiple variables.
Query options.
Show all defined schemas
Note,that this is a schema in the postgres sense of the word, not a database table. In mysql and sqlite, this will show all tables.
Options supplied
Sync all defined models to the DB.
Optionaloptions: SyncOptionsSync Options
Start a transaction. When using transactions, you should pass the transaction in the options argument in order for the query to happen under that transaction
try {
const transaction = await sequelize.transaction();
const user = await User.findOne(..., { transaction });
await user.update(..., { transaction });
await transaction.commit();
} catch(err) {
await transaction.rollback();
}
})
A syntax for automatically committing or rolling back based on the promise chain resolution is also supported:
try {
await sequelize.transaction(transaction => { // Note that we pass a callback rather than awaiting the call with no arguments
const user = await User.findOne(..., {transaction});
await user.update(..., {transaction});
});
// Committed
} catch(err) {
// Rolled back
console.error(err);
}
If you have CLS enabled, the transaction will automatically be passed to any query that runs witin the callback. To enable CLS, add it do your project, create a namespace and set it on the sequelize constructor:
const cls = require('cls-hooked');
const namespace = cls.createNamespace('....');
const Sequelize = require('sequelize');
Sequelize.useCLS(namespace);
Note, that CLS is enabled for all sequelize instances, and all instances will share the same namespace
Transaction Options
Callback for the transaction
Start a transaction. When using transactions, you should pass the transaction in the options argument in order for the query to happen under that transaction
try {
const transaction = await sequelize.transaction();
const user = await User.findOne(..., { transaction });
await user.update(..., { transaction });
await transaction.commit();
} catch(err) {
await transaction.rollback();
}
})
A syntax for automatically committing or rolling back based on the promise chain resolution is also supported:
try {
await sequelize.transaction(transaction => { // Note that we pass a callback rather than awaiting the call with no arguments
const user = await User.findOne(..., {transaction});
await user.update(..., {transaction});
});
// Committed
} catch(err) {
// Rolled back
console.error(err);
}
If you have CLS enabled, the transaction will automatically be passed to any query that runs witin the callback. To enable CLS, add it do your project, create a namespace and set it on the sequelize constructor:
const cls = require('cls-hooked');
const namespace = cls.createNamespace('....');
const Sequelize = require('sequelize');
Sequelize.useCLS(namespace);
Note, that CLS is enabled for all sequelize instances, and all instances will share the same namespace
Callback for the transaction
Start a transaction. When using transactions, you should pass the transaction in the options argument in order for the query to happen under that transaction
try {
const transaction = await sequelize.transaction();
const user = await User.findOne(..., { transaction });
await user.update(..., { transaction });
await transaction.commit();
} catch(err) {
await transaction.rollback();
}
})
A syntax for automatically committing or rolling back based on the promise chain resolution is also supported:
try {
await sequelize.transaction(transaction => { // Note that we pass a callback rather than awaiting the call with no arguments
const user = await User.findOne(..., {transaction});
await user.update(..., {transaction});
});
// Committed
} catch(err) {
// Rolled back
console.error(err);
}
If you have CLS enabled, the transaction will automatically be passed to any query that runs witin the callback. To enable CLS, add it do your project, create a namespace and set it on the sequelize constructor:
const cls = require('cls-hooked');
const namespace = cls.createNamespace('....');
const Sequelize = require('sequelize');
Sequelize.useCLS(namespace);
Note, that CLS is enabled for all sequelize instances, and all instances will share the same namespace
Optionaloptions: TransactionOptionsTransaction Options
Truncate all tables defined through the sequelize models. This is done by calling Model.truncate() on each model.
Optionaloptions: DestroyOptions<any>The options passed to Model.destroy in addition to truncate
Optionaloptions: QueryOptionsStaticaddStaticafterA hook that is run after creating instances in bulk
A callback function that is called with instances, options
A hook that is run after creating instances in bulk
A callback function that is called with instances, options
StaticafterA hook that is run after destroying instances in bulk
A callback function that is called with options
A hook that is run after destroying instances in bulk
A callback function that is called with options
StaticafterA hook that is run after sequelize.sync call
A callback function that is called with options passed to sequelize.sync
A hook that is run after sequelize.sync call
A callback function that is called with options passed to sequelize.sync
StaticafterA hook that is run after updating instances in bulk
A callback function that is called with options
A hook that is run after updating instances in bulk
A callback function that is called with options
StaticafterA hook that is run after a connection is established
A callback function that is called with options
A hook that is run after a connection is established
A callback function that is called with options
StaticafterA hook that is run after creating a single instance
A callback function that is called with attributes, options
A hook that is run after creating a single instance
A callback function that is called with attributes, options
StaticafterA hook that is run after a define call
A callback function that is called with factory
A hook that is run after a define call
A callback function that is called with factory
StaticafterA hook that is run after destroying a single instance
A callback function that is called with instance, options
A hook that is run after destroying a single instance
A callback function that is called with instance, options
StaticafterA hook that is run after a connection is released
A callback function that is called with options
A hook that is run after a connection is released
A callback function that is called with options
StaticafterA hook that is run after a find (select) query
A callback function that is called with instance(s), options
A hook that is run after a find (select) query
A callback function that is called with instance(s), options
StaticafterA hook that is run after Sequelize() call
A callback function that is called with sequelize
A hook that is run after Sequelize() call
A callback function that is called with sequelize
StaticafterA hook that is run after successfully acquiring a connection from the pool
A callback function that is called with options
A hook that is run after successfully acquiring a connection from the pool
A callback function that is called with options
StaticafterA hook that is run after creating or updating a single instance, It proxies afterCreate and afterUpdate
A callback function that is called with instance, options
A hook that is run after creating or updating a single instance, It proxies afterCreate and afterUpdate
A callback function that is called with instance, options
StaticafterA hook that is run after Model.sync call
A callback function that is called with options passed to Model.sync
A hook that is run after Model.sync call
A callback function that is called with options passed to Model.sync
StaticafterA hook that is run after updating a single instance
A callback function that is called with instance, options
A hook that is run after updating a single instance
A callback function that is called with instance, options
StaticafterA hook that is run after validation
A callback function that is called with instance, options
A hook that is run after validation
A callback function that is called with instance, options
StaticbeforeA hook that is run before creating instances in bulk
A callback function that is called with instances, options
A hook that is run before creating instances in bulk
A callback function that is called with instances, options
StaticbeforeA hook that is run before destroying instances in bulk
A callback function that is called with options
A hook that is run before destroying instances in bulk
A callback function that is called with options
StaticbeforeA hook that is run before sequelize.sync call
A callback function that is called with options passed to sequelize.sync
A hook that is run before sequelize.sync call
A callback function that is called with options passed to sequelize.sync
StaticbeforeA hook that is run after updating instances in bulk
A callback function that is called with options
A hook that is run after updating instances in bulk
A callback function that is called with options
StaticbeforeA hook that is run before a connection is established
A callback function that is called with options
A hook that is run before a connection is established
A callback function that is called with options
StaticbeforeA hook that is run before creating a single instance
A callback function that is called with attributes, options
A hook that is run before creating a single instance
A callback function that is called with attributes, options
StaticbeforeStaticbeforeA hook that is run before destroying a single instance
A callback function that is called with instance, options
A hook that is run before destroying a single instance
A callback function that is called with instance, options
StaticbeforeA hook that is run before a connection is released
A callback function that is called with options
A hook that is run before a connection is released
A callback function that is called with options
StaticbeforeA hook that is run before a find (select) query
A callback function that is called with options
A hook that is run before a find (select) query
A callback function that is called with options
StaticbeforeA hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded
A callback function that is called with options
A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded
A callback function that is called with options
StaticbeforeA hook that is run before a find (select) query, after all option parsing is complete
A callback function that is called with options
A hook that is run before a find (select) query, after all option parsing is complete
A callback function that is called with options
StaticbeforeA hook that is run before Sequelize() call
A callback function that is called with config, options
A hook that is run before Sequelize() call
A callback function that is called with config, options
StaticbeforeA hook that is run before attempting to acquire a connection from the pool
A callback function that is called with options
A hook that is run before attempting to acquire a connection from the pool
A callback function that is called with options
StaticbeforeA hook that is run before creating or updating a single instance, It proxies beforeCreate and beforeUpdate
A callback function that is called with instance, options
A hook that is run before creating or updating a single instance, It proxies beforeCreate and beforeUpdate
A callback function that is called with instance, options
StaticbeforeA hook that is run before Model.sync call
A callback function that is called with options passed to Model.sync
A hook that is run before Model.sync call
A callback function that is called with options passed to Model.sync
StaticbeforeA hook that is run before updating a single instance
A callback function that is called with instance, options
A hook that is run before updating a single instance
A callback function that is called with instance, options
StaticbeforeA hook that is run before validation
A callback function that is called with instance, options
A hook that is run before validation
A callback function that is called with instance, options
StatichasStatichasStaticremoveStaticuseUse CLS with Sequelize.
CLS namespace provided is stored as Sequelize._cls
and Promise is patched to use the namespace, using cls-hooked module.
Manager class for Sequelize ORM integration with Storehouse. Provides connection management, model registration, and health checking for SQL databases supported by Sequelize (PostgreSQL, MySQL, SQLite, MariaDB, SQL Server, etc.).
This manager extends the Sequelize class, offering a unified interface for working with relational databases through the Storehouse registry system.
Implements
Example