Skip to content

Commit

Permalink
bug(scopes) Set Default value for defaultScope to an empty object. Cl…
Browse files Browse the repository at this point in the history
…oses #5277
  • Loading branch information
janmeier committed Jan 30, 2016
1 parent 7b44a87 commit ece7631
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.md
Expand Up @@ -3,6 +3,7 @@
- [FIXED] Fixed Instance.reload issues ([#4844](https://github.com/sequelize/sequelize/issues/4844) and [#4452](https://github.com/sequelize/sequelize/issues/4452))
- [FIXED] Fix upsert when primary key contains `.field` (internal API change for `queryInterface.upsert`) [#4755](https://github.com/sequelize/sequelize/issues/4755)
- [ADDED] Geography support for postgres
- [FIXED] Default value for `defaultScope` is now an empty object. This fixes calling `.scope('defaultScope')` when no scope is explicitly defined, see [#5277](https://github.com/sequelize/sequelize/issues/5277)

# 3.18.0
- [ADDED] Support silent: true in bulk update [#5200](https://github.com/sequelize/sequelize/issues/5200)
Expand Down
4 changes: 2 additions & 2 deletions lib/model.js
Expand Up @@ -37,7 +37,7 @@ var Model = function(name, attributes, options) {
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: null,
defaultScope: {},
scopes: [],
hooks: {},
indexes: []
Expand Down Expand Up @@ -692,7 +692,7 @@ Model.prototype.init = function(modelManager) {
// Add head and tail default attributes (id, timestamps)
addOptionalClassMethods.call(this);

this.$scope = this.options.defaultScope || {};
this.$scope = this.options.defaultScope;

if (_.isPlainObject(this.$scope)) {
conformOptions(this.$scope, this);
Expand Down
2 changes: 1 addition & 1 deletion lib/sequelize.js
Expand Up @@ -529,7 +529,7 @@ Sequelize.prototype.getQueryInterface = function() {
* @param {Object} [attributes.validate] An object of validations to execute for this column every time the model is saved. Can be either the name of a validation provided by validator.js, a validation function provided by extending validator.js (see the `DAOValidator` property for more details), or a custom validation function. Custom validation functions are called with the value of the field, and can possibly take a second callback argument, to signal that they are asynchronous. If the validator is sync, it should throw in the case of a failed validation, it it is async, the callback should be called with the error text.
* @param {Object} [options] These options are merged with the default define options provided to the Sequelize constructor
* @param {Object} [options.defaultScope] Define the default search scope to use for this model. Scopes have the same form as the options passed to find / findAll
* @param {Object} [options.defaultScope={}] Define the default search scope to use for this model. Scopes have the same form as the options passed to find / findAll
* @param {Object} [options.scopes] More scopes, defined in the same way as defaultScope above. See `Model.scope` for more information about how scopes are defined, and what you can do with them
* @param {Boolean} [options.omitNull] Don't persist null values. This means that all columns with null values will not be saved
* @param {Boolean} [options.timestamps=true] Adds createdAt and updatedAt timestamps to the model.
Expand Down
2 changes: 1 addition & 1 deletion test/unit/associations/belongs-to-many.test.js
Expand Up @@ -36,7 +36,7 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() {

AB = current.model('AB');

expect(AB.options.defaultScope).not.to.be.ok;
expect(AB.options.defaultScope).to.deep.equal({});
expect(AB.options.scopes).to.have.length(0);
});

Expand Down
6 changes: 6 additions & 0 deletions test/unit/model/scope.test.js
Expand Up @@ -70,6 +70,12 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});

describe('.scope', function () {
it('defaultScope should be an empty object if not overriden', function () {
var Foo = current.define('foo', {}, {});

expect(Foo.scope('defaultScope').$scope).to.deep.equal({});
});

it('should apply default scope', function () {
expect(Company.$scope).to.deep.equal({
include: [{ model: Project }],
Expand Down

0 comments on commit ece7631

Please sign in to comment.