A reference to the deferrable collection. Use this to access the different deferrable options.
A reference to the sequelize instance class.
A Model represents a table in the database. Sometimes you might also see it referred to as model, or simply as factory. This class should not be instantiated directly, it is created using sequelize.define, and already created models can be loaded using sequelize.import
A modified version of bluebird promises, that allows listening for sql events
Available query types for use with sequelize.query
A reference to Sequelize constructor from sequelize. Useful for accessing DataTypes, Errors etc.
A reference to the sequelize transaction class. Use this to access isolationLevels when creating a transaction
A reference to sequelize utilities. Most users will not need to use these utils directly. However, you
might want to use Sequelize.Utils._
, which is a reference to the lodash library, if you don't already
have it imported in your project.
Exposes the validator.js object, so you can extend it with custom validation functions. The validator is exposed both on the instance, and on the constructor.
Defined models.
Add a hook to the model
Provide a name for the hook function. It can be used to remove the hook later or to order hooks based on some sort of priority system in the future.
The hook function
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 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 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 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
An AND query
Each argument will be joined by AND
Test the connection by trying to authenticate
Query 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 destroying 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 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 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 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 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
Creates a object representing a call to the cast function.
The value to cast
The type to cast it to
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.
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
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
A function that logs sql queries, or false for no logging
Returns the database version
Define a new model, representing a table in the DB.
The table columns are define 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:
sequelize.define('modelName', {
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.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 http://docs.sequelizejs.com/en/latest/docs/models-definition/#data-types
For more about getters and setters, see http://docs.sequelizejs.com/en/latest/docs/models-definition/#getters-setters
For more about instance and class methods, see http://docs.sequelizejs.com/en/latest/docs/models-definition/#expansion-of-models
For more about validation, see http://docs.sequelizejs.com/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:
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
The 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
A function that logs sql queries, or false for no logging
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
A function that logs sql queries, or false for no logging
Escape value.
Value that needs to be escaped
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.updateAttributes({
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
Returns the specified dialect.
Returns an instance of QueryInterface.
Check whether the mode has any hooks of this type
Imports a model defined in another file
Imported models are cached, so multiple calls to import with the same path will not load the file multiple times
See https://github.com/sequelize/sequelize/blob/master/examples/using-multiple-model-files/Task.js for a short example of how to define your models in separate files so that they can be imported by sequelize.import
The path to the file that holds the model you want to import. If the part is relative, it will be resolved relatively to the calling file
An optional function that provides model definitions. Useful if you do not want to use the module root as the define function
Checks whether a model with the given name is defined
The name of a model defined with Sequelize.define
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.
An 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.
Fetch a Model which is already defined
The name of a model defined with Sequelize.define
An OR query
Each argument will be joined by OR
Execute a query on the DB, with the posibility to bypass 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 .spread
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:
sequelize.query('SELECT...').spread(function (results, metadata) {
// Raw query - use spread
});
sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }).then(function (results) {
// SELECT query - use then
})
Query options
Remove hook from the model
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
A function that logs sql queries, or false for no logging
Sync all defined models to the DB.
Sync 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
sequelize.transaction().then(function (t) {
return User.find(..., { transaction: t}).then(function (user) {
return user.updateAttributes(..., { transaction: t});
})
.then(t.commit.bind(t))
.catch(t.rollback.bind(t));
})
A syntax for automatically committing or rolling back based on the promise chain resolution is also supported:
sequelize.transaction(function (t) { // Note that we use a callback rather than a promise.then()
return User.find(..., { transaction: t}).then(function (user) {
return user.updateAttributes(..., { transaction: t});
});
}).then(function () {
// Commited
}).catch(function (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:
var cls = require('continuation-local-storage'),
ns = cls.createNamespace('....');
var Sequelize = require('sequelize');
Sequelize.cls = ns;
Note, that CLS is enabled for all sequelize instances, and all instances will share the same namespace
Transaction Options
Callback for the transaction
Truncate all tables defined through the sequelize models. This is done by calling Model.truncate() on each model.
A 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
.
The attribute, which can be either an attribute object from Model.rawAttributes
or a
sequelize object, for example an instance of sequelize.fn
. For simple string attributes, use the
POJO syntax
Comparator
The condition. Can be both a simply type, or a further condition (.or
, .and
, .literal
etc.)
Generated using TypeDoc
This is the main class, the entry point to sequelize. To use it, you just need to import sequelize:
var Sequelize = require('sequelize');
In addition to sequelize, the connection library for the dialect you want to use should also be installed in your project. You don't need to import it however, as sequelize will take care of that.