Count everything currently associated with this, using an optional where clause
**Params:**
| Name | Type | Description |
| ---- | ---- | ----------- |
| [options] | Object | |
| [options.where] | Object | An optional where clause to limit the associated models |
| [options.scope] | String | Boolean | Apply a scope on the related model, or remove its default scope by passing false |
***
_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 <a href="irc://irc.freenode.net/#sequelizejs">IRC</a>, 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](http://usejsdoc.org) and [dox](https://github.com/tj/dox)_
_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 <a href="irc://irc.freenode.net/#sequelizejs">IRC</a>, 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](http://usejsdoc.org) and [dox](https://github.com/tj/dox)_
In the API reference below, replace `Assocation(s)` with the actual name of your association, e.g. for `User.belongsToMany(Project)` the getter will be `user.getProjects()`.
In the API reference below, replace `Assocation` with the actual name of your association, e.g. for `User.belongsTo(Project)` the getter will be `user.getProject()`.
| [newAssociations] | Instance | String | Number | An array of instances or primary key of instances to associate with this. Pass `null` or `undefined` to remove all associations. |
| [newAssociation] | Instance | String | Number | An instance or the primary key of an instance to associate with this. Pass `null` or `undefined` to remove the association. |
| [options] | Object | Options passed to `this.save` |
| [options] | Object | Options passed to `this.save` |
| [options.save=true] | Boolean | Skip saving this after setting the foreign key if false. |
| [options.save=true] | Boolean | Skip saving this after setting the foreign key if false. |
In the API reference below, replace `Assocation(s)` with the actual name of your association, e.g. for `User.belongsToMany(Project)` the getter will be `user.getProjects()`.
In the API reference below, replace `Assocation(s)` with the actual name of your association, e.g. for `User.belongsToMany(Project)` the getter will be `user.getProjects()`.
...
@@ -10,7 +10,7 @@ In the API reference below, replace `Assocation(s)` with the actual name of your
...
@@ -10,7 +10,7 @@ In the API reference below, replace `Assocation(s)` with the actual name of your
Count everything currently associated with this, using an optional where clause
**Params:**
| Name | Type | Description |
| ---- | ---- | ----------- |
| [options] | Object | |
| [options.where] | Object | An optional where clause to limit the associated models |
| [options.scope] | String | Boolean | Apply a scope on the related model, or remove its default scope by passing false |
***
_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 <a href="irc://irc.freenode.net/#sequelizejs">IRC</a>, 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](http://usejsdoc.org) and [dox](https://github.com/tj/dox)_
_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 <a href="irc://irc.freenode.net/#sequelizejs">IRC</a>, 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](http://usejsdoc.org) and [dox](https://github.com/tj/dox)_
In the API reference below, replace `Assocation(s)` with the actual name of your association, e.g. for `User.belongsToMany(Project)` the getter will be `user.getProjects()`.
In the API reference below, replace `Assocation(s)` with the actual name of your association, e.g. for `User.belongsToMany(Project)` the getter will be `user.getProjects()`.
...
@@ -10,7 +10,7 @@ In the API reference below, replace `Assocation(s)` with the actual name of your
...
@@ -10,7 +10,7 @@ In the API reference below, replace `Assocation(s)` with the actual name of your
Creating assocations in sequelize is done by calling one of the belongsTo / hasOne / hasMany functions on a model (the source), and providing another model as the first argument to the function (the target).
Creating assocations in sequelize is done by calling one of the belongsTo / hasOne / hasMany functions on a model (the source), and providing another model as the first argument to the function (the target).
* hasOne - adds a foreign key to target
* hasOne - adds a foreign key to target
...
@@ -80,7 +80,7 @@ Note how we also specified `constraints: false` for profile picture. This is bec
...
@@ -80,7 +80,7 @@ Note how we also specified `constraints: false` for profile picture. This is bec
Creates an association between this (the source) and the provided target. The foreign key is added on the target.
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.
Example: `User.hasOne(Profile)`. This will add userId to the profile table.
...
@@ -95,7 +95,7 @@ Example: `User.hasOne(Profile)`. This will add userId to the profile table.
...
@@ -95,7 +95,7 @@ Example: `User.hasOne(Profile)`. This will add userId to the profile table.
| [options.hooks=false] | boolean | Set to true to run before-/afterDestroy hooks when an associated model is deleted because of a cascade. For example if `User.hasOne(Profile, {onDelete: 'cascade', hooks:true})`, the before-/afterDestroy hooks for profile will be called when a user is deleted. Otherwise the profile will be deleted without invoking any hooks |
| [options.hooks=false] | boolean | Set to true to run before-/afterDestroy hooks when an associated model is deleted because of a cascade. For example if `User.hasOne(Profile, {onDelete: 'cascade', hooks:true})`, the before-/afterDestroy hooks for profile will be called when a user is deleted. Otherwise the profile will be deleted without invoking any hooks |
| [options.as] | string | The alias of this model, in singular form. See also the `name` option passed to `sequelize.define`. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the assocition, you should provide the same alias when eager loading and when getting assocated models. Defaults to the singularized name of target |
| [options.as] | string | The alias of this model, in singular form. See also the `name` option passed to `sequelize.define`. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the assocition, you should provide the same alias when eager loading and when getting assocated models. Defaults to the singularized name of target |
| [options.foreignKey] | string | object | The name of the foreign key in the target table or an object representing the type definition for the foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property to set the name of the colum. Defaults to the name of source + primary key of source |
| [options.foreignKey] | string | object | The name of the foreign key in the target table or an object representing the type definition for the foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property to set the name of the colum. Defaults to the name of source + primary key of source |
| [options.onDelete='SET NULL'] | string | |
| [options.onDelete='SET NULL | CASCADE'] | string | SET NULL if foreignKey allows nulls, CASCADE if otherwise |
| [options.onUpdate='CASCADE'] | string | |
| [options.onUpdate='CASCADE'] | string | |
| [options.constraints=true] | boolean | Should on update and on delete constraints be enabled on the foreign key. |
| [options.constraints=true] | boolean | Should on update and on delete constraints be enabled on the foreign key. |
...
@@ -104,7 +104,7 @@ Example: `User.hasOne(Profile)`. This will add userId to the profile table.
...
@@ -104,7 +104,7 @@ Example: `User.hasOne(Profile)`. This will add userId to the profile table.
Creates an association between this (the source) and the provided target. The foreign key is added on the source.
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.
Example: `Profile.belongsTo(User)`. This will add userId to the profile table.
...
@@ -120,7 +120,7 @@ Example: `Profile.belongsTo(User)`. This will add userId to the profile table.
...
@@ -120,7 +120,7 @@ Example: `Profile.belongsTo(User)`. This will add userId to the profile table.
| [options.as] | string | The alias of this model, in singular form. See also the `name` option passed to `sequelize.define`. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the association, you should provide the same alias when eager loading and when getting assocated models. Defaults to the singularized name of target |
| [options.as] | string | The alias of this model, in singular form. See also the `name` option passed to `sequelize.define`. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the association, you should provide the same alias when eager loading and when getting assocated models. Defaults to the singularized name of target |
| [options.foreignKey] | string | object | The name of the foreign key in the source table or an object representing the type definition for the foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property to set the name of the column. Defaults to the name of target + primary key of target |
| [options.foreignKey] | string | object | The name of the foreign key in the source table or an object representing the type definition for the foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property to set the name of the column. Defaults to the name of target + primary key of target |
| [options.targetKey] | string | The name of the field to use as the key for the association in the target table. Defaults to the primary key of the target table |
| [options.targetKey] | string | The name of the field to use as the key for the association in the target table. Defaults to the primary key of the target table |
| [options.onDelete='SET NULL'] | string | |
| [options.onDelete='SET NULL | NO ACTION'] | string | SET NULL if foreignKey allows nulls, NO ACTION if otherwise |
| [options.onUpdate='CASCADE'] | string | |
| [options.onUpdate='CASCADE'] | string | |
| [options.constraints=true] | boolean | Should on update and on delete constraints be enabled on the foreign key. |
| [options.constraints=true] | boolean | Should on update and on delete constraints be enabled on the foreign key. |
...
@@ -129,7 +129,7 @@ Example: `Profile.belongsTo(User)`. This will add userId to the profile table.
...
@@ -129,7 +129,7 @@ Example: `Profile.belongsTo(User)`. This will add userId to the profile table.
| [options.hooks=false] | boolean | Set to true to run before-/afterDestroy hooks when an associated model is deleted because of a cascade. For example if `User.hasOne(Profile, {onDelete: 'cascade', hooks:true})`, the before-/afterDestroy hooks for profile will be called when a user is deleted. Otherwise the profile will be deleted without invoking any hooks |
| [options.hooks=false] | boolean | Set to true to run before-/afterDestroy hooks when an associated model is deleted because of a cascade. For example if `User.hasOne(Profile, {onDelete: 'cascade', hooks:true})`, the before-/afterDestroy hooks for profile will be called when a user is deleted. Otherwise the profile will be deleted without invoking any hooks |
| [options.through] | Model | string | object | The name of the table that is used to join source and target in n:m associations. Can also be a sequelize model if you want to define the junction table yourself and add extra attributes to it. |
| [options.through.model] | Model | The model used to join both sides of the N:M association. |
| [options.through.scope] | object | A key/value set that will be used for association create and find defaults on the through model. (Remember to add the attributes to the through model) |
| [options.through.unique=true] | boolean | If true a unique key will be generated from the foreign keys used (might want to turn this off and create specific unique keys when using scopes) |
| [options.as] | string | object | The alias of this model. If you provide a string, it should be plural, and will be singularized using node.inflection. If you want to control the singular version yourself, provide an object with `plural` and `singular` keys. See also the `name` option passed to `sequelize.define`. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the assocition, you should provide the same alias when eager loading and when getting assocated models. Defaults to the pluralized name of target |
| [options.as] | string | object | The alias of this model. If you provide a string, it should be plural, and will be singularized using node.inflection. If you want to control the singular version yourself, provide an object with `plural` and `singular` keys. See also the `name` option passed to `sequelize.define`. If you create multiple associations between the same tables, you should provide an alias to be able to distinguish between them. If you provide an alias when creating the assocition, you should provide the same alias when eager loading and when getting assocated models. Defaults to the pluralized name of target |
| [options.foreignKey] | string | object | The name of the foreign key in the target table / join table or an object representing the type definition for the foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property to set the name of the colum. Defaults to the name of source + primary key of source |
| [options.foreignKey] | string | object | The name of the foreign key in the target table / join table or an object representing the type definition for the foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property to set the name of the colum. Defaults to the name of source + primary key of source |
| [options.scope] | object | A key/value set that will be used for association create and find defaults on the target. (sqlite not supported for N:M) |
| [options.scope] | object | A key/value set that will be used for association create and find defaults on the target. (sqlite not supported for N:M) |
| [options.onDelete='SET NULL | CASCADE'] | string | Cascade if this is a n:m, and set null if it is a 1:m |
| [options.onDelete='SET NULL | CASCADE'] | string | SET NULL if foreignKey allows nulls, CASCADE if otherwise |
| [options.onUpdate='CASCADE'] | string | |
| [options.onUpdate='CASCADE'] | string | |
| [options.constraints=true] | boolean | Should on update and on delete constraints be enabled on the foreign key. |
| [options.constraints=true] | boolean | Should on update and on delete constraints be enabled on the foreign key. |
| [options.foreignKey] | string | object | The name of the foreign key in the join table (representing the source model) or an object representing the type definition for the foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property to set the name of the colum. Defaults to the name of source + primary key of source |
| [options.foreignKey] | string | object | The name of the foreign key in the join table (representing the source model) or an object representing the type definition for the foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property to set the name of the colum. Defaults to the name of source + primary key of source |
| [options.otherKey] | string | object | The name of the foreign key in the join table (representing the target model) or an object representing the type definition for the other column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property to set the name of the colum. Defaults to the name of target + primary key of target |
| [options.otherKey] | string | object | The name of the foreign key in the join table (representing the target model) or an object representing the type definition for the other column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property to set the name of the colum. Defaults to the name of target + primary key of target |
| [options.scope] | object | A key/value set that will be used for association create and find defaults on the target. (sqlite not supported for N:M) |
| [options.scope] | object | A key/value set that will be used for association create and find defaults on the target. (sqlite not supported for N:M) |
| [options.timestamps=sequelize.options.timestamps] | boolean | Should the join model have timestamps |
| [options.onDelete='SET NULL | CASCADE'] | string | Cascade if this is a n:m, and set null if it is a 1:m |
| [options.onDelete='SET NULL | CASCADE'] | string | Cascade if this is a n:m, and set null if it is a 1:m |
| [options.onUpdate='CASCADE'] | string | |
| [options.onUpdate='CASCADE'] | string | |
| [options.constraints=true] | boolean | Should on update and on delete constraints be enabled on the foreign key. |
| [options.constraints=true] | boolean | Should on update and on delete constraints be enabled on the foreign key. |
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.
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.
You could also use it to validate a value before permuting and storing it. Checking password length before hashing it for example:
You could also use it to validate a value before permuting and storing it. Checking password length before hashing it for example:
```js
```js
sequelize.define('user',{
sequelize.define('user',{
password_hash:DataTypes.STRING
password_hash:DataTypes.STRING,
password:{
password:{
type:DataTypes.VIRTUAL,
type:DataTypes.VIRTUAL,
set:function(val){
set:function(val){
...
@@ -237,6 +237,20 @@ sequelize.define('user', {
...
@@ -237,6 +237,20 @@ sequelize.define('user', {
}
}
})
})
```
```
VIRTUAL also takes a return type and dependency fields as arguments
If a virtual attribute is present in `attributes` it will automatically pull in the extra fields aswell.
Return type is mostly usefull for setups that rely on types like GraphQL.
```js
{
active: {
type: new DataTypes.VIRTUAL(DataTypes.BOOLEAN, ['createdAt']),
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.
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.
All sequelize errors inherit from the base JS error object.
...
@@ -9,7 +9,7 @@ All sequelize errors inherit from the base JS error object.
...
@@ -9,7 +9,7 @@ All sequelize errors inherit from the base JS error object.
| fn | Function | A callback function that is called with options passed to sequelize.sync |
***
_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 <a href="irc://irc.freenode.net/#sequelizejs">IRC</a>, 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](http://usejsdoc.org) and [dox](https://github.com/tj/dox)_
_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 <a href="irc://irc.freenode.net/#sequelizejs">IRC</a>, 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](http://usejsdoc.org) and [dox](https://github.com/tj/dox)_
Set is used to update values on the instance (the sequelize representation of the instance that is, remember that nothing will be persisted before you actually call `save`).
Set is used to update values on the instance (the sequelize representation of the instance that is, remember that nothing will be persisted before you actually call `save`).
In its most basic form `set` will update a value stored in the underlying `dataValues` object. However, if a custom setter function is defined for the key, that function
In its most basic form `set` will update a value stored in the underlying `dataValues` object. However, if a custom setter function is defined for the key, that function
will be called instead. To bypass the setter, you can pass `raw: true` in the options object.
will be called instead. To bypass the setter, you can pass `raw: true` in the options object.
If changed is called with a string it will return a boolean indicating whether the value of that key in `dataValues` is different from the value in `_previousDataValues`.
If changed is called with a string it will return a boolean indicating whether the value of that key in `dataValues` is different from the value in `_previousDataValues`.
If changed is called without an argument, it will return an array of keys that have changed.
If changed is called without an argument, it will return an array of keys that have changed.
...
@@ -172,7 +172,7 @@ If changed is called without an argument and no keys have changed, it will retur
...
@@ -172,7 +172,7 @@ If changed is called without an argument and no keys have changed, it will retur
Validate this instance, and if the validation passes, persist it to the database.
Validate this instance, and if the validation passes, persist it to the database. It will only save changed fields, and do nothing if no fields have changed.
On success, the callback will be called with this instance. On validation error, the callback will be called with an instance of `Sequelize.ValidationError`.
On success, the callback will be called with this instance. On validation error, the callback will be called with an instance of `Sequelize.ValidationError`.
This error will have a property for each of the fields for which validation failed, with the error message for that field.
This error will have a property for each of the fields for which validation failed, with the error message for that field.
...
@@ -203,13 +203,14 @@ This error will have a property for each of the fields for which validation fail
...
@@ -203,13 +203,14 @@ This error will have a property for each of the fields for which validation fail
| [options.validate=true] | Boolean | If false, validations won't be run. |
| [options.validate=true] | Boolean | If false, validations won't be run. |
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.transaction] | Transaction | |
| [options.transaction] | Transaction | |
| [options.searchPath=DEFAULT] | String | An optional parameter to specify the schema search_path (Postgres only) |
Destroy the row corresponding to this instance. Depending on your setting for paranoid, the row will either be completely deleted, or have its deletedAt timestamp set to the current time.
Destroy the row corresponding to this instance. Depending on your setting for paranoid, the row will either be completely deleted, or have its deletedAt timestamp set to the current time.
...
@@ -290,13 +291,14 @@ Destroy the row corresponding to this instance. Depending on your setting for pa
...
@@ -290,13 +291,14 @@ Destroy the row corresponding to this instance. Depending on your setting for pa
| [options.force=false] | Boolean | If set to true, paranoid models will actually be deleted |
| [options.force=false] | Boolean | If set to true, paranoid models will actually be deleted |
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.transaction] | Transaction | |
| [options.transaction] | Transaction | |
| [options.searchPath=DEFAULT] | String | An optional parameter to specify the schema search_path (Postgres only) |
Increment the value of one or more columns. This is done in the database, which means it does not use the values currently stored on the Instance. The increment is done using a
Increment the value of one or more columns. This is done in the database, which means it does not use the values currently stored on the Instance. The increment is done using a
Decrement the value of one or more columns. This is done in the database, which means it does not use the values currently stored on the Instance. The decrement is done using a
Decrement the value of one or more columns. This is done in the database, which means it does not use the values currently stored on the Instance. The decrement is done using a
Convert the instance to a JSON representation. Proxies to calling `get` with no keys. This means get all values gotten from the DB, and apply all custom getters.
Convert the instance to a JSON representation. Proxies to calling `get` with no keys. This means get all values gotten from the DB, and apply all custom getters.
A Model represents a table in the database. Sometimes you might also see it referred to as model, or simply as factory.
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`
This class should _not_ be instantiated directly, it is created using `sequelize.define`, and already created models can be loaded using `sequelize.import`
...
@@ -12,7 +12,7 @@ This class should _not_ be instantiated directly, it is created using `sequelize
...
@@ -12,7 +12,7 @@ This class should _not_ be instantiated directly, it is created using `sequelize
Add a new scope to the model. This is especially useful for adding scopes with includes, when the model you want to include is not available at the time this model is defined.
By default this will throw an error if a scope with that name already exists. Pass `override: true` in the options object to silence this error.
**Params:**
| Name | Type | Description |
| ---- | ---- | ----------- |
| name | String | The name of the scope. Use `defaultScope` to override the default scope |
The success listener is called with an array of instances if the query succeeds.
The success listener is called with an array of instances if the query succeeds.
...
@@ -233,6 +258,8 @@ The success listener is called with an array of instances if the query succeeds.
...
@@ -233,6 +258,8 @@ The success listener is called with an array of instances if the query succeeds.
| [options.include[].where] | Object | Where clauses to apply to the child models. Note that this converts the eager load to an inner join, unless you explicitly set `required: false` |
| [options.include[].where] | Object | Where clauses to apply to the child models. Note that this converts the eager load to an inner join, unless you explicitly set `required: false` |
| [options.include[].attributes] | Array.<String> | A list of attributes to select from the child model |
| [options.include[].attributes] | Array.<String> | A list of attributes to select from the child model |
| [options.include[].required] | Boolean | If true, converts to an inner join, which means that the parent model will only be loaded if it has any matching children. True if `include.where` is set, false otherwise. |
| [options.include[].required] | Boolean | If true, converts to an inner join, which means that the parent model will only be loaded if it has any matching children. True if `include.where` is set, false otherwise. |
| [options.include[].separate] | Boolean | If true, runs a separate query to fetch the associated instances, only supported for hasMany associations |
| [options.include[].limit] | Number | Limit the joined rows, only supported with include.separate=true |
| [options.include[].through.where] | Object | Filter on the join model for belongsToMany relations |
| [options.include[].through.where] | Object | Filter on the join model for belongsToMany relations |
| [options.include[].through.attributes] | Array | A list of attributes to select from the join model for belongsToMany relations |
| [options.include[].through.attributes] | Array | A list of attributes to select from the join model for belongsToMany relations |
| [options.include[].include] | Array.<Object | Model> | Load further nested related models |
| [options.include[].include] | Array.<Object | Model> | Load further nested related models |
...
@@ -244,6 +271,7 @@ The success listener is called with an array of instances if the query succeeds.
...
@@ -244,6 +271,7 @@ The success listener is called with an array of instances if the query succeeds.
| [options.raw] | Boolean | Return raw result. See sequelize.query for more information. |
| [options.raw] | Boolean | Return raw result. See sequelize.query for more information. |
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.having] | Object | |
| [options.having] | Object | |
| [options.searchPath=DEFAULT] | String | An optional parameter to specify the schema search_path (Postgres only) |
Find all the rows matching your query, within a specified offset / limit, and get the total number of rows matching your query. This is very usefull for paging
Find all the rows matching your query, within a specified offset / limit, and get the total number of rows matching your query. This is very usefull for paging
Insert or update a single row. An update will be executed if a row which matches the supplied values on either the primary key or a unique key is found. Note that the unique index must be defined in your sequelize model and not just in the table. Otherwise you may experience a unique constraint violation, because sequelize fails to identify the row that should be updated.
Insert or update a single row. An update will be executed if a row which matches the supplied values on either the primary key or a unique key is found. Note that the unique index must be defined in your sequelize model and not just in the table. Otherwise you may experience a unique constraint violation, because sequelize fails to identify the row that should be updated.
**Implementation details:**
**Implementation details:**
...
@@ -568,7 +624,9 @@ Insert or update a single row. An update will be executed if a row which matches
...
@@ -568,7 +624,9 @@ Insert or update a single row. An update will be executed if a row which matches
| [options] | Object | |
| [options] | Object | |
| [options.validate=true] | Boolean | Run validations before the row is inserted |
| [options.validate=true] | Boolean | Run validations before the row is inserted |
| [options.fields=Object.keys(this.attributes)] | Array | The fields to insert / update. Defaults to all fields |
| [options.fields=Object.keys(this.attributes)] | Array | The fields to insert / update. Defaults to all fields |
| [options.transaction] | Transaction | Transaction to run query under |
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.searchPath=DEFAULT] | String | An optional parameter to specify the schema search_path (Postgres only) |
__Returns:__ Returns a boolean indicating whether the row was created or updated.
__Returns:__ Returns a boolean indicating whether the row was created or updated.
The success handler is passed an array of instances, but please notice that these may not completely represent the state of the rows in the DB. This is because MySQL
The success handler is passed an array of instances, but please notice that these may not completely represent the state of the rows in the DB. This is because MySQL
...
@@ -599,13 +657,14 @@ To obtain Instances for the newly created values, you will need to query for the
...
@@ -599,13 +657,14 @@ To obtain Instances for the newly created values, you will need to query for the
| [options.updateOnDuplicate] | Array | Fields to update if row key already exists (on duplicate key update)? (only supported by mysql & mariadb). By default, all fields are updated. |
| [options.updateOnDuplicate] | Array | Fields to update if row key already exists (on duplicate key update)? (only supported by mysql & mariadb). By default, all fields are updated. |
| [options.transaction] | Transaction | Transaction to run query under |
| [options.transaction] | Transaction | Transaction to run query under |
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.searchPath=DEFAULT] | String | An optional parameter to specify the schema search_path (Postgres only) |
Truncate all instances of the model. This is a convenient method for Model.destroy({ truncate: true }).
Truncate all instances of the model. This is a convenient method for Model.destroy({ truncate: true }).
...
@@ -621,14 +680,16 @@ Truncate all instances of the model. This is a convenient method for Model.destr
...
@@ -621,14 +680,16 @@ Truncate all instances of the model. This is a convenient method for Model.destr
| [options] | object | The options passed to Model.destroy in addition to truncate |
| [options] | object | The options passed to Model.destroy in addition to truncate |
| [options.transaction] | Boolean | function | Transaction to run query under |
| [options.transaction] | Boolean | function | Transaction to run query under |
| [options.cascade | Boolean | function | = false] Only used in conjuction with TRUNCATE. Truncates all tables that have foreign-key references to the named table, or to any tables added to the group due to CASCADE. |
| [options.cascade | Boolean | function | = false] Only used in conjuction with TRUNCATE. Truncates all tables that have foreign-key references to the named table, or to any tables added to the group due to CASCADE. |
| [options.transaction] | Transaction | Transaction to run query under |
| [options.logging] | Boolean | function | A function that logs sql queries, or false for no logging |
| [options.logging] | Boolean | function | A function that logs sql queries, or false for no logging |
| [options.searchPath=DEFAULT] | String | An optional parameter to specify the schema search_path (Postgres only) |
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.
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.
**See:**
**See:**
...
@@ -120,26 +127,21 @@ A reference to sequelize utilities. Most users will not need to use these utils
...
@@ -120,26 +127,21 @@ A reference to sequelize utilities. Most users will not need to use these utils
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.
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.
**See:**
**See:**
...
@@ -151,7 +153,7 @@ Exposes the validator.js object, so you can extend it with custom validation fun
...
@@ -151,7 +153,7 @@ Exposes the validator.js object, so you can extend it with custom validation fun
Define a new model, representing a table in the DB.
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:
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:
...
@@ -501,7 +515,7 @@ For more about validation, see http://docs.sequelizejs.com/en/latest/docs/models
...
@@ -501,7 +515,7 @@ For more about validation, see http://docs.sequelizejs.com/en/latest/docs/models
Execute a query on the DB, with the posibility to bypass all the sequelize goodness.
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.
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.
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.logging=false] | Function | A function that gets executed while running the query to log the sql. |
| [options.instance] | Instance | A sequelize instance used to build the return instance |
| [options.instance] | Instance | A sequelize instance used to build the return instance |
| [options.model] | Model | A sequelize model used to build the returned model instances (used to be called callee) |
| [options.model] | Model | A sequelize model used to build the returned model instances (used to be called callee) |
| [options.searchPath=DEFAULT] | String | An optional parameter to specify the schema search_path (Postgres only) |
| [options.supportsSearchPath] | Boolean | If false do not prepend the query with the search_path (Postgres only) |
| [options.mapToModel=false] | Object | Map returned fields to model's fields if `options.model` or `options.instance` is present. Mapping will occur before building the model instance. |
| [options.fieldMap] | Object | Map returned fields to arbitrary names for `SELECT` query type. |
@@ -720,13 +738,15 @@ Sync all defined models to the DB.
...
@@ -720,13 +738,15 @@ Sync all defined models to the DB.
| [options.match] | RegEx | Match a regex against the database name before syncing, a safety check for cases where force: true is used in tests but not live code |
| [options.match] | RegEx | Match a regex against the database name before syncing, a safety check for cases where force: true is used in tests but not live code |
| [options.logging=console.log] | Boolean | function | A function that logs sql queries, or false for no logging |
| [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 |
| [options.schema='public'] | String | The schema that the tables should be created in. This can be overriden for each table in sequelize.define |
| [options.searchPath=DEFAULT] | String | An optional parameter to specify the schema search_path (Postgres only) |
| [options.hooks=true] | Boolean | If hooks is true then beforeSync, afterSync, beforBulkSync, afterBulkSync hooks will be called |
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.
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.
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.
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.
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.
**See:**
**See:**
...
@@ -829,7 +849,7 @@ Creates a object representing a column in the DB. This is often useful in conjun
...
@@ -829,7 +849,7 @@ Creates a object representing a column in the DB. This is often useful in conjun
The attr can either be an object taken from `Model.rawAttributes` (for example `Model.rawAttributes.id` or `Model.rawAttributes.name`). The
The attr can either be an object taken from `Model.rawAttributes` (for example `Model.rawAttributes.id` or `Model.rawAttributes.name`). The
...
@@ -939,7 +959,7 @@ For string attributes, use the regular `{ where: { attr: something }}` syntax. I
...
@@ -939,7 +959,7 @@ For string attributes, use the regular `{ where: { attr: something }}` syntax. I
| ---- | ---- | ----------- |
| ---- | ---- | ----------- |
| attr | Object | 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 |
| attr | Object | 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='='] | string | |
| [comparator='='] | string | |
| logic | String | Object | The condition. Can be both a simply type, or a further condition (`.or`, `.and`, `.literal` etc.) |
| logic | String | Object | The condition. Can be both a simply type, or a further condition (`$or`, `$and`, `.literal` etc.) |
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
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