不要怂,就是干,撸起袖子干!

Commit dc57cfb0 by Jan Aagaard Meier

Merge pull request #3066 from Willyham/willForceMaster

Support forcing a query to use the write pool
2 parents 10ff63d2 aa2b4586
<a name="mixin"></a>
# Mixin Mixin
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/associations/mixin.js#L95)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/associations/mixin.js#L95)
Creating assocations in sequelize is done by calling one of the belongsTo / hasOne / hasMany functions
on a model (the source), and prodiving another model as the first argument to the function (the target).
......@@ -89,7 +89,7 @@ you should either disable some constraints, or rethink your associations complet
<a name="hasone"></a>
## `hasOne(target, [options])`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/associations/mixin.js#L145)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/associations/mixin.js#L145)
Creates an association between this (the source) and the provided target. The foreign key is added on the target.
Example: `User.hasOne(Profile)`. This will add userId to the profile table.
......@@ -121,7 +121,7 @@ All methods return a promise
<a name="belongsto"></a>
## `belongsTo(target, [options])`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/associations/mixin.js#L170)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/associations/mixin.js#L170)
Creates an association between this (the source) and the provided target. The foreign key is added on the source.
Example: `Profile.belongsTo(User)`. This will add userId to the profile table.
......@@ -153,7 +153,7 @@ All methods return a promise
<a name="hasmany"></a>
## `hasMany(target, [options])`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/associations/mixin.js#L245)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/associations/mixin.js#L245)
Create an association that is either 1:m or n:m.
```js
......@@ -236,7 +236,7 @@ user.getProjects().success(function (projects) {
<a name="belongstomany"></a>
## `belongsToMany(target, [options])`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/associations/mixin.js#L339)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/associations/mixin.js#L339)
Create an N:M association with a join table
```js
......
<a name="datatypes"></a>
# Class DataTypes
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L309)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L309)
A convenience class holding commonly used data types. The datatypes are used when definining a new model using `Sequelize.define`, like this:
```js
sequelize.define('model', {
......@@ -32,7 +32,7 @@ sequelize.define('model', {
<a name="string"></a>
## `STRING`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L317)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L317)
A variable length string. Default length 255
Available properties: `BINARY`
......@@ -42,7 +42,7 @@ Available properties: `BINARY`
<a name="char"></a>
## `CHAR`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L325)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L325)
A fixed length string. Default length 255
Available properties: `BINARY`
......@@ -52,14 +52,14 @@ Available properties: `BINARY`
<a name="text"></a>
## `TEXT`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L330)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L330)
An unlimited length text column
***
<a name="integer"></a>
## `INTEGER`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L338)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L338)
A 32 bit integer.
Available properties: `UNSIGNED`, `ZEROFILL`
......@@ -69,7 +69,7 @@ Available properties: `UNSIGNED`, `ZEROFILL`
<a name="bigint"></a>
## `BIGINT`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L346)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L346)
A 64 bit integer.
Available properties: `UNSIGNED`, `ZEROFILL`
......@@ -79,35 +79,35 @@ Available properties: `UNSIGNED`, `ZEROFILL`
<a name="time"></a>
## `TIME`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L351)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L351)
A time column
***
<a name="date"></a>
## `DATE`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L356)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L356)
A datetime column
***
<a name="dateonly"></a>
## `DATEONLY`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L361)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L361)
A date only column
***
<a name="boolean"></a>
## `BOOLEAN`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L366)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L366)
A boolean / tinyint column, depending on dialect
***
<a name="float"></a>
## `FLOAT`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L374)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L374)
Floating point number. Accepts one or two arguments for precision
Available properties: `UNSIGNED`, `ZEROFILL`
......@@ -117,14 +117,14 @@ Available properties: `UNSIGNED`, `ZEROFILL`
<a name="now"></a>
## `NOW`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L379)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L379)
A default value of the current timestamp
***
<a name="blob"></a>
## `BLOB`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L385)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L385)
Binary storage. Available lengths: `tiny`, `medium`, `long`
......@@ -132,7 +132,7 @@ Binary storage. Available lengths: `tiny`, `medium`, `long`
<a name="decimal"></a>
## `DECIMAL`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L393)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L393)
Decimal number. Accepts one or two arguments for precision
Available properties: `UNSIGNED`, `ZEROFILL`
......@@ -142,49 +142,49 @@ Available properties: `UNSIGNED`, `ZEROFILL`
<a name="uuid"></a>
## `UUID`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L398)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L398)
A column storing a unique univeral identifier. Use with `UUIDV1` or `UUIDV4` for default values.
***
<a name="uuidv1"></a>
## `UUIDV1`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L403)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L403)
A default unique universal identifier generated following the UUID v1 standard
***
<a name="uuidv4"></a>
## `UUIDV4`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L408)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L408)
A default unique universal identifier generated following the UUID v2 standard
***
<a name="hstore"></a>
## `HSTORE`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L414)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L414)
A key / value column. Only available in postgres.
***
<a name="json"></a>
## `JSON`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L420)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L420)
A JSON string column. Only available in postgres.
***
<a name="jsonb"></a>
## `JSONB`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L426)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L426)
A pre-processed JSON data column. Only available in postgres.
***
<a name="virtual"></a>
## `VIRTUAL`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L456)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L456)
A virtual value that is not stored in the DB. This could for example be useful if you want to provide a default value in your model
that is returned to the user but not stored in the DB.
......@@ -215,7 +215,7 @@ __Aliases:__ NONE
<a name="enum"></a>
## `ENUM`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L464)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L464)
An enumeration. `DataTypes.ENUM('value', 'another value')`.
......@@ -223,7 +223,7 @@ An enumeration. `DataTypes.ENUM('value', 'another value')`.
<a name="array"></a>
## `ARRAY()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/data-types.js#L484)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/data-types.js#L484)
An array of `type`, e.g. `DataTypes.ARRAY(DataTypes.DECIMAL)`. Only available in postgres.
***
......
<a name="errors"></a>
# Class Errors
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L11)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L11)
Sequelize provides a host of custom error classes, to allow you to do easier debugging. All of these errors are exposed on the sequelize object and the sequelize constructor.
All sequelize errors inherit from the base JS error object.
......@@ -9,7 +9,7 @@ All sequelize errors inherit from the base JS error object.
<a name="baseerror"></a>
## `new BaseError()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L20)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L20)
The Base Error all Sequelize Errors inherit from.
__Aliases:__ Error
......@@ -18,7 +18,7 @@ __Aliases:__ Error
<a name="validationerror"></a>
## `new ValidationError(message, [errors])`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L40)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L40)
Validation Error. Thrown when the sequelize validation has failed. The error contains an `errors` property,
which is an array with 1 or more ValidationErrorItems, one for each validation that failed.
......@@ -37,7 +37,7 @@ __Extends:__ BaseError
<a name="get"></a>
## `get(path)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L53)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L53)
Gets all validation error items for the path / field specified.
......@@ -52,14 +52,14 @@ Gets all validation error items for the path / field specified.
<a name="errors"></a>
## `errors()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L67)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L67)
An array of ValidationErrorItems
***
<a name="databaseerror"></a>
## `new DatabaseError()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L74)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L74)
A base class for all database related errors.
__Extends:__ BaseError
......@@ -68,21 +68,21 @@ __Extends:__ BaseError
<a name="parent"></a>
## `parent()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L89)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L89)
The database specific error which triggered this one
***
<a name="sql"></a>
## `sql()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L95)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L95)
The SQL that triggered the error
***
<a name="timeouterror"></a>
## `new TimeoutError()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L102)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L102)
Thrown when a database query times out because of a deadlock
__Extends:__ DatabaseError
......@@ -91,7 +91,7 @@ __Extends:__ DatabaseError
<a name="uniqueconstrainterror"></a>
## `new UniqueConstraintError()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L113)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L113)
Thrown when a unique constraint is violated in the database
__Extends:__ DatabaseError
......@@ -100,7 +100,7 @@ __Extends:__ DatabaseError
<a name="foreignkeyconstrainterror"></a>
## `new ForeignKeyConstraintError()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L132)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L132)
Thrown when a foreign key constraint is violated in the database
__Extends:__ DatabaseError
......@@ -109,35 +109,35 @@ __Extends:__ DatabaseError
<a name="message"></a>
## `message()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L152)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L152)
The message from the DB.
***
<a name="fields"></a>
## `fields()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L158)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L158)
The fields of the unique constraint
***
<a name="value"></a>
## `value()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L164)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L164)
The value(s) which triggered the error
***
<a name="index"></a>
## `index()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L170)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L170)
The name of the index that triggered the error
***
<a name="validationerroritem"></a>
## `new ValidationErrorItem(message, type, path, value)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L182)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L182)
Validation Error Item
Instances of this class are included in the `ValidationError.errors` property.
......@@ -156,7 +156,7 @@ Instances of this class are included in the `ValidationError.errors` property.
<a name="connectionerror"></a>
## `new ConnectionError()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L194)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L194)
A base class for all connection related errors.
__Extends:__ BaseError
......@@ -165,14 +165,14 @@ __Extends:__ BaseError
<a name="parent"></a>
## `parent()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L208)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L208)
The connection specific error which triggered this one
***
<a name="connectionrefusederror"></a>
## `new ConnectionRefusedError()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L215)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L215)
Thrown when a connection to a database is refused
__Extends:__ ConnectionError
......@@ -181,7 +181,7 @@ __Extends:__ ConnectionError
<a name="accessdeniederror"></a>
## `new AccessDeniedError()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L226)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L226)
Thrown when a connection to a database is refused due to insufficient privileges
__Extends:__ ConnectionError
......@@ -190,7 +190,7 @@ __Extends:__ ConnectionError
<a name="hostnotfounderror"></a>
## `new HostNotFoundError()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L237)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L237)
Thrown when a connection to a database has a hostname that was not found
__Extends:__ ConnectionError
......@@ -199,7 +199,7 @@ __Extends:__ ConnectionError
<a name="hostnotreachableerror"></a>
## `new HostNotReachableError()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L248)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L248)
Thrown when a connection to a database has a hostname that was not reachable
__Extends:__ ConnectionError
......@@ -208,7 +208,7 @@ __Extends:__ ConnectionError
<a name="invalidconnectionerror"></a>
## `new InvalidConnectionError()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L259)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L259)
Thrown when a connection to a database has invalid values for any of the connection parameters
__Extends:__ ConnectionError
......@@ -217,7 +217,7 @@ __Extends:__ ConnectionError
<a name="connectiontimedouterror"></a>
## `new ConnectionTimedOutError()`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/errors.js#L270)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/errors.js#L270)
Thrown when a connection to a database times out
__Extends:__ ConnectionError
......
<a name="hooks"></a>
# Mixin Hooks
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L37)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L37)
Hooks are function that are called before and after (bulk-) creation/updating/deletion and validation. Hooks can be added to you models in three ways:
1. By specifying them as options in `sequelize.define`
......@@ -38,7 +38,7 @@ Model.afterBulkUpdate(function () {})
<a name="addhook"></a>
## `addHook(hooktype, [name], fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L149)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L149)
Add a hook to the model
......@@ -56,7 +56,7 @@ __Aliases:__ hook
<a name="hashook"></a>
## `hasHook(hookType)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L171)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L171)
Check whether the mode has any hooks of this type
......@@ -72,7 +72,7 @@ __Aliases:__ hasHooks
<a name="beforevalidate"></a>
## `beforeValidate(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L181)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L181)
A hook that is run before validation
**Params:**
......@@ -87,7 +87,7 @@ A hook that is run before validation
<a name="aftervalidate"></a>
## `afterValidate(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L190)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L190)
A hook that is run after validation
**Params:**
......@@ -102,7 +102,7 @@ A hook that is run after validation
<a name="beforecreate"></a>
## `beforeCreate(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L199)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L199)
A hook that is run before creating a single instance
**Params:**
......@@ -117,7 +117,7 @@ A hook that is run before creating a single instance
<a name="aftercreate"></a>
## `afterCreate(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L208)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L208)
A hook that is run after creating a single instance
**Params:**
......@@ -132,7 +132,7 @@ A hook that is run after creating a single instance
<a name="beforedestroy"></a>
## `beforeDestroy(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L219)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L219)
A hook that is run before destroying a single instance
**Params:**
......@@ -148,7 +148,7 @@ __Aliases:__ beforeDelete
<a name="afterdestroy"></a>
## `afterDestroy(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L234)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L234)
A hook that is run after destroying a single instance
**Params:**
......@@ -164,7 +164,7 @@ __Aliases:__ afterDelete
<a name="beforeupdate"></a>
## `beforeUpdate(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L247)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L247)
A hook that is run before updating a single instance
**Params:**
......@@ -179,7 +179,7 @@ A hook that is run before updating a single instance
<a name="afterupdate"></a>
## `afterUpdate(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L256)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L256)
A hook that is run after updating a single instance
**Params:**
......@@ -194,7 +194,7 @@ A hook that is run after updating a single instance
<a name="beforebulkcreate"></a>
## `beforeBulkCreate(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L265)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L265)
A hook that is run before creating instances in bulk
**Params:**
......@@ -209,7 +209,7 @@ A hook that is run before creating instances in bulk
<a name="afterbulkcreate"></a>
## `afterBulkCreate(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L274)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L274)
A hook that is run after creating instances in bulk
**Params:**
......@@ -224,7 +224,7 @@ A hook that is run after creating instances in bulk
<a name="beforebulkdestroy"></a>
## `beforeBulkDestroy(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L285)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L285)
A hook that is run before destroying instances in bulk
**Params:**
......@@ -240,7 +240,7 @@ __Aliases:__ beforeBulkDelete
<a name="afterbulkdestroy"></a>
## `afterBulkDestroy(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L300)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L300)
A hook that is run after destroying instances in bulk
**Params:**
......@@ -256,7 +256,7 @@ __Aliases:__ afterBulkDelete
<a name="beforebulkupdate"></a>
## `beforeBulkUpdate(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L313)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L313)
A hook that is run after updating instances in bulk
**Params:**
......@@ -271,7 +271,7 @@ A hook that is run after updating instances in bulk
<a name="afterbulkupdate"></a>
## `afterBulkUpdate(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L322)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L322)
A hook that is run after updating instances in bulk
**Params:**
......@@ -286,7 +286,7 @@ A hook that is run after updating instances in bulk
<a name="beforefind"></a>
## `beforeFind(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L331)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L331)
A hook that is run before a find (select) query
**Params:**
......@@ -301,7 +301,7 @@ A hook that is run before a find (select) query
<a name="beforefindafterexpandincludeall"></a>
## `beforeFindAfterExpandIncludeAll(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L340)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L340)
A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded
**Params:**
......@@ -316,7 +316,7 @@ A hook that is run before a find (select) query, after any { include: {all: ...}
<a name="beforefindafteroptions"></a>
## `beforeFindAfterOptions(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L349)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L349)
A hook that is run before a find (select) query, after all option parsing is complete
**Params:**
......@@ -331,7 +331,7 @@ A hook that is run before a find (select) query, after all option parsing is com
<a name="afterfind"></a>
## `afterFind(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L358)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L358)
A hook that is run after a find (select) query
**Params:**
......@@ -346,7 +346,7 @@ A hook that is run after a find (select) query
<a name="beforedefine"></a>
## `beforeDefine(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L367)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L367)
A hook that is run before a define call
**Params:**
......@@ -361,7 +361,7 @@ A hook that is run before a define call
<a name="afterdefine"></a>
## `afterDefine(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L376)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L376)
A hook that is run after a define call
**Params:**
......@@ -376,7 +376,7 @@ A hook that is run after a define call
<a name="beforeinit"></a>
## `beforeInit(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L385)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L385)
A hook that is run before Sequelize() call
**Params:**
......@@ -391,7 +391,7 @@ A hook that is run before Sequelize() call
<a name="afterinit"></a>
## `afterInit(name, fn)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/hooks.js#L394)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/hooks.js#L394)
A hook that is run after Sequelize() call
**Params:**
......
<a name="promise"></a>
# Class Promise
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/promise.js#L25)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/promise.js#L25)
A slightly modified version of bluebird promises. This means that, on top of the methods below, you can also call all the methods listed on the link below.
The main difference is that sequelize promises allows you to attach a listener that will be called with the generated SQL, each time a query is run.
......@@ -15,7 +15,7 @@ If you want to propagate SQL events across `then`, `all` calls etc., you must us
<a name="on"></a>
## `on(evt, fct)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/promise.js#L119)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/promise.js#L119)
Listen for events, event emitter style. Mostly for backwards compat. with EventEmitter
**Deprecated**
......@@ -32,7 +32,7 @@ Listen for events, event emitter style. Mostly for backwards compat. with EventE
<a name="emit"></a>
## `emit(type, value(s)*)`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/promise.js#L140)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/promise.js#L140)
Emit an event from the emitter
**Deprecated**
......@@ -48,7 +48,7 @@ Emit an event from the emitter
<a name="success"></a>
## `success(onSuccess)` -> `this`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/promise.js#L178)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/promise.js#L178)
Listen for success events.
```js
......@@ -71,7 +71,7 @@ __Aliases:__ ok
<a name="error"></a>
## `error(onError)` -> `this`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/promise.js#L205)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/promise.js#L205)
Listen for error events
```js
......@@ -94,7 +94,7 @@ __Aliases:__ fail, failure
<a name="done"></a>
## `done(onDone)` -> `this`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/promise.js#L228)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/promise.js#L228)
Listen for both success and error events.
```js
......@@ -117,7 +117,7 @@ __Aliases:__ complete
<a name="sql"></a>
## `sql(onSQL)` -> `this`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/promise.js#L253)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/promise.js#L253)
Attach a function that is called every time the function that created this emitter executes a query.
**Params:**
......@@ -131,7 +131,7 @@ Attach a function that is called every time the function that created this emitt
<a name="proxy"></a>
## `proxy(promise, [options])` -> `this`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/promise.js#L268)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/promise.js#L268)
Proxy every event of this promise to another one.
**Deprecated**
......
<a name="transaction"></a>
# Class Transaction
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/transaction.js#L12)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/transaction.js#L12)
The transaction object is used to identify a running transaction. It is created by calling `Sequelize.transaction()`.
To run a query under a transaction, you should pass the transaction in the options object.
......@@ -9,7 +9,7 @@ To run a query under a transaction, you should pass the transaction in the optio
<a name="isolation_levels"></a>
## `ISOLATION_LEVELS`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/transaction.js#L45)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/transaction.js#L45)
The possible isolations levels to use when starting a transaction
```js
......@@ -26,7 +26,7 @@ The possible isolations levels to use when starting a transaction
<a name="lock"></a>
## `LOCK`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/transaction.js#L67)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/transaction.js#L67)
Possible options for row locking. Used in conjuction with `find` calls:
```js
......@@ -44,7 +44,7 @@ Model.findAll({
<a name="commit"></a>
## `commit()` -> `this`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/transaction.js#L77)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/transaction.js#L77)
Commit the transaction
......@@ -52,7 +52,7 @@ Commit the transaction
<a name="rollback"></a>
## `rollback()` -> `this`
[View code](https://github.com/sequelize/sequelize/blob/5be311997d5a07030ba62d9df1ab77ec52be415f/lib/transaction.js#L98)
[View code](https://github.com/sequelize/sequelize/blob/dbcbddd0f1bbdad7ff8b4f939616f51b610619a4/lib/transaction.js#L98)
Rollback (abort) the transaction
......
......@@ -12,15 +12,14 @@ var Pooling = require('generic-pool')
, ConnectionManager;
ConnectionManager = function(dialect, sequelize) {
var config = sequelize.config
, self = this;
var config = sequelize.config;
this.sequelize = sequelize;
this.config = config;
this.dialect = dialect;
if (config.pool) {
config.pool = _.extend({}, config.pool); // Make sure we don't modify the existing config object (user might re-use it)
config.pool = _.clone(config.pool); // Make sure we don't modify the existing config object (user might re-use it)
config.pool =_.defaults(config.pool, defaultPoolingConfig, {
validate: this.$validate.bind(this)
}) ;
......@@ -28,7 +27,7 @@ ConnectionManager = function(dialect, sequelize) {
// If the user has turned off pooling we provide a 0/1 pool for backwards compat
config.pool = _.defaults({
max: 1,
min: 0,
min: 0
}, defaultPoolingConfig, {
validate: this.$validate.bind(this)
});
......@@ -66,125 +65,119 @@ ConnectionManager.prototype.close = function () {
// If he does this, generic-pool will try to call connect before the dialect-specific connection manager has been correctly set up
ConnectionManager.prototype.initPools = function () {
var self = this
, config = this.config;
, config = _.cloneDeep(this.config);
if (config.replication) {
var reads = 0
, writes = 0;
if (!config.replication) {
this.pool = Pooling.Pool({
name: 'sequelize-connection',
create: function(callback) {
self.$connect(config).nodeify(function (err, connection) {
callback(err, connection); // For some reason this is needed, else generic-pool things err is a connection or some shit
});
},
destroy: function(connection) {
self.$disconnect(connection);
},
max: config.pool.max,
min: config.pool.min,
validate: config.pool.validate,
idleTimeoutMillis: config.pool.idle
});
return;
}
if (!Array.isArray(config.replication.read)) {
config.replication.read = [config.replication.read];
}
var reads = 0;
// Make sure we don't modify the existing config object (user might re-use it)
config.replication.write = _.extend({}, config.replication.write);
config.replication.read = config.replication.read.map(function (read) {
return _.extend({}, read);
});
if (!Array.isArray(config.replication.read)) {
config.replication.read = [config.replication.read];
}
// Map main connection config
config.replication.write = _.defaults(config.replication.write, {
host: config.host,
port: config.port,
username: config.username,
password: config.password,
database: config.database
// Map main connection config
config.replication.write = _.defaults(config.replication.write, {
host: config.host,
port: config.port,
username: config.username,
password: config.password,
database: config.database
});
// Apply defaults to each read config
config.replication.read = _.map(config.replication.read, function(config) {
return _.defaults(config, {
host: self.config.host,
port: self.config.port,
username: self.config.username,
password: self.config.password,
database: self.config.database
});
});
for (var i = 0; i < config.replication.read.length; i++) {
config.replication.read[i] = _.defaults(config.replication.read[i], {
host: this.config.host,
port: this.config.port,
username: this.config.username,
password: this.config.password,
database: this.config.database
// I'll make my own pool, with blackjack and hookers! (original credit goes to @janzeh)
this.pool = {
release: function(client) {
if (client.queryType === 'read') {
return self.pool.read.release(client);
} else {
return self.pool.write.release(client);
}
},
acquire: function(callback, priority, queryType, useMaster) {
useMaster = _.isUndefined(useMaster) ? false : useMaster;
if (queryType === 'SELECT' && !useMaster) {
self.pool.read.acquire(callback, priority);
} else {
self.pool.write.acquire(callback, priority);
}
},
destroy: function(connection) {
return self.pool[connection.queryType].destroy(connection);
},
destroyAllNow: function() {
self.pool.read.destroyAllNow();
self.pool.write.destroyAllNow();
},
drain: function(cb) {
self.pool.write.drain(function() {
self.pool.read.drain(cb);
});
}
// I'll make my own pool, with blackjack and hookers! (original credit goes to @janzeh)
this.pool = {
release: function(client) {
if (client.queryType === 'read') {
return self.pool.read.release(client);
} else {
return self.pool.write.release(client);
}
},
acquire: function(callback, priority, queryType) {
if (queryType === 'SELECT') {
self.pool.read.acquire(callback, priority);
} else {
self.pool.write.acquire(callback, priority);
}
},
read: Pooling.Pool({
name: 'sequelize-connection-read',
create: function(callback) {
// Simple round robin config
var nextRead = reads++ % config.replication.read.length;
self.$connect(config.replication.read[nextRead]).tap(function (connection) {
connection.queryType = 'read';
}).nodeify(function (err, connection) {
callback(err, connection); // For some reason this is needed, else generic-pool things err is a connection or some shit
});
},
destroy: function(connection) {
return self.pool[connection.queryType].destroy(connection);
},
destroyAllNow: function() {
self.pool.read.destroyAllNow();
self.pool.write.destroyAllNow();
},
drain: function(cb) {
self.pool.write.drain(function() {
self.pool.read.drain(cb);
});
self.$disconnect(connection);
},
read: Pooling.Pool({
name: 'sequelize-connection-read',
create: function(callback) {
if (reads >= config.replication.read.length) {
reads = 0;
}
// Simple round robin config
self.$connect(config.replication.read[reads++]).tap(function (connection) {
connection.queryType = 'read';
}).nodeify(function (err, connection) {
callback(err, connection); // For some reason this is needed, else generic-pool things err is a connection or some shit
});
},
destroy: function(connection) {
self.$disconnect(connection);
},
validate: config.pool.validate,
max: config.pool.max,
min: config.pool.min,
idleTimeoutMillis: config.pool.idle
}),
write: Pooling.Pool({
name: 'sequelize-connection-write',
create: function(callback) {
self.$connect(config.replication.write).tap(function (connection) {
connection.queryType = 'write';
}).nodeify(function (err, connection) {
callback(err, connection); // For some reason this is needed, else generic-pool things err is a connection or some shit
});
},
destroy: function(connection) {
self.$disconnect(connection);
},
validate: config.pool.validate,
max: config.pool.max,
min: config.pool.min,
idleTimeoutMillis: config.pool.idle
})
};
} else {
this.pool = Pooling.Pool({
name: 'sequelize-connection',
validate: config.pool.validate,
max: config.pool.max,
min: config.pool.min,
idleTimeoutMillis: config.pool.idle
}),
write: Pooling.Pool({
name: 'sequelize-connection-write',
create: function(callback) {
self.$connect(config).nodeify(function (err, connection) {
self.$connect(config.replication.write).tap(function (connection) {
connection.queryType = 'write';
}).nodeify(function (err, connection) {
callback(err, connection); // For some reason this is needed, else generic-pool things err is a connection or some shit
});
},
destroy: function(connection) {
self.$disconnect(connection);
},
validate: config.pool.validate,
max: config.pool.max,
min: config.pool.min,
validate: config.pool.validate,
idleTimeoutMillis: config.pool.idle
});
}
})
};
};
ConnectionManager.prototype.getConnection = function(options) {
......@@ -195,7 +188,7 @@ ConnectionManager.prototype.getConnection = function(options) {
self.pool.acquire(function(err, connection) {
if (err) return reject(err);
resolve(connection);
}, options.priority, options.type);
}, options.priority, options.type, options.useMaster);
});
};
ConnectionManager.prototype.releaseConnection = function(connection) {
......
......@@ -654,6 +654,7 @@ module.exports = (function() {
* @param {Boolean} [options.nest=false] If true, transforms objects with `.` separated property names into nested objects using [dottie.js](https://github.com/mickhansen/dottie.js). For example { 'user.username': 'john' } becomes { user: { username: 'john' }}. When `nest` is true, the query type is assumed to be `'SELECT'`, unless otherwise specified
* @param {Boolean} [options.plain=false] Sets the query type to `SELECT` and return a single row
* @param {Object|Array} [options.replacements] Either an object of named parameter replacements in the format `:param` or an array of unnamed replacements to replace `?` in your SQL.
* @param {Boolean} [options.useMaster=false] Force the query to use the write pool, regardless of the query type.
*
* @return {Promise}
*
......
'use strict';
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../../support')
, sinon = require('sinon')
, Config = require(__dirname + '/../../../config/config')
, ConnectionManager = require(__dirname + '/../../../../lib/dialects/abstract/connection-manager')
, Pooling = require('generic-pool')
, _ = require('lodash')
, Promise = require(__dirname + '/../../../../lib/promise');
chai.config.includeStack = true;
var baseConf = Config[Support.getTestDialect()];
var poolEntry = {
host: baseConf.host,
port: baseConf.port,
pool: {}
};
describe('Connction Manager', function() {
var sandbox;
beforeEach(function(){
sandbox = sinon.sandbox.create();
});
afterEach(function(){
sandbox.restore();
});
it('should initialize a single pool without replication', function() {
var options = {
replication: null
};
var sequelize = Support.createSequelizeInstance(options);
var connectionManager = new ConnectionManager(Support.getTestDialect(), sequelize);
var poolSpy = sandbox.spy(Pooling, "Pool");
connectionManager.initPools();
expect(poolSpy.calledOnce).to.be.true;
});
it('should initialize a multiple pools with replication', function() {
var options = {
replication: {
write: _.clone(poolEntry),
read: [_.clone(poolEntry), _.clone(poolEntry)]
}
};
var sequelize = Support.createSequelizeInstance(options);
var connectionManager = new ConnectionManager(Support.getTestDialect(), sequelize);
var poolSpy = sandbox.spy(Pooling, "Pool");
connectionManager.initPools();
expect(poolSpy.calledTwice).to.be.true;
});
it('should round robin calls to the read pool', function() {
if (Support.getTestDialect() === 'sqlite') {
return;
}
var slave1 = _.clone(poolEntry);
var slave2 = _.clone(poolEntry);
slave1.host = 'slave1';
slave2.host = 'slave2';
var options = {
replication: {
write: _.clone(poolEntry),
read: [slave1, slave2]
}
};
var sequelize = Support.createSequelizeInstance(options);
var connectionManager = new ConnectionManager(Support.getTestDialect(), sequelize);
var resolvedPromise = new Promise(function(resolve) {
resolve({
queryType: 'read'
});
});
var connectStub = sandbox.stub(connectionManager, '$connect').returns(resolvedPromise);
connectionManager.initPools();
var queryOptions = {
priority: 0,
type: 'SELECT',
useMaster: false
};
var _getConnection = _.bind(connectionManager.getConnection, connectionManager, queryOptions);
return _getConnection()
.then(_getConnection)
.then(_getConnection)
.then(function checkPoolHosts() {
chai.expect(connectStub.calledThrice).to.be.true;
var calls = connectStub.getCalls();
chai.expect(calls[0].args[0].host).to.eql('slave1');
chai.expect(calls[1].args[0].host).to.eql('slave2');
chai.expect(calls[2].args[0].host).to.eql('slave1');
});
});
it('should allow forced reads from the write pool', function() {
var master = _.clone(poolEntry);
master.host = 'the-boss';
var options = {
replication: {
write: master,
read: [_.clone(poolEntry)]
}
};
var sequelize = Support.createSequelizeInstance(options);
var connectionManager = new ConnectionManager(Support.getTestDialect(), sequelize);
var resolvedPromise = new Promise(function(resolve) {
resolve({
queryType: 'read'
});
});
var connectStub = sandbox.stub(connectionManager, '$connect').returns(resolvedPromise);
connectionManager.initPools();
var queryOptions = {
priority: 0,
type: 'SELECT',
useMaster: true
};
return connectionManager.getConnection(queryOptions)
.then(function checkPoolHosts() {
chai.expect(connectStub.calledOnce).to.be.true;
var calls = connectStub.getCalls();
chai.expect(calls[0].args[0].host).to.eql('the-boss');
});
});
});
......@@ -77,10 +77,9 @@ describe(Support.getTestDialectTeaser('Sequelize#transaction'), function() {
// how could we enforce an authentication error in sqlite?
} else {
it('gets triggered once an error occurs', function(done) {
var sequelize = Support.createSequelizeInstance();
// lets overwrite the host to get an error
sequelize.config.username = 'foobarbaz';
// Supply bad config to force an error
var sequelize = Support.getSequelizeInstance('this', 'is', 'fake config');
sequelize
.transaction().then(function() {})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!