Sequelize
The main class
Members:
- Sequelize
- Sequelize
- Utils
- QueryTypes
- connectorManager
- Transaction
- getDialect
- getQueryInterface
- getMigrator
- define
- model
- isDefined
- import
- query
- query
- createSchema
- showAllSchemas
- dropSchema
- dropAllSchemas
- sync
- authenticate
- fn
- col
- cast
- literal
- asIs
- and
- or
- transaction
new Sequelize(database, [username=null], [password=null], [options={}])
Instantiate sequelize with name of database, username and password
Example usage
// without password and options
var sequelize = new Sequelize('database', 'username')
// without options
var sequelize = new Sequelize('database', 'username', 'password')
// without password / with blank password
var sequelize = new Sequelize('database', 'username', null, {})
// with password and options
var sequelize = new Sequelize('my_database', 'john', 'doe', {})
// with uri (see below)
var sequelize = new Sequelize('mysql://localhost:3306/database', {})
Params:
| Name | Type | Description |
|---|---|---|
| database | String | The name of the database |
| [username=null] | String | The username which is used to authenticate against the database. |
| [password=null] | String | The password which is used to authenticate against the database. |
| [options={}] | Object | An object with options. |
| [options.dialect='mysql'] | String | The dialect of the relational database. |
| [options.dialectModulePath=null] | String | If specified, load the dialect library from this path. |
| [options.host='localhost'] | String | The host of the relational database. |
| [options.port=] | Integer | The port of the relational database. |
| [options.protocol='tcp'] | String | The protocol of the relational database. |
| [options.define={}] | Object | Options, which shall be default for every model definition. See sequelize#define for options |
| [options.query={}] | Object | I have absolutely no idea. |
| [options.sync={}] | Object | Options, which shall be default for every `sync` call. |
| [options.logging=console.log] | Function | A function that gets executed everytime Sequelize would log something. |
| [options.omitNull=false] | Boolean | A flag that defines if null values should be passed to SQL queries or not. |
| [options.queue=true] | Boolean | I have absolutely no idea. |
| [options.native=false] | Boolean | A flag that defines if native library shall be used or not. |
| [options.replication=false] | Boolean | I have absolutely no idea. |
| [options.pool={}] | Object | Something. |
| [options.quoteIdentifiers=true] | Boolean | Set to `false` to make table names and attributes case-insensitive on Postgres and skip double quoting of them. |
new Sequelize(uri, [options={}])
Instantiate sequlize with an URI
Params:
| Name | Type | Description |
|---|---|---|
| uri | String | A full database URI |
| [options={}] | object | See above for possible options |
Utils
A reference to Utils
See: Utils
QueryTypes
An object of different query types. This is used when doing raw queries (sequlize.query). If no type is provided to .query, sequelize will try to guess the correct type based on your SQL. This might not always work if you query is formatted in a special way
See: QueryTypes
connectorManager
Direct access to the sequelize connectorManager
See: ConnectorManager
Transaction
A reference to transaction. Use this to access isolationLevels when creating a transaction
See: Transaction
getDialect()
Returns the specified dialect.
Return:
- String The specified dialect.
getQueryInterface()
Returns an instance of QueryInterface.
See: QueryInterface
Return:
- QueryInterface An instance (singleton) of QueryInterface.
getMigrator([options={}], [force=false])
Returns an instance (singleton) of Migrator.
Params:
| Name | Type | Description |
|---|---|---|
| [options={}] | Object | Some options |
| [force=false] | Boolean | A flag that defines if the migrator should get instantiated or not. |
Return:
- Migrator An instance of Migrator.
define(daoName, attributes, [options])
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(..., {
columnA: {
type: Sequelize.BOOLEAN,
// Other attributes here
},
columnB: Sequelize.STRING,
columnC: 'MY VERY OWN COLUMN TYPE'
})
For a list of possible data types, see http://sequelizejs.com/docs/latest/models#data-types
For more about getters and setters, see http://sequelizejs.com/docs/latest/models#getters---setters
For more about instance and class methods see http://sequelizejs.com/docs/latest/models#expansion-of-models
See: DataTypes
Params:
| Name | Type | Description |
|---|---|---|
| daoName | String | |
| attributes | Object | 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: |
| attributes.column | String|DataType|Object | The description of a database column |
| attributes.column.type | String|DataType | A string or a data type |
| [attributes.column.allowNull=true] | Boolean | If false, the column will have a NOT NULL constraint |
| [attributes.column.defaultValue=null] | Boolean | A literal default value, or a function, see sequelize#fn |
| [attributes.column.unique=false] | String|Boolean | If true, the column will get a unique constraint. If a string is provided, the column will be part of a composite unique index. If multiple columns have the same string, they will be part of the same unique index |
| [attributes.column.primaryKey=false] | Boolean | |
| [attributes.column.autoIncrement=false] | Boolean | |
| [attributes.column.comment=null] | String | |
| [attributes.column.references] | String|DAOFactory | If this column references another table, provide it here as a DAOFactory, or a string |
| [attributes.column.referencesKey='id'] | String | The column of the foreign table that this column references |
| [attributes.column.onUpdate] | String | What should happen when the referenced key is updated. One of CASCADE, RESTRICT or NO ACTION |
| [attributes.column.onDelete] | String | What should happen when the referenced key is deleted. One of CASCADE, RESTRICT or NO ACTION |
| [attributes.column.get] | Function | Provide a custom getter for this column. Use this.getDataValue(String) and this.setDataValue(String, Value) to manipulate the underlying values. |
| [attributes.column.set] | Function | Provide a custom setter for this column. Use this.getDataValue(String) and this.setDataValue(String, Value) to manipulate the underlying values. |
| [options] | Object | These options are merged with the options provided to the Sequelize constructor |
| [options.omitNull] | Boolean | Don't persits null values. This means that all columns with null values will not be saved |
| [options.timestamps=true] | Boolean | Handle createdAt and updatedAt timestamps |
| [options.paranoid=false] | Boolean | Calling destroy will not delete the model, but instead set a deletedAt timestamp if this is true. Needs timestamps=true to work |
| [options.underscored=false] | Boolean | Converts all camelCased columns to underscored if true |
| [options.freezeTableName=false] | Boolean | If freezeTableName is true, sequelize will not try to alter the DAO name to get the table name. Otherwise, the tablename will be pluralized |
| [options.createdAt] | String|Boolean | Override the name of the createdAt column if a string is provided, or disable it if true. Timestamps must be true |
| [options.updatedAt] | String|Boolean | Override the name of the updatedAt column if a string is provided, or disable it if true. Timestamps must be true |
| [options.deletedAt] | String|Boolean | Override the name of the deletedAt column if a string is provided, or disable it if true. Timestamps must be true |
| [options.tableName] | String | Defaults to pluralized DAO name |
| [options.getterMethods] | Object | Provide getter functions that work like those defined per column. If you provide a getter method with the same name as a column, it will be used to access the value of that column. If you provide a name that does not match a column, this function will act as a virtual getter, that can fetch multiple other values |
| [options.setterMethods] | Object | Provide setter functions that work like those defined per column. If you provide a setter method with the same name as a column, it will be used to update the value of that column. If you provide a name that does not match a column, this function will act as a virtual setter, that can act on and set other values, but will not be persisted |
| [options.instanceMethods] | Object | Provide functions that are added to each instance (DAO) |
| [options.classMethods] | Object | Provide functions that are added to the model (DAOFactory) |
| [options.schema='public'] | String | |
| [options.engine] | String | |
| [options.charset] | Strng | |
| [options.comment] | String | |
| [options.collate] | String |
Return:
- DaoFactory
model(daoName)
Fetch a DAO factory which is already defined
Params:
| Name | Type | Description |
|---|---|---|
| daoName | String | The name of a model defined with Sequelize.define |
Return:
- DAOFactory The DAOFactory for daoName
isDefined(daoName)
Checks whether a DAO with the given name is defined
Params:
| Name | Type | Description |
|---|---|---|
| daoName | String | The name of a model defined with Sequelize.define |
Return:
- Boolean Is a DAO with that name already defined?
import(path)
Imports a DAO defined in another file
Imported DAOs 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
Params:
| Name | Type | Description |
|---|---|---|
| path | String | 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 |
Return:
- DAOFactory
query(sql, [callee], [options={}], [replacements])
Execute a query on the DB, with the posibility to bypass all the sequelize goodness.
See: for more information about callee.
Params:
| Name | Type | Description |
|---|---|---|
| sql | String | |
| [callee] | DAOFactory | If callee is provided, the selected data will be used to build an instance of the DAO represented by the factory. Equivalent to calling DAOFactory.build with the values provided by the query. |
| [options={}] | Object | Query options. |
| [options.raw] | Boolean | If true, sequelize will not try to format the results of the query, or build an instance of a model from the result |
| [options.type] | String | What is the type of this query (SELECT, UPDATE etc.). If the query starts with SELECT, the type will be assumed to be SELECT, otherwise no assumptions are made. Only used when raw is false. |
| [options.transaction=null] | Transaction | The transaction that the query should be executed under |
| [replacements] | Object|Array | Either an object of named parameter replacements in the format `:param` or an array of unnamed replacements to replace `?` |
Return:
- EventEmitter
query(sql, [options={raw:true}])
Execute a raw query against the DB.
Params:
| Name | Type | Description |
|---|---|---|
| sql | String | |
| [options={raw:true}] | Object | Query options. See above for a full set of options |
Return:
- EventEmitter
createSchema(schema)
Create a new database schema
Params:
| Name | Type | Description |
|---|---|---|
| schema | String | Name of the schema |
Return:
- EventEmitter
showAllSchemas()
Show all defined schemas
Return:
- EventEmitter
dropSchema(schema)
Drop a single schema
Params:
| Name | Type | Description |
|---|---|---|
| schema | String | Name of the schema |
Return:
- EventEmitter
dropAllSchemas()
Drop all schemas
Return:
- EventEmitter
sync([options={}])
Sync all defined DAOs to the DB.
Params:
| Name | Type | Description |
|---|---|---|
| [options={}] | Object | |
| [options.force=false] | Boolean | If force is true, each DAO will do DROP TABLE IF EXISTS ..., before it tries to create its own table |
| [options.logging=console.log] | Boolean|function | A function that logs sql queries, or false for no logging |
| [options.schema='public'] | String | The schema that the tables should be created in. This can be overriden for each table in sequelize.define |
Return:
- EventEmitter
authenticate()
Test the connetion by trying to authenticate
Return:
- EventEmitter
fn(fn, args)
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.
See: Sequelize#col
Params:
| Name | Type | Description |
|---|---|---|
| fn | String | The function you want to call |
| args | any | All further arguments will be passed as arguments to the function |
Return:
- An instance of Sequelize.fn
col(col)
Creates a object representing a column in the DB. This is usefull in sequelize.fn, since raw string arguments to that will be escaped.
See: Sequelize#fn
Params:
| Name | Type | Description |
|---|---|---|
| col | String | The name of the column |
Return:
- An instance of Sequelize.col
cast(val, type)
Creates a object representing a call to the cast function.
Params:
| Name | Type | Description |
|---|---|---|
| val | any | The value to cast |
| type | String | The type to cast it to |
Return:
- An instance of Sequelize.cast
literal(val)
Creates a object representing a literal, i.e. something that will not be escaped.
Params:
| Name | Type | Description |
|---|---|---|
| val | any |
Return:
- An instance of Sequelize.literal
asIs()
An alias of literal
See: Sequelize#literal
and(args)
An AND query
See: DAOFactory#find
Params:
| Name | Type | Description |
|---|---|---|
| args | String|Object | Each argument will be joined by AND |
Return:
- An instance of Sequelize.and
or(args)
An OR query
See: DAOFactory#find
Params:
| Name | Type | Description |
|---|---|---|
| args | String|Object | Each argument will be joined by OR |
Return:
- An instance of Sequelize.or
transaction([options={}], callback)
Start a transaction.
Params:
| Name | Type | Description |
|---|---|---|
| [options={}] | Object | |
| [options.autocommit=true] | Boolean | |
| [options.isolationLevel='REPEATABLE_READ'] | String | One of READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE. It is preferred to use sequelize.Transaction.ISOLATION_LEVELS as opposed to providing a string |
| callback | Function | Called when the transaction has been set up and is ready for use. If the callback takes two arguments it will be called with err, transaction, otherwise it will be called with transaction. |
Return:
- Transaction
This document is automatically generated based on source code comments. Please do not edit it directly, as your changes will be ignored. Please write on IRC, open an issue or a create a pull request if you feel something can be improved. For help on how to write source code documentation see JSDoc, dox and markdox
This documentation was automagically created on Sun Feb 09 2014 20:14:56 GMT+0100 (CET)