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

Commit 14e98ff2 by Mick Hansen

Merge pull request #3614 from sequelize/refactor-scopes

Refactor scopes
2 parents f970157b 6fd677ed
# Next # Next
- [FEATURE] Lock modes in Postgres now support `OF table` - [FEATURE] Lock modes in Postgres now support `OF table`
- [FEATURE] New transaction lock modes `FOR KEY SHARE` and `NO KEY UPDATE` for Postgres 9.3+ - [FEATURE] New transaction lock modes `FOR KEY SHARE` and `NO KEY UPDATE` for Postgres 9.3+
- [FEATURE/REFACTOR] Rewritten scopes with complete support for includes and scopes across associations
# 2.1.0 # 2.1.0
- [BUG] Enable standards conforming strings on connection in postgres. Adresses [#3545](https://github.com/sequelize/sequelize/issues/3545) - [BUG] Enable standards conforming strings on connection in postgres. Adresses [#3545](https://github.com/sequelize/sequelize/issues/3545)
...@@ -17,6 +18,7 @@ ...@@ -17,6 +18,7 @@
#### Backwards compatibility changes #### Backwards compatibility changes
- Events support have been removed so using `.on('succes')` or `.succes()` is no longer supported. - Events support have been removed so using `.on('succes')` or `.succes()` is no longer supported.
- Trying to apply a scope that does not exist will always throw an error
# 2.0.6 # 2.0.6
- [BUG] Don't update virtual attributes in Model.update. Fixes [#2860](https://github.com/sequelize/sequelize/issues/2860) - [BUG] Don't update virtual attributes in Model.update. Fixes [#2860](https://github.com/sequelize/sequelize/issues/2860)
......
...@@ -188,14 +188,153 @@ UserProjects = sequelize.define('UserProjects', { ...@@ -188,14 +188,153 @@ UserProjects = sequelize.define('UserProjects', {
}) })
``` ```
## Scopes
This section concerns association scopes. For a definition of assocation scopes vs. scopes on associated models, see [Scopes](docs/scopes).
Association scopes allow you to place a scope (a set of default attributes for `get` and `create`) on the association. Scopes can be placed both on the associated model (the target of the association), and on the through table for n:m relations.
#### 1:m
Assume we have tables Comment, Post and Image. A comment can be associated to either an image or a post via `commentable_id` and `commentable` - we say that Post and Image are `Commentable`
```js
this.Comment = this.sequelize.define('comment', {
title: Sequelize.STRING,
commentable: Sequelize.STRING,
commentable_id: Sequelize.INTEGER
}, {
instanceMethods: {
getItem: function() {
return this['get' + this.get('commentable').substr(0, 1).toUpperCase() + this.get('commentable').substr(1)]();
}
}
});
this.Post.hasMany(this.Comment, {
foreignKey: 'commentable_id',
scope: {
commentable: 'post'
}
});
this.Comment.belongsTo(this.Post, {
foreignKey: 'commentable_id',
as: 'post'
});
this.Image.hasMany(this.Comment, {
foreignKey: 'commentable_id',
scope: {
commentable: 'image'
}
});
this.Comment.belongsTo(this.Image, {
foreignKey: 'commentable_id',
as: 'image'
});
```
Note that the Image -> Comment and Post -> Comment relations define a scope, `commentable: 'image'` and `commentable: 'post'` respectively. This scope is automatically applied when using the association functions:
```js
Image.getComments()
SELECT * FROM comments WHERE commentable_id = 42 AND commentable = 'image';
Image.createComment({
title: 'Awesome!'
})
INSERT INTO comments (title, commentable_id, commentable) VALUES ('Awesome!', 'image', 42);
Image.addComment(comment);
UPDATE comments SET commentable_id = 42, commentable = 'image'
```
The `getItem` utility function on `Comment` completes the picture - it simply converts the `commentable` string into a call to etiher `getImage` or `getPost`, providing an abstraction over whether a comment belongs to a post or an image.
#### n:m
Continuing with the idea of a polymorphic model, consider a tag table - an item can have multiple tags, and a tag can be related to several item
For brevity, the example only shows a Post model, but in reality Tag would be related to several other models.
```js
ItemTag = sequelize.define('item_tag', {
tag_id: {
type: DataTypes.INTEGER,
unique: 'item_tag_taggable'
},
taggable: {
type: DataTypes.STRING,
unique: 'item_tag_taggable'
},
taggable_id: {
type: DataTypes.INTEGER,
unique: 'item_tag_taggable',
references: null
}
});
Tag = sequelize.define('tag', {
name: DataTypes.STRING
});
Post.belongsToMany(Tag, {
through: {
model: ItemTag,
unique: false,
scope: {
taggable: 'post'
}
},
foreignKey: 'taggable_id',
constraints: false
});
Tag.belongsToMany(Post, {
through: {
model: ItemTag,
unique: false
},
foreignKey: 'tag_id'
});
```
Notice that the scoped column (`taggable`) is now on the through model (`ItemTag`).
We could also define a more restrictive association, for example to get all pending tags for a post by applying a scope of both the through model (`ItemTag`) and the target model (`Tag`):
```js
Post.hasMany(Tag, {
through: {
model: ItemTag,
unique: false,
scope: {
taggable: 'post'
}
},
scope: {
status: 'pending'
},
as: 'pendingTags',
foreignKey: 'taggable_id',
constraints: false
});
Post.getPendingTags();
```
```sql
SELECT `tag`.* INNER JOIN `item_tags` AS `item_tag`
ON `tag`.`id` = `item_tag`.`tagId`
AND `item_tag`.`taggable_id` = 42
AND `item_tag`.`taggable` = 'post'
WHERE (`tag`.`status` = 'pending');
```
`constraints: false` disables references constraints on the `taggable_id` column. Because the column is polymorphic, we cannot say that it `REFERENCES` a specific table.
## Naming strategy ## Naming strategy
By default sequelize will use the model name (the name passed to `sequelize.define`) to figure out the name of the model when used in associations. For example, a model named `user` will add the functions `get/set/add User` to instances of the associated model, and a property named `.user` in eager loading, while a model named `User` will add the same functions, but a property named `.User` (notice the upper case U) in eager loading. By default sequelize will use the model name (the name passed to `sequelize.define`) to figure out the name of the model when used in associations. For example, a model named `user` will add the functions `get/set/add User` to instances of the associated model, and a property named `.user` in eager loading, while a model named `User` will add the same functions, but a property named `.User` (notice the upper case U) in eager loading.
As we've already seen, you can alias models in associations using `as`. In single associations (has one and belongs to), the alias should be singular, while for many associations (has many) it should be plural. Sequelize then uses the [inflection ][0]library to convert the alias to its singular form. However, this might not always work for irregular or non-english words. In this case, you can provide both the plural and the singular form of the alias: As we've already seen, you can alias models in associations using `as`. In single associations (has one and belongs to), the alias should be singular, while for many associations (has many) it should be plural. Sequelize then uses the [inflection ][0]library to convert the alias to its singular form. However, this might not always work for irregular or non-english words. In this case, you can provide both the plural and the singular form of the alias:
```js ```js
User.belongsToMany(Project, { as: { singular: 'task', plural: 'tasks' }}) User.belongsToMany(Project, { as: { singular: 'task', plural: 'tasks' }})
// Notice that inflection has no problem singularizing tasks, this is just for illustrative purposes. // Notice that inflection has no problem singularizing tasks, this is just for illustrative purposes.
``` ```
......
# Definition
Scoping allows you to define commonly used queries that you can easily use later. Scopes can include all the same attributes as regular finders, `where`, `include`, `limit` etc.
Scopes are defined in the model definition and can be finder objects, or functions returning finder objects - except for the default scope, which can only be an object:
```js
var Project = sequelize.define('project', {
// Attributes
}, {
defaultScope: {
where: {
active: true
}
},
scopes: {
deleted: {
where: {
deleted: true
}
},
activeUsers: {
include: [
{ model: User, where: { active: true }}
]
}
random: function () {
return {
where: {
someNumber: Math.random()
}
}
},
accessLevel: function (value) {
return {
where: {
accessLevel: {
$gte: value
}
}
}
}
}
});
```
The default scope is always applied. This means, that with the model definition above, `Project.findAll()` will create the following query:
```sql
SELECT * FROM projects WHERE active = true
```
The default scope can be removed by calling `.unscoped()`, `.scope(null)`, or by invoking another scope:
```js
Project.scope('deleted').findAll(); // Removes the default scope
```
```sql
SELECT * FROM projects WHERE deleted = true
```
# Usage
Scopes are applied by calling `.scope` on the model definition, passing the name of one or more scopes. `.scope` returns a fully functional model instance with all the regular methods: `.findAll`, `.update`, `.count`, `.destroy` etc. You can save this model instance and reuse it later:
```js
var DeletedProjects = Project.scope('deleted');
DeletedProjects.findAll();
// some time passes
// let's look for deleted projects again!
DeletedProjects.findAll();
```
Scopes apply to `.find`, `.findAll`, `.count`, `.update` and `.destroy`.
Scopes which are functions can be invoked in two ways. If the scope does not take any arguments it can be invoked as normally. If the scope takes arguments, pass an object:
```js
Project.scope('random', { method: ['accessLevel', 19]}).findAll();
```
```sql
SELECT * FROM projects WHERE someNumber = 42 AND accessLevel >= 19
```
## Merging
Several scopes can be applied simultaneously by passing an array of scopes to `.scope`, or by passing the scopes as consequtive arguments.
```js
// These two are equivalent
Project.scope('deleted', 'activeUsers').findAll();
Project.scope(['deleted', 'activeUsers']).findAll();
```
```sql
SELECT * FROM projects
INNER JOIN users ON projects.userId = users.id
AND users.active = true
```
If you want to apply another scope alongside the default scope, pass the key `defaultScope` to `.scope`:
```js
Project.scope('defaultScope', 'deleted').findAll();
```
```sql
SELECT * FROM projects WHERE active = true AND deleted = true
```
When invoking several scopes, keys from subsequent scopes will overwrite previous ones (similar to [_.assign](https://lodash.com/docs#assign)). Consider two scopes:
```js
{
scope1: {
where: {
firstName: 'bob',
age: {
$gt: 20
}
},
limit: 2
},
scope2: {
where: {
age: {
$gt: 30
}
},
limit: 10
}
}
```
Calling `.scope('scope1', 'scope2')` will yield the following query
```sql
WHERE firstName = 'bob' AND age > 30 LIMIT 10
```
Note how `limit` and `age` are overwritten by `scope2`, whíle `firstName` is preserved. `limit`, `offset`, `order`, `paranoid`, `lock` and `raw` are overwritten, while `where` and `include` are shallowly merged. This means that identical keys in the where objects, and subsequent includes of the same model will both overwrite each other.
The same merge logic applies when passing a find object directly to findAll on a scoped model:
```js
Project.scope('deleted').findAll({
where: {
firstName: 'john'
}
})
```
```sql
WHERE deleted = true AND firstName = 'john'
```
Here the `deleted` scope is merged with the finder. If we were to pass `where: { firstName: 'john', deleted: false }` to the finder, the `deleted` scope would be overwritten.
# Associations
Sequelize has two different but related scope concepts in relation to associations. The difference is subtle but important:
* **Assocation scopes** Allow you to specify default attributes when getting and setting associations - useful when implementing polymorphic associations. This scope is only invoked on the association between the two models, when using the `get`, `set`, `add` and `create` associated model functions
* **Scopes on associated models** Allows you to apply default and other scopes when fetching associations, and allows you to pass a scoped model when creating associtaions. These scopes both apply to regular finds on the model and to find through the association.
As an example, consider the models Post and Comment. Comment is associated to several other models (Image, Video etc.) and the association between Comment and other models is polymorphic, which means that Comment stores a `commentable` column, in addition to the foreign key `commentable_id`.
The polymorphic association can be implemented with an _association scope_ :
```js
this.Post.hasMany(this.Comment, {
foreignKey: 'commentable_id',
scope: {
commentable: 'post'
}
});
```
When calling `post.getComments()`, this will automatically add `WHERE commentable = 'post'`. Similarly, when adding new comments to a post, `commentable` will automagically be set to `'post'`. The association scope is meant to live in the background without the programmer having to worry about it - it cannot be disabled. For a more complete polymorphic example, see [Association scopes](docs/associations/#scopes)
Consider then, that Post has a default scope which only shows active posts: `where: { active: true }`. This scope lives on the associated model (Post), and not on the association like the `commentable` scope did. Just like the default scope is applied when calling `Post.findAll()`, it is also applied when calling `User.getPosts()` - this will only return the active posts for that user.
To disable the default scope, pass `scope: null` to the getter: `User.getPosts({ scope: null })`. Similarly, if you want to apply other scopes, pass an array like you would to `.scope`:
```js
User.getPosts({ scope: ['scope1', 'scope2']});
```
If you want to create a shortcut method to a scope on an associated model, you can pass the scoped model to the association. Consider a shortcut to get all deleted posts for a user:
```js
var Post = sequelize.define('post', attributes, {
defaultScope: {
where: {
active: true
}
},
scopes: {
deleted: {
where: {
deleted: true
}
}
}
});
User.hasMany(Post); // regular getPosts association
User.hasMany(Post.scope('deleted'), { as: 'deletedPosts' });
```
```js
User.getPosts(); // WHERE active = true
User.getDeletedPosts(); // WHERE deleted = true
```
...@@ -310,10 +310,7 @@ module.exports = (function() { ...@@ -310,10 +310,7 @@ module.exports = (function() {
, throughWhere; , throughWhere;
if (association.scope) { if (association.scope) {
scopeWhere = {}; scopeWhere = _.clone(association.scope);
Object.keys(association.scope).forEach(function (attribute) {
scopeWhere[attribute] = association.scope[attribute];
}.bind(this));
} }
options.where = { options.where = {
...@@ -351,7 +348,16 @@ module.exports = (function() { ...@@ -351,7 +348,16 @@ module.exports = (function() {
}); });
} }
return association.target.findAll(options, queryOptions); var model = association.target;
if (options.hasOwnProperty('scope')) {
if (!options.scope) {
model = model.unscoped();
} else {
model = model.scope(options.scope);
}
}
return model.findAll(options, queryOptions);
}; };
obj[this.accessors.hasAll] = function(instances, options) { obj[this.accessors.hasAll] = function(instances, options) {
...@@ -359,21 +365,24 @@ module.exports = (function() { ...@@ -359,21 +365,24 @@ module.exports = (function() {
, where; , where;
options = options || {}; options = options || {};
options.scope = false;
instances.forEach(function(instance) { instances.forEach(function(instance) {
if (instance instanceof association.target.Instance) { if (instance instanceof association.target.Instance) {
where = new Utils.or([where, instance.primaryKeyValues]); where = { $or: [where, instance.primaryKeyValues]};
} else { } else {
var _where = {}; var _where = {};
_where[association.target.primaryKeyAttribute] = instance; _where[association.target.primaryKeyAttribute] = instance;
where = new Utils.or([where, _where]); where = { $or: [where, _where]};
} }
}); });
options.where = new Utils.and([ options.where = {
where, $and: [
options.where where,
]); options.where
]
};
return instance[association.accessors.get]( return instance[association.accessors.get](
options, options,
...@@ -388,6 +397,7 @@ module.exports = (function() { ...@@ -388,6 +397,7 @@ module.exports = (function() {
, where; , where;
options = options || {}; options = options || {};
options.scope = false;
if (param instanceof association.target.Instance) { if (param instanceof association.target.Instance) {
where = param.primaryKeyValues; where = param.primaryKeyValues;
...@@ -396,10 +406,12 @@ module.exports = (function() { ...@@ -396,10 +406,12 @@ module.exports = (function() {
where[association.target.primaryKeyAttribute] = param; where[association.target.primaryKeyAttribute] = param;
} }
options.where = new Utils.and([ options.where = {
where, $and: [
options.where where,
]); options.where
]
};
return instance[association.accessors.get]( return instance[association.accessors.get](
options, options,
...@@ -419,7 +431,9 @@ module.exports = (function() { ...@@ -419,7 +431,9 @@ module.exports = (function() {
options = options || {}; options = options || {};
var instance = this; var instance = this;
return instance[association.accessors.get]({}, { return instance[association.accessors.get]({
scope: false
}, {
transaction: options.transaction, transaction: options.transaction,
logging: options.logging logging: options.logging
}).then(function(oldAssociatedObjects) { }).then(function(oldAssociatedObjects) {
...@@ -668,6 +682,7 @@ module.exports = (function() { ...@@ -668,6 +682,7 @@ module.exports = (function() {
} }
return instance[association.accessors.get]({ return instance[association.accessors.get]({
scope: false,
where: newInstance.primaryKeyValues where: newInstance.primaryKeyValues
}, { }, {
transaction: (additionalAttributes || {}).transaction, transaction: (additionalAttributes || {}).transaction,
......
...@@ -97,20 +97,31 @@ module.exports = (function() { ...@@ -97,20 +97,31 @@ module.exports = (function() {
BelongsTo.prototype.injectGetter = function(instancePrototype) { BelongsTo.prototype.injectGetter = function(instancePrototype) {
var association = this; var association = this;
instancePrototype[this.accessors.get] = function(params) { instancePrototype[this.accessors.get] = function(options) {
var where = {}; var where = {};
where[association.targetIdentifier] = this.get(association.identifier);
params = association.target.__optClone(params) || {}; options = association.target.__optClone(options) || {};
params.where = (params.where && [params.where]) || [];
where[association.targetIdentifier] = this.get(association.identifier); options.where = {
params.where.push(where); $and: [
options.where,
where
]
};
params.where = new Utils.and(params.where); if (options.limit === undefined) options.limit = null;
if (params.limit === undefined) params.limit = null; var model = association.target;
if (options.hasOwnProperty('scope')) {
if (!options.scope) {
model = model.unscoped();
} else {
model = model.scope(options.scope);
}
}
return association.target.find(params); return model.find(options);
}; };
return this; return this;
......
...@@ -18,16 +18,27 @@ module.exports = (function() { ...@@ -18,16 +18,27 @@ module.exports = (function() {
}.bind(this)); }.bind(this));
} }
options.where = new Utils.and([ options.where = {
new Utils.where( $and: [
this.target.rawAttributes[this.association.identifier], new Utils.where(
this.instance[this.source.primaryKeyAttribute] this.target.rawAttributes[this.association.identifier],
), this.instance[this.source.primaryKeyAttribute]
scopeWhere, ),
options.where scopeWhere,
]); options.where
]
return this.association.target.all(options, queryOptions); };
var model = this.association.target;
if (options.hasOwnProperty('scope')) {
if (!options.scope) {
model = model.unscoped();
} else {
model = model.scope(options.scope);
}
}
return model.all(options, queryOptions);
}; };
HasManySingleLinked.prototype.injectSetter = function(oldAssociations, newAssociations, defaultAttributes) { HasManySingleLinked.prototype.injectSetter = function(oldAssociations, newAssociations, defaultAttributes) {
...@@ -66,7 +77,7 @@ module.exports = (function() { ...@@ -66,7 +77,7 @@ module.exports = (function() {
updateWhere = {}; updateWhere = {};
updateWhere[primaryKey] = obsoleteIds; updateWhere[primaryKey] = obsoleteIds;
promises.push(this.association.target.update( promises.push(this.association.target.unscoped().update(
update, update,
Utils._.extend(options, { Utils._.extend(options, {
allowNull: [self.association.identifier], allowNull: [self.association.identifier],
...@@ -100,7 +111,7 @@ module.exports = (function() { ...@@ -100,7 +111,7 @@ module.exports = (function() {
updateWhere[primaryKey] = unassociatedIds; updateWhere[primaryKey] = unassociatedIds;
promises.push(this.association.target.update( promises.push(this.association.target.unscoped().update(
update, update,
Utils._.extend(options, { Utils._.extend(options, {
allowNull: [self.association.identifier], allowNull: [self.association.identifier],
......
...@@ -334,6 +334,7 @@ module.exports = (function() { ...@@ -334,6 +334,7 @@ module.exports = (function() {
obj[this.accessors.get] = function(options, queryOptions) { obj[this.accessors.get] = function(options, queryOptions) {
options = association.target.__optClone(options) || {}; options = association.target.__optClone(options) || {};
queryOptions = queryOptions || {}; queryOptions = queryOptions || {};
var Class = Object(association.through.model) === association.through.model ? HasManyDoubleLinked : HasManySingleLinked; var Class = Object(association.through.model) === association.through.model ? HasManyDoubleLinked : HasManySingleLinked;
return new Class(association, this).injectGetter(options, queryOptions); return new Class(association, this).injectGetter(options, queryOptions);
...@@ -344,21 +345,24 @@ module.exports = (function() { ...@@ -344,21 +345,24 @@ module.exports = (function() {
, where; , where;
options = options || {}; options = options || {};
options.scope = false;
instances.forEach(function(instance) { instances.forEach(function(instance) {
if (instance instanceof association.target.Instance) { if (instance instanceof association.target.Instance) {
where = new Utils.or([where, instance.primaryKeyValues]); where = { $or: [where, instance.primaryKeyValues]};
} else { } else {
var _where = {}; var _where = {};
_where[association.target.primaryKeyAttribute] = instance; _where[association.target.primaryKeyAttribute] = instance;
where = new Utils.or([where, _where]); where = { $or: [where, _where]};
} }
}); });
options.where = new Utils.and([ options.where = {
where, $and: [
options.where where,
]); options.where
]
};
return instance[association.accessors.get]( return instance[association.accessors.get](
options, options,
...@@ -373,6 +377,7 @@ module.exports = (function() { ...@@ -373,6 +377,7 @@ module.exports = (function() {
, where; , where;
options = options || {}; options = options || {};
options.scope = false;
if (param instanceof association.target.Instance) { if (param instanceof association.target.Instance) {
where = param.primaryKeyValues; where = param.primaryKeyValues;
...@@ -381,10 +386,12 @@ module.exports = (function() { ...@@ -381,10 +386,12 @@ module.exports = (function() {
where[association.target.primaryKeyAttribute] = param; where[association.target.primaryKeyAttribute] = param;
} }
options.where = new Utils.and([ options.where = {
where, $and: [
options.where where,
]); options.where
]
};
return instance[association.accessors.get]( return instance[association.accessors.get](
options, options,
...@@ -420,7 +427,9 @@ module.exports = (function() { ...@@ -420,7 +427,9 @@ module.exports = (function() {
var instance = this; var instance = this;
return instance[association.accessors.get]({}, { return instance[association.accessors.get]({
scope: false
}, {
transaction: (additionalAttributes || {}).transaction, transaction: (additionalAttributes || {}).transaction,
logging: (additionalAttributes || {}).logging logging: (additionalAttributes || {}).logging
}).then(function(oldAssociatedObjects) { }).then(function(oldAssociatedObjects) {
...@@ -467,7 +476,8 @@ module.exports = (function() { ...@@ -467,7 +476,8 @@ module.exports = (function() {
} }
return instance[association.accessors.get]({ return instance[association.accessors.get]({
where: newInstance.primaryKeyValues where: newInstance.primaryKeyValues,
scope: false
}, { }, {
transaction: (additionalAttributes || {}).transaction transaction: (additionalAttributes || {}).transaction
}).then(function(currentAssociatedObjects) { }).then(function(currentAssociatedObjects) {
...@@ -483,7 +493,9 @@ module.exports = (function() { ...@@ -483,7 +493,9 @@ module.exports = (function() {
obj[this.accessors.remove] = function(oldAssociatedObject, options) { obj[this.accessors.remove] = function(oldAssociatedObject, options) {
var instance = this; var instance = this;
return instance[association.accessors.get]({}, options).then(function(currentAssociatedObjects) { return instance[association.accessors.get]({
scope: false
}, options).then(function(currentAssociatedObjects) {
var newAssociations = []; var newAssociations = [];
if (!(oldAssociatedObject instanceof association.target.Instance)) { if (!(oldAssociatedObject instanceof association.target.Instance)) {
...@@ -506,7 +518,9 @@ module.exports = (function() { ...@@ -506,7 +518,9 @@ module.exports = (function() {
obj[this.accessors.removeMultiple] = function(oldAssociatedObjects, options) { obj[this.accessors.removeMultiple] = function(oldAssociatedObjects, options) {
var instance = this; var instance = this;
return instance[association.accessors.get]({}, options).then(function(currentAssociatedObjects) { return instance[association.accessors.get]({
scope: false
}, options).then(function(currentAssociatedObjects) {
var newAssociations = []; var newAssociations = [];
// Ensure the oldAssociatedObjects array is an array of target instances // Ensure the oldAssociatedObjects array is an array of target instances
......
...@@ -96,20 +96,31 @@ module.exports = (function() { ...@@ -96,20 +96,31 @@ module.exports = (function() {
HasOne.prototype.injectGetter = function(instancePrototype) { HasOne.prototype.injectGetter = function(instancePrototype) {
var association = this; var association = this;
instancePrototype[this.accessors.get] = function(params) { instancePrototype[this.accessors.get] = function(options) {
var where = {}; var where = {};
where[association.identifier] = this.get(association.sourceIdentifier);
params = association.target.__optClone(params) || {}; options = association.target.__optClone(options) || {};
params.where = (params.where && [params.where]) || [];
where[association.identifier] = this.get(association.sourceIdentifier); options.where = {
params.where.push(where); $and: [
options.where,
where
]
};
params.where = new Utils.and(params.where); if (options.limit === undefined) options.limit = null;
if (params.limit === undefined) params.limit = null; var model = association.target;
if (options.hasOwnProperty('scope')) {
if (!options.scope) {
model = model.unscoped();
} else {
model = model.scope(options.scope);
}
}
return association.target.find(params); return model.find(options);
}; };
return this; return this;
...@@ -121,6 +132,8 @@ module.exports = (function() { ...@@ -121,6 +132,8 @@ module.exports = (function() {
instancePrototype[this.accessors.set] = function(associatedInstance, options) { instancePrototype[this.accessors.set] = function(associatedInstance, options) {
var instance = this; var instance = this;
options = options || {};
options.scope = false;
return instance[association.accessors.get](options).then(function(oldInstance) { return instance[association.accessors.get](options).then(function(oldInstance) {
if (oldInstance) { if (oldInstance) {
oldInstance[association.identifier] = null; oldInstance[association.identifier] = null;
......
...@@ -366,7 +366,7 @@ Mixin.getAssociation = function(target, alias) { ...@@ -366,7 +366,7 @@ Mixin.getAssociation = function(target, alias) {
if (this.associations.hasOwnProperty(associationName)) { if (this.associations.hasOwnProperty(associationName)) {
var association = this.associations[associationName]; var association = this.associations[associationName];
if (association.target === target && (alias === undefined ? !association.isAliased : association.as === alias)) { if (association.target.name === target.name && (alias === undefined ? !association.isAliased : association.as === alias)) {
return association; return association;
} }
} }
......
...@@ -1223,7 +1223,7 @@ module.exports = (function() { ...@@ -1223,7 +1223,7 @@ module.exports = (function() {
} else if (Utils._.isPlainObject(options.where)) { } else if (Utils._.isPlainObject(options.where)) {
options.where['__' + as] = subQueryWhere; options.where['__' + as] = subQueryWhere;
} else { } else {
options.where = new Utils.and(options.where, subQueryWhere); options.where = { $and: [options.where, subQueryWhere] };
} }
} }
...@@ -1533,7 +1533,9 @@ module.exports = (function() { ...@@ -1533,7 +1533,9 @@ module.exports = (function() {
result = (value === 'NULL') ? key + ' IS NULL' : [key, value].join(smth.comparator); result = (value === 'NULL') ? key + ' IS NULL' : [key, value].join(smth.comparator);
} else if (_.isPlainObject(value)) { } else if (_.isPlainObject(value)) {
result = this.plainObjectToWhere(value, key, key, factory).join(' AND '); result = this.whereItemQuery(smth.attribute, value, {
model: factory
});
} else { } else {
if (typeof value === 'boolean') { if (typeof value === 'boolean') {
value = this.booleanValue(value); value = this.booleanValue(value);
...@@ -1726,7 +1728,6 @@ module.exports = (function() { ...@@ -1726,7 +1728,6 @@ module.exports = (function() {
outerBinding = ''; outerBinding = '';
if (key === '$not') outerBinding = 'NOT '; if (key === '$not') outerBinding = 'NOT ';
if (Array.isArray(value)) { if (Array.isArray(value)) {
value = value.map(function (item) { value = value.map(function (item) {
var itemQuery = self.whereItemsQuery(item, options, ' AND '); var itemQuery = self.whereItemsQuery(item, options, ' AND ');
...@@ -2166,83 +2167,6 @@ module.exports = (function() { ...@@ -2166,83 +2167,6 @@ module.exports = (function() {
return [_key, _value].join(' ' + logicResult + ' '); return [_key, _value].join(' ' + logicResult + ' ');
}, },
/*
Takes a hash and transforms it into a mysql where condition: {key: value, key2: value2} ==> key=value AND key2=value2
The values are transformed by the relevant datatype.
*/
hashToWhereConditions: function(hash, dao, options) {
var result = [];
options = options || {};
// Closures are nice
Utils._.each(hash, function(value, key) {
var _key
, _value = null;
if (value && value._isSequelizeMethod === true && (value instanceof Utils.literal)) {
result.push(value.val);
return;
}
if (options.keysEscaped) {
_key = key;
} else {
if (this.isAssociationFilter(key, dao)) {
_key = key = this.getAssociationFilterColumn(key, dao, options);
} else {
_key = this.quoteIdentifiers(key);
}
}
if (Array.isArray(value)) {
result.push(this.arrayValue(value, key, _key, dao, 'IN'));
} else if (value && Utils._.isPlainObject(value)) {
result = result.concat(this.plainObjectToWhere(value, key, _key, dao));
} else {
if (typeof value === 'boolean') {
_value = this.booleanValue(value);
} else {
_value = this.escape(value);
}
result.push((_value === 'NULL') ? _key + ' IS NULL' : [_key, _value].join('='));
}
}.bind(this));
return result.join(' AND ');
},
plainObjectToWhere: function (value, key, _key, dao) {
var _value
, result = [];
if (!!value.join) {
//using as sentinel for join column => value
_value = this.quoteIdentifiers(value.join);
result.push([_key, _value].join('='));
} else {
for (var logic in value) {
var logicResult = Utils.getWhereLogic(logic, value[logic]);
if (logicResult === 'BETWEEN' || logicResult === 'NOT BETWEEN') {
_value = this.escape(value[logic][0]);
var _value2 = this.escape(value[logic][1]);
result.push(' (' + _key + ' ' + logicResult + ' ' + _value + ' AND ' + _value2 + ') ');
} else if (logicResult === 'IN' || logicResult === 'NOT IN' || Array.isArray(value[logic])) {
var values = Array.isArray(value[logic]) ? value[logic] : [value[logic]];
result.push(this.arrayValue(values, key, _key, dao, logicResult));
} else {
_value = this.escape(value[logic]);
result.push([_key, _value].join(' ' + logicResult + ' '));
}
}
}
return result;
},
booleanValue: function(value) { booleanValue: function(value) {
return value; return value;
} }
......
...@@ -481,7 +481,7 @@ module.exports = (function() { ...@@ -481,7 +481,7 @@ module.exports = (function() {
if (!resultMap[itemHash]) { if (!resultMap[itemHash]) {
$parent = resultMap[parentHash]; $parent = resultMap[parentHash];
$lastKeyPrefix = lastKeyPrefix(prevKey); $lastKeyPrefix = lastKeyPrefix(prevKey);
//console.log($parent, prevKey, $lastKeyPrefix);
if (includeMap[prevKey].association.isSingleAssociation) { if (includeMap[prevKey].association.isSingleAssociation) {
$parent[$lastKeyPrefix] = resultMap[itemHash] = values; $parent[$lastKeyPrefix] = resultMap[itemHash] = values;
} else { } else {
...@@ -561,7 +561,7 @@ module.exports = (function() { ...@@ -561,7 +561,7 @@ module.exports = (function() {
if (!resultMap[itemHash]) { if (!resultMap[itemHash]) {
$parent = resultMap[parentHash]; $parent = resultMap[parentHash];
$lastKeyPrefix = lastKeyPrefix(prevKey); $lastKeyPrefix = lastKeyPrefix(prevKey);
//console.log($parent, prevKey, $lastKeyPrefix);
if (includeMap[prevKey].association.isSingleAssociation) { if (includeMap[prevKey].association.isSingleAssociation) {
$parent[$lastKeyPrefix] = resultMap[itemHash] = values; $parent[$lastKeyPrefix] = resultMap[itemHash] = values;
} else { } else {
......
...@@ -680,22 +680,6 @@ module.exports = (function() { ...@@ -680,22 +680,6 @@ module.exports = (function() {
options.type = QueryTypes.SELECT; options.type = QueryTypes.SELECT;
// See if we need to merge options and model.scopeObj
// we're doing this on the QueryInterface level because it's a bridge between
// sequelize and the databases
if (model.options.defaultScope && Object.keys(model.options.defaultScope).length > 0) {
if (!!options) {
Utils.injectScope.call(model, options, true);
}
var scopeObj = buildScope.call(model);
Object.keys(scopeObj).forEach(function(method) {
if (typeof scopeObj[method] === 'number' || !Utils._.isEmpty(scopeObj[method])) {
options[method] = scopeObj[method];
}
});
}
options.instance = model; options.instance = model;
return this.sequelize.query( return this.sequelize.query(
this.QueryGenerator.selectQuery(tableName, options, model), this.QueryGenerator.selectQuery(tableName, options, model),
...@@ -935,16 +919,5 @@ module.exports = (function() { ...@@ -935,16 +919,5 @@ module.exports = (function() {
return this.sequelize.query(sql, options); return this.sequelize.query(sql, options);
}; };
// private
var buildScope = function() {
var smart;
// Use smartWhere to convert several {where} objects into a single where object
smart = Utils.smartWhere(this.scopeObj.where || [], this.daoFactoryManager.sequelize.options.dialect);
smart = Utils.compileSmartWhere.call(this, smart, this.daoFactoryManager.sequelize.options.dialect);
return {limit: this.scopeObj.limit || null, offset: this.scopeObj.offset || null, where: smart, order: (this.scopeObj.order || []).join(', ')};
};
return QueryInterface; return QueryInterface;
})(); })();
...@@ -568,7 +568,8 @@ module.exports = (function() { ...@@ -568,7 +568,8 @@ module.exports = (function() {
delete options.modelName; delete options.modelName;
var factory = new Model(modelName, attributes, options); var factory = new Model(modelName, attributes, options);
this.modelManager.addDAO(factory.init(this.modelManager)); factory = factory.init(this.modelManager);
this.modelManager.addDAO(factory);
this.runHooks('afterDefine', factory); this.runHooks('afterDefine', factory);
......
...@@ -16,7 +16,9 @@ pages: ...@@ -16,7 +16,9 @@ pages:
- ['docs/getting-started.md', 'Documentation', 'Getting Started'] - ['docs/getting-started.md', 'Documentation', 'Getting Started']
- ['docs/schema.md', 'Documentation', 'Working with table schemas'] - ['docs/schema.md', 'Documentation', 'Working with table schemas']
#- ['docs/usage.md', 'Documentation', 'Usage'] #- ['docs/usage.md', 'Documentation', 'Usage']
- ['docs/models.md', 'Documentation', 'Models'] - ['docs/models-definition.md', 'Documentation', 'Models - Definition']
- ['docs/models-usage.md', 'Documentation', 'Models - Usage']
- ['docs/scopes.md', 'Documentation', 'Scopes']
- ['docs/instances.md', 'Documentation', 'Instances'] - ['docs/instances.md', 'Documentation', 'Instances']
- ['docs/associations.md', 'Documentation', 'Relations/Associations'] - ['docs/associations.md', 'Documentation', 'Relations/Associations']
- ['docs/hooks.md', 'Documentation', 'Hooks'] - ['docs/hooks.md', 'Documentation', 'Hooks']
......
...@@ -290,7 +290,7 @@ if (Support.dialectIsMySQL()) { ...@@ -290,7 +290,7 @@ if (Support.dialectIsMySQL()) {
) )
}; };
}], }],
expectation: "SELECT * FROM `myTable` WHERE (`myTable`.`archived` IS NULL AND COALESCE(`place_type_codename`, `announcement_type_codename`) IN ('Lost','Found'));", expectation: "SELECT * FROM `myTable` WHERE (`myTable`.`archived` IS NULL AND COALESCE(`place_type_codename`, `announcement_type_codename`) IN ('Lost', 'Found'));",
context: QueryGenerator, context: QueryGenerator,
needsSequelize: true needsSequelize: true
}, { }, {
...@@ -564,38 +564,6 @@ if (Support.dialectIsMySQL()) { ...@@ -564,38 +564,6 @@ if (Support.dialectIsMySQL()) {
arguments: ['User', ['foo', 'bar']], arguments: ['User', ['foo', 'bar']],
expectation: 'DROP INDEX user_foo_bar ON `User`' expectation: 'DROP INDEX user_foo_bar ON `User`'
} }
],
hashToWhereConditions: [
{
arguments: [{ id: [1, 2, 3] }],
expectation: '`id` IN (1,2,3)'
},
{
arguments: [{ id: [] }],
expectation: '`id` IN (NULL)'
},
{
arguments: [{ maple: false, bacon: true }],
expectation: '`maple`=false AND `bacon`=true'
},
{
arguments: [{ beaver: [false, true] }],
expectation: '`beaver` IN (false,true)'
},
{
arguments: [{birthday: new Date(Date.UTC(2011, 6, 1, 10, 1, 55))}],
expectation: "`birthday`='2011-07-01 10:01:55'"
},
{
arguments: [{ birthday: new Date(Date.UTC(2011, 6, 1, 10, 1, 55)),
otherday: new Date(Date.UTC(2013, 6, 2, 10, 1, 22)) }],
expectation: "`birthday`='2011-07-01 10:01:55' AND `otherday`='2013-07-02 10:01:22'"
},
{
arguments: [{ birthday: [new Date(Date.UTC(2011, 6, 1, 10, 1, 55)), new Date(Date.UTC(2013, 6, 2, 10, 1, 22))] }],
expectation: "`birthday` IN ('2011-07-01 10:01:55','2013-07-02 10:01:22')"
}
] ]
}; };
......
...@@ -315,7 +315,7 @@ if (dialect.match(/^postgres/)) { ...@@ -315,7 +315,7 @@ if (dialect.match(/^postgres/)) {
) )
}; };
}], }],
expectation: 'SELECT * FROM "myTable" WHERE ("myTable"."archived" IS NULL AND COALESCE("place_type_codename", "announcement_type_codename") IN (\'Lost\',\'Found\'));', expectation: 'SELECT * FROM "myTable" WHERE ("myTable"."archived" IS NULL AND COALESCE("place_type_codename", "announcement_type_codename") IN (\'Lost\', \'Found\'));',
context: QueryGenerator, context: QueryGenerator,
needsSequelize: true needsSequelize: true
}, { }, {
...@@ -939,47 +939,6 @@ if (dialect.match(/^postgres/)) { ...@@ -939,47 +939,6 @@ if (dialect.match(/^postgres/)) {
expectation: 'DROP INDEX IF EXISTS mySchema.user_foo_bar', expectation: 'DROP INDEX IF EXISTS mySchema.user_foo_bar',
context: {options: {quoteIdentifiers: false}} context: {options: {quoteIdentifiers: false}}
} }
],
hashToWhereConditions: [
{
arguments: [{ id: [1, 2, 3] }],
expectation: '\"id\" IN (1,2,3)'
},
{
arguments: [{ id: [] }],
expectation: '\"id\" IN (NULL)'
},
{
arguments: [{id: {not: [1, 2, 3] }}],
expectation: '\"id\" NOT IN (1,2,3)'
},
{
arguments: [{id: {not: [] }}],
expectation: '\"id\" NOT IN (NULL)'
},
// Variants when quoteIdentifiers is false
{
arguments: [{ id: [1, 2, 3] }],
expectation: 'id IN (1,2,3)',
context: {options: {quoteIdentifiers: false}}
},
{
arguments: [{ id: [] }],
expectation: 'id IN (NULL)',
context: {options: {quoteIdentifiers: false}}
},
{
arguments: [{ id: {not: [1, 2, 3] }}],
expectation: 'id NOT IN (1,2,3)',
context: {options: {quoteIdentifiers: false}}
},
{
arguments: [{ id: {not: [] }}],
expectation: 'id NOT IN (NULL)',
context: {options: {quoteIdentifiers: false}}
}
] ]
}; };
......
...@@ -368,8 +368,8 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -368,8 +368,8 @@ describe(Support.getTestDialectTeaser('Model'), function() {
return self.Environment.find({ return self.Environment.find({
where: { name: 'environment' }, where: { name: 'environment' },
include: [ include: [
{ daoFactory: self.Domain, as: 'PrivateDomain' }, { model: self.Domain, as: 'PrivateDomain' },
{ daoFactory: self.Domain, as: 'PublicDomain' } { model: self.Domain, as: 'PublicDomain' }
] ]
}).then(function(environment) { }).then(function(environment) {
expect(environment).to.exist; expect(environment).to.exist;
...@@ -559,7 +559,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -559,7 +559,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('throws an error if alias is not associated', function() { it('throws an error if alias is not associated', function() {
var self = this; var self = this;
return self.Worker.find({ include: [{ daoFactory: self.Task, as: 'Work' }] }).catch (function(err) { return self.Worker.find({ include: [{ model: self.Task, as: 'Work' }] }).catch (function(err) {
expect(err.message).to.equal('Task (Work) is not associated to Worker!'); expect(err.message).to.equal('Task (Work) is not associated to Worker!');
}); });
}); });
...@@ -567,7 +567,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -567,7 +567,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('returns the associated task via worker.task', function() { it('returns the associated task via worker.task', function() {
return this.Worker.find({ return this.Worker.find({
where: { name: 'worker' }, where: { name: 'worker' },
include: [{ daoFactory: this.Task, as: 'ToDo' }] include: [{ model: this.Task, as: 'ToDo' }]
}).then(function(worker) { }).then(function(worker) {
expect(worker).to.exist; expect(worker).to.exist;
expect(worker.ToDo).to.exist; expect(worker.ToDo).to.exist;
...@@ -648,7 +648,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -648,7 +648,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
where: { where: {
name: 'Boris' name: 'Boris'
}, },
include: [self.PhoneNumber, { daoFactory: self.Photo, as: 'Photos' }] include: [self.PhoneNumber, { model: self.Photo, as: 'Photos' }]
}).then(function(fetchedContact) { }).then(function(fetchedContact) {
expect(fetchedContact).to.exist; expect(fetchedContact).to.exist;
expect(fetchedContact.Photos.length).to.equal(1); expect(fetchedContact.Photos.length).to.equal(1);
...@@ -720,7 +720,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -720,7 +720,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('throws an error if alias is not associated', function() { it('throws an error if alias is not associated', function() {
var self = this; var self = this;
return self.Worker.find({ include: [{ daoFactory: self.Task, as: 'Work' }] }).catch (function(err) { return self.Worker.find({ include: [{ model: self.Task, as: 'Work' }] }).catch (function(err) {
expect(err.message).to.equal('Task (Work) is not associated to Worker!'); expect(err.message).to.equal('Task (Work) is not associated to Worker!');
}); });
}); });
...@@ -728,7 +728,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -728,7 +728,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('returns the associated task via worker.task', function() { it('returns the associated task via worker.task', function() {
return this.Worker.find({ return this.Worker.find({
where: { name: 'worker' }, where: { name: 'worker' },
include: [{ daoFactory: this.Task, as: 'ToDos' }] include: [{ model: this.Task, as: 'ToDos' }]
}).then(function(worker) { }).then(function(worker) {
expect(worker).to.exist; expect(worker).to.exist;
expect(worker.ToDos).to.exist; expect(worker.ToDos).to.exist;
......
...@@ -630,7 +630,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -630,7 +630,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('throws an error if alias is not associated', function() { it('throws an error if alias is not associated', function() {
var self = this; var self = this;
return self.Worker.findAll({ include: [{ daoFactory: self.Task, as: 'Work' }] }).catch (function(err) { return self.Worker.findAll({ include: [{ model: self.Task, as: 'Work' }] }).catch (function(err) {
expect(err.message).to.equal('Task (Work) is not associated to Worker!'); expect(err.message).to.equal('Task (Work) is not associated to Worker!');
}); });
}); });
...@@ -638,7 +638,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -638,7 +638,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('returns the associated task via worker.task', function() { it('returns the associated task via worker.task', function() {
return this.Worker.findAll({ return this.Worker.findAll({
where: { name: 'worker' }, where: { name: 'worker' },
include: [{ daoFactory: this.Task, as: 'ToDo' }] include: [{ model: this.Task, as: 'ToDo' }]
}).then(function(workers) { }).then(function(workers) {
expect(workers).to.exist; expect(workers).to.exist;
expect(workers[0].ToDo).to.exist; expect(workers[0].ToDo).to.exist;
...@@ -722,7 +722,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -722,7 +722,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('throws an error if alias is not associated', function() { it('throws an error if alias is not associated', function() {
var self = this; var self = this;
return self.Worker.findAll({ include: [{ daoFactory: self.Task, as: 'Work' }] }).catch (function(err) { return self.Worker.findAll({ include: [{ model: self.Task, as: 'Work' }] }).catch (function(err) {
expect(err.message).to.equal('Task (Work) is not associated to Worker!'); expect(err.message).to.equal('Task (Work) is not associated to Worker!');
}); });
}); });
...@@ -730,7 +730,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -730,7 +730,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('returns the associated task via worker.task', function() { it('returns the associated task via worker.task', function() {
return this.Worker.findAll({ return this.Worker.findAll({
where: { name: 'worker' }, where: { name: 'worker' },
include: [{ daoFactory: this.Task, as: 'ToDos' }] include: [{ model: this.Task, as: 'ToDos' }]
}).then(function(workers) { }).then(function(workers) {
expect(workers).to.exist; expect(workers).to.exist;
expect(workers[0].ToDos).to.exist; expect(workers[0].ToDos).to.exist;
...@@ -741,7 +741,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -741,7 +741,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('returns the associated task via worker.task when daoFactory is aliased with model', function() { it('returns the associated task via worker.task when daoFactory is aliased with model', function() {
return this.Worker.findAll({ return this.Worker.findAll({
where: { name: 'worker' }, where: { name: 'worker' },
include: [{ daoFactory: this.Task, as: 'ToDos' }] include: [{ model: this.Task, as: 'ToDos' }]
}).then(function(workers) { }).then(function(workers) {
expect(workers[0].ToDos[0].title).to.equal('homework'); expect(workers[0].ToDos[0].title).to.equal('homework');
}); });
......
'use strict';
/* jshint -W030 */
/* jshint -W110 */
var chai = require('chai')
, Sequelize = require('../../../../index')
, expect = chai.expect
, Promise = Sequelize.Promise
, Support = require(__dirname + '/../../support');
describe(Support.getTestDialectTeaser('Model'), function() {
describe('scope', function () {
describe('associations', function () {
beforeEach(function () {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
access_level: Sequelize.INTEGER,
other_value: Sequelize.INTEGER,
parent_id: Sequelize.INTEGER
}, {
defaultScope: {
where: {
access_level: {
gte: 5
}
}
},
scopes: {
isTony: {
where: {
username: 'tony'
}
},
}
});
this.Project = this.sequelize.define('project');
this.Company = this.sequelize.define('company', {
active: Sequelize.BOOLEAN
}, {
defaultScope: {
where: { active: true }
},
scopes: {
notActive: {
where: {
active: false
}
},
reversed: {
order: [['id', 'DESC']]
}
}
});
this.Profile = this.sequelize.define('profile', {
active: Sequelize.BOOLEAN
}, {
defaultScope: {
where: { active: true }
},
scopes: {
notActive: {
where: {
active: false
}
},
}
});
this.Project.belongsToMany(this.Company, { through: 'CompanyProjects' });
this.Company.belongsToMany(this.Project, { through: 'CompanyProjects' });
this.ScopeMe.hasOne(this.Profile, { foreignKey: 'userId' });
this.ScopeMe.belongsTo(this.Company);
this.UserAssociation = this.Company.hasMany(this.ScopeMe, { as: 'users'});
return this.sequelize.sync({force: true}).bind(this).then(function() {
return Promise.all([
this.ScopeMe.create({ id: 1, username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10, parent_id: 1}),
this.ScopeMe.create({ id: 2, username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11, parent_id: 2}),
this.ScopeMe.create({ id: 3, username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7, parent_id: 1}),
this.ScopeMe.create({ id: 4, username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7, parent_id: 1}),
this.Company.create({ id: 1, active: true}),
this.Company.create({ id: 2, active: false}),
this.ScopeMe.create({ id: 5, username: 'bob', email: 'bob@foobar.com', access_level: 1, other_value: 9, parent_id: 5}),
]);
}).spread(function (u1, u2, u3, u4, c1, c2, u5) {
return Promise.all([
c1.setUsers([u1, u2, u3, u4]),
c2.setUsers([u5]),
]);
});
});
describe('include', function () {
it('should scope columns properly', function () {
// Will error with ambigous column if id is not scoped properly to `Company`.`id`
return expect(this.Company.findAll({
where: { id: 1 },
include: [this.UserAssociation]
})).not.to.be.rejected;
});
it('should apply default scope when including an associations', function () {
return this.Company.findAll({
include: [this.UserAssociation]
}).get(0).then(function (company) {
expect(company.users).to.have.length(2);
});
});
it('should apply default scope when including a model', function () {
return this.Company.findAll({
include: [{ model: this.ScopeMe, as: 'users'}]
}).get(0).then(function (company) {
expect(company.users).to.have.length(2);
});
});
it('should be able to include a scoped model', function () {
return this.Company.findAll({
include: [{ model: this.ScopeMe.scope('isTony'), as: 'users'}]
}).get(0).then(function (company) {
expect(company.users).to.have.length(1);
expect(company.users[0].get('username')).to.equal('tony');
});
});
});
describe('get', function () {
beforeEach(function () {
return Promise.all([
this.Project.create(),
this.Company.unscoped().findAll()
]).spread(function (p, companies) {
return p.setCompanies(companies);
});
});
describe('it should be able to unscope', function () {
it('hasMany', function () {
return this.Company.find(1).then(function (company) {
return company.getUsers({ scope: false});
}).then(function (users) {
expect(users).to.have.length(4);
});
});
it('hasOne', function () {
return this.Profile.create({
active: false,
userId: 1
}).bind(this).then(function () {
return this.ScopeMe.find(1);
}).then(function (user) {
return user.getProfile({ scope: false });
}).then(function (project) {
expect(project).to.be.ok;
});
});
it('belongsTo', function () {
return this.ScopeMe.unscoped().find({ where: { username: 'bob' }}).then(function (user) {
return user.getCompany({ scope: false });
}).then(function (company) {
expect(company).to.be.ok;
});
});
it('belongsToMany', function () {
return this.Project.findAll().get(0).then(function (p) {
return p.getCompanies({ scope: false});
}).then(function (companies) {
expect(companies).to.have.length(2);
});
});
});
describe('it should apply default scope', function () {
it('hasMany', function () {
return this.Company.find(1).then(function (company) {
return company.getUsers();
}).then(function (users) {
expect(users).to.have.length(2);
});
});
it('hasOne', function () {
return this.Profile.create({
active: false,
userId: 1
}).bind(this).then(function () {
return this.ScopeMe.find(1);
}).then(function (user) {
return user.getProfile();
}).then(function (project) {
expect(project).not.to.be.ok;
});
});
it('belongsTo', function () {
return this.ScopeMe.unscoped().find({ where: { username: 'bob' }}).then(function (user) {
return user.getCompany();
}).then(function (company) {
expect(company).not.to.be.ok;
});
});
it('belongsToMany', function () {
return this.Project.findAll().get(0).then(function (p) {
return p.getCompanies();
}).then(function (companies) {
expect(companies).to.have.length(1);
expect(companies[0].get('active')).to.be.ok;
});
});
});
describe('it should be able to apply another scope', function () {
it('hasMany', function () {
return this.Company.find(1).then(function (company) {
return company.getUsers({ scope: 'isTony'});
}).then(function (users) {
expect(users).to.have.length(1);
expect(users[0].get('username')).to.equal('tony');
});
});
it('hasOne', function () {
return this.Profile.create({
active: true,
userId: 1
}).bind(this).then(function () {
return this.ScopeMe.find(1);
}).then(function (user) {
return user.getProfile({ scope: 'notActive' });
}).then(function (project) {
expect(project).not.to.be.ok;
});
});
it('belongsTo', function () {
return this.ScopeMe.unscoped().find({ where: { username: 'bob' }}).then(function (user) {
return user.getCompany({ scope: 'notActive' });
}).then(function (company) {
expect(company).to.be.ok;
});
});
it('belongsToMany', function () {
return this.Project.findAll().get(0).then(function (p) {
return p.getCompanies({ scope: 'reversed' });
}).then(function (companies) {
expect(companies).to.have.length(2);
expect(companies[0].id).to.equal(2);
expect(companies[1].id).to.equal(1);
});
});
});
});
});
});
});
'use strict';
/* jshint -W030 */
/* jshint -W110 */
var chai = require('chai')
, Sequelize = require('../../../../index')
, expect = chai.expect
, Support = require(__dirname + '/../../support');
describe(Support.getTestDialectTeaser('Model'), function() {
describe('scope', function () {
describe('count', function () {
beforeEach(function () {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
access_level: Sequelize.INTEGER,
other_value: Sequelize.INTEGER
}, {
defaultScope: {
where: {
access_level: {
gte: 5
}
}
},
scopes: {
lowAccess: {
where: {
access_level: {
lte: 5
}
}
}
}
});
return this.sequelize.sync({force: true}).then(function() {
var records = [
{username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7},
{username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11},
{username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10},
{username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7}
];
return this.ScopeMe.bulkCreate(records);
}.bind(this));
});
it('should apply defaultScope', function () {
return expect(this.ScopeMe.count()).to.eventually.equal(2);
});
it('should be able to override default scope', function () {
return expect(this.ScopeMe.count({ where: { access_level: { gt: 5 }}})).to.eventually.equal(1);
});
it('should be able to unscope', function () {
return expect(this.ScopeMe.unscoped().count()).to.eventually.equal(4);
});
it('should be able to apply other scopes', function () {
return expect(this.ScopeMe.scope('lowAccess').count()).to.eventually.equal(3);
});
it('should be able to merge scopes with where', function () {
return expect(this.ScopeMe.scope('lowAccess').count({ where: { username: 'dan'}})).to.eventually.equal(1);
});
});
});
});
'use strict';
/* jshint -W030 */
/* jshint -W110 */
var chai = require('chai')
, Sequelize = require('../../../../index')
, expect = chai.expect
, Support = require(__dirname + '/../../support');
describe(Support.getTestDialectTeaser('Model'), function() {
describe('scope', function () {
describe('destroy', function () {
beforeEach(function () {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
access_level: Sequelize.INTEGER,
other_value: Sequelize.INTEGER
}, {
defaultScope: {
where: {
access_level: {
gte: 5
}
}
},
scopes: {
lowAccess: {
where: {
access_level: {
lte: 5
}
}
}
}
});
return this.sequelize.sync({force: true}).then(function() {
var records = [
{username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7},
{username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11},
{username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10},
{username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7}
];
return this.ScopeMe.bulkCreate(records);
}.bind(this));
});
it('should apply defaultScope', function () {
return this.ScopeMe.destroy({ where: {}}).bind(this).then(function () {
return this.ScopeMe.unscoped().findAll();
}).then(function (users) {
expect(users).to.have.length(2);
expect(users[0].get('username')).to.equal('tony');
expect(users[1].get('username')).to.equal('fred');
});
});
it('should be able to override default scope', function () {
return this.ScopeMe.destroy({ where: { access_level: { lt: 5 }}}).bind(this).then(function () {
return this.ScopeMe.unscoped().findAll();
}).then(function (users) {
expect(users).to.have.length(2);
expect(users[0].get('username')).to.equal('tobi');
expect(users[1].get('username')).to.equal('dan');
});
});
it('should be able to unscope destroy', function () {
return this.ScopeMe.unscoped().destroy({ where: {}}).bind(this).then(function() {
return expect(this.ScopeMe.unscoped().findAll()).to.eventually.have.length(0);
});
});
it('should be able to apply other scopes', function () {
return this.ScopeMe.scope('lowAccess').destroy({ where: {}}).bind(this).then(function () {
return this.ScopeMe.unscoped().findAll();
}).then(function (users) {
expect(users).to.have.length(1);
expect(users[0].get('username')).to.equal('tobi');
});
});
it('should be able to merge scopes with where', function () {
return this.ScopeMe.scope('lowAccess').destroy({ where: { username: 'dan'}}).bind(this).then(function () {
return this.ScopeMe.unscoped().findAll();
}).then(function (users) {
expect(users).to.have.length(3);
expect(users[0].get('username')).to.equal('tony');
expect(users[1].get('username')).to.equal('tobi');
expect(users[2].get('username')).to.equal('fred');
});
});
});
});
});
'use strict';
/* jshint -W030 */
/* jshint -W110 */
var chai = require('chai')
, Sequelize = require('../../../../index')
, expect = chai.expect
, Support = require(__dirname + '/../../support');
describe(Support.getTestDialectTeaser('Model'), function() {
describe('scopes', function() {
beforeEach(function() {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
access_level: Sequelize.INTEGER,
other_value: Sequelize.INTEGER,
parent_id: Sequelize.INTEGER
}, {
defaultScope: {
where: {
access_level: {
gte: 5
}
}
},
scopes: {
highValue: {
where: {
other_value: {
gte: 10
}
}
},
andScope: {
where: {
$and: [
{
email: {
like: '%@sequelizejs.com'
}
},
{ access_level : 3 }
]
}
}
}
});
return this.sequelize.sync({force: true}).then(function() {
var records = [
{username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7, parent_id: 1},
{username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11, parent_id: 2},
{username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10, parent_id: 1},
{username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7, parent_id: 1}
];
return this.ScopeMe.bulkCreate(records);
}.bind(this));
});
it('should be able use where in scope', function() {
return this.ScopeMe.scope({where: { parent_id: 2 }}).findAll().then(function(users) {
expect(users).to.have.length(1);
expect(users[0].username).to.equal('tobi');
});
});
it('should be able to combine scope and findAll where clauses', function() {
return this.ScopeMe.scope({where: { parent_id: 1 }}).findAll({ where: {access_level: 3}}).then(function(users) {
expect(users).to.have.length(2);
expect(['tony', 'fred'].indexOf(users[0].username) !== -1).to.be.true;
expect(['tony', 'fred'].indexOf(users[1].username) !== -1).to.be.true;
});
});
it('should be able to use a defaultScope if declared', function() {
return this.ScopeMe.all().then(function(users) {
expect(users).to.have.length(2);
expect([10, 5].indexOf(users[0].access_level) !== -1).to.be.true;
expect([10, 5].indexOf(users[1].access_level) !== -1).to.be.true;
expect(['dan', 'tobi'].indexOf(users[0].username) !== -1).to.be.true;
expect(['dan', 'tobi'].indexOf(users[1].username) !== -1).to.be.true;
});
});
it('should be able to handle $and in scopes', function () {
return this.ScopeMe.scope('andScope').findAll().then(function(users) {
expect(users).to.have.length(1);
expect(users[0].username).to.equal('tony');
});
});
describe('should not overwrite', function() {
it('default scope with values from previous finds', function() {
return this.ScopeMe.findAll({ where: { other_value: 10 }}).bind(this).then(function(users) {
expect(users).to.have.length(1);
return this.ScopeMe.findAll();
}).then(function(users) {
// This should not have other_value: 10
expect(users).to.have.length(2);
});
});
it('other scopes with values from previous finds', function() {
return this.ScopeMe.scope('highValue').findAll({ where: { access_level: 10 }}).bind(this).then(function(users) {
expect(users).to.have.length(1);
return this.ScopeMe.scope('highValue').findAll();
}).then(function(users) {
// This should not have other_value: 10
expect(users).to.have.length(2);
});
});
});
it('should have no problem performing findOrCreate', function() {
return this.ScopeMe.findOrCreate({ where: {username: 'fake'}}).spread(function(user) {
expect(user.username).to.equal('fake');
});
});
});
});
'use strict';
/* jshint -W030 */
/* jshint -W110 */
var chai = require('chai')
, _ = require('lodash')
, Sequelize = require('../../../../index')
, expect = chai.expect
, Support = require(__dirname + '/../../support');
describe(Support.getTestDialectTeaser('Model'), function() {
describe('scope', function () {
describe('update', function () {
beforeEach(function () {
this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING,
email: Sequelize.STRING,
access_level: Sequelize.INTEGER,
other_value: Sequelize.INTEGER
}, {
defaultScope: {
where: {
access_level: {
gte: 5
}
}
},
scopes: {
lowAccess: {
where: {
access_level: {
lte: 5
}
}
}
}
});
return this.sequelize.sync({force: true}).then(function() {
var records = [
{username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7},
{username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11},
{username: 'dan', email: 'dan@sequelizejs.com', access_level: 5, other_value: 10},
{username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7}
];
return this.ScopeMe.bulkCreate(records);
}.bind(this));
});
it('should apply defaultScope', function () {
return this.ScopeMe.update({ username: 'ruben' }, { where: {}}).bind(this).then(function () {
return this.ScopeMe.unscoped().findAll({ where: { username: 'ruben' }});
}).then(function (users) {
expect(users).to.have.length(2);
expect(users[0].get('email')).to.equal('tobi@fakeemail.com');
expect(users[1].get('email')).to.equal('dan@sequelizejs.com');
});
});
it('should be able to override default scope', function () {
return this.ScopeMe.update({ username: 'ruben' }, { where: { access_level: { lt: 5 }}}).bind(this).then(function () {
return this.ScopeMe.unscoped().findAll({ where: { username: 'ruben' }});
}).then(function (users) {
expect(users).to.have.length(2);
expect(users[0].get('email')).to.equal('tony@sequelizejs.com');
expect(users[1].get('email')).to.equal('fred@foobar.com');
});
});
it('should be able to unscope destroy', function () {
return this.ScopeMe.unscoped().update({ username: 'ruben' }, { where: {}}).bind(this).then(function() {
return this.ScopeMe.unscoped().findAll();
}).then(function (rubens) {
expect(_.every(rubens, function (r) {
return r.get('username') === 'ruben';
})).to.be.true;
});
});
it('should be able to apply other scopes', function () {
return this.ScopeMe.scope('lowAccess').update({ username: 'ruben' }, { where: {}}).bind(this).then(function () {
return this.ScopeMe.unscoped().findAll({ where: { username: { $ne: 'ruben' }}});
}).then(function (users) {
expect(users).to.have.length(1);
expect(users[0].get('email')).to.equal('tobi@fakeemail.com');
});
});
it('should be able to merge scopes with where', function () {
return this.ScopeMe.scope('lowAccess').update({ username: 'ruben' }, { where: { username: 'dan'}}).bind(this).then(function () {
return this.ScopeMe.unscoped().findAll({ where: { username: 'ruben' }});
}).then(function (users) {
expect(users).to.have.length(1);
expect(users[0].get('email')).to.equal('dan@sequelizejs.com');
});
});
});
});
});
'use strict';
/* jshint -W030 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, current = Support.sequelize;
describe(Support.getTestDialectTeaser('Model'), function() {
var Project = current.define('project')
, User = current.define('user')
, Company;
var scopes = {
somethingTrue: {
where: {
something: true,
somethingElse: 42
},
limit: 5
},
somethingFalse: {
where: {
something: false
}
},
users: {
include: [
{ model: User }
]
},
alsoUsers: {
include: [
{ model: User, where: { something: 42}}
]
},
projects: {
include: [Project]
},
noArgs: function () {
// This does not make much sense, since it does not actually need to be in a function,
// In reality it could be used to do for example new Date or random in the scope - but we want it deterministic
return {
where: {
other_value: 7
}
};
},
actualValue: function(value) {
return {
where: {
other_value: value
}
};
},
};
Company = current.define('company', {}, {
defaultScope: {
where: { active: true }
},
scopes: scopes
});
describe('.scope', function () {
it('should apply default scope', function () {
expect(Company.$scope).to.deep.equal({ where: { active: true }});
});
it('should be able to unscope', function () {
expect(Company.scope(null).$scope).to.be.empty;
expect(Company.unscoped().$scope).to.be.empty;
});
it('should be able to merge scopes', function() {
expect(Company.scope('somethingTrue', 'somethingFalse').$scope).to.deep.equal({
where: {
something: false,
somethingElse: 42
},
limit: 5
});
});
it('should support multiple, coexistent scoped models', function () {
var scoped1 = Company.scope('somethingTrue')
, scoped2 = Company.scope('somethingFalse');
expect(scoped1.$scope).to.deep.equal(scopes.somethingTrue);
expect(scoped2.$scope).to.deep.equal(scopes.somethingFalse);
});
it('should work with function scopes', function () {
expect(Company.scope({method: ['actualValue', 11]}).$scope).to.deep.equal({
where: {
other_value: 11
}
});
expect(Company.scope('noArgs').$scope).to.deep.equal({
where: {
other_value: 7
}
});
});
it('should be able to merge two scoped includes', function () {
expect(Company.scope('users', 'projects').$scope).to.deep.equal({
include: [
{ model: User },
{ model: Project }
]
});
});
it('should be able to override the default scope', function() {
expect(Company.scope('somethingTrue').$scope).to.deep.equal(scopes.somethingTrue);
});
it('should be able to combine default with another scope', function () {
expect(Company.scope(['defaultScope', {method: ['actualValue', 11]}]).$scope).to.deep.equal({
where: {
active: true,
other_value: 11
}
});
});
it('should emit an error for scopes that dont exist', function() {
expect(function () {
Company.scope('doesntexist');
}).to.throw('Invalid scope doesntexist called.');
});
});
describe('$injectScope', function () {
it('should be able to merge scope and where', function () {
var scope = {
where: {
something: true,
somethingElse: 42
},
limit: 15,
offset: 3
};
var options = {
where: {
something: false
},
limit: 9
};
current.Model.$injectScope(scope, options);
expect(options).to.deep.equal({
where: {
something: false,
somethingElse: 42
},
limit: 9,
offset: 3
});
});
it('should be able to overwrite multiple scopes with the same include', function () {
var scope = {
include: [
{ model: Project, where: { something: false }},
{ model: Project, where: { something: true }}
]
};
var options = {};
current.Model.$injectScope(scope, options);
expect(options.include).to.have.length(1);
expect(options.include[0]).to.deep.equal({ model: Project, where: { something: true }});
});
it('should be able to override scoped include', function () {
var scope = {
include: [{ model: Project, where: { something: false }}]
};
var options = {
include: [{ model: Project, where: { something: true }}]
};
current.Model.$injectScope(scope, options);
expect(options.include).to.have.length(1);
expect(options.include[0]).to.deep.equal({ model: Project, where: { something: true }});
});
it('should be able to merge scoped include with include in find', function () {
var scope = {
include: [
{ model: Project, where: { something: false }}
]
};
var options = {
include: [
{ model: User, where: { something: true }}
]
};
current.Model.$injectScope(scope, options);
expect(options.include).to.have.length(2);
expect(options.include[0]).to.deep.equal({ model: User, where: { something: true }});
expect(options.include[1]).to.deep.equal({ model: Project, where: { something: false }});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!