From ece763177186e8bac2208904500fb5720824ac30 Mon Sep 17 00:00:00 2001 From: Jan Aagaard Meier Date: Sat, 30 Jan 2016 22:42:46 +0100 Subject: [PATCH] bug(scopes) Set Default value for defaultScope to an empty object. Closes #5277 --- changelog.md | 1 + lib/model.js | 4 ++-- lib/sequelize.js | 2 +- test/unit/associations/belongs-to-many.test.js | 2 +- test/unit/model/scope.test.js | 6 ++++++ 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/changelog.md b/changelog.md index 826f872b64a6..4b6a24515ac1 100644 --- a/changelog.md +++ b/changelog.md @@ -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) diff --git a/lib/model.js b/lib/model.js index 6f89caa15ef6..cda24b071b1b 100644 --- a/lib/model.js +++ b/lib/model.js @@ -37,7 +37,7 @@ var Model = function(name, attributes, options) { whereCollection: null, schema: null, schemaDelimiter: '', - defaultScope: null, + defaultScope: {}, scopes: [], hooks: {}, indexes: [] @@ -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); diff --git a/lib/sequelize.js b/lib/sequelize.js index d3c751527538..ab321108cdb6 100755 --- a/lib/sequelize.js +++ b/lib/sequelize.js @@ -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. diff --git a/test/unit/associations/belongs-to-many.test.js b/test/unit/associations/belongs-to-many.test.js index ae5fe38e4e5b..49154a469073 100644 --- a/test/unit/associations/belongs-to-many.test.js +++ b/test/unit/associations/belongs-to-many.test.js @@ -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); }); diff --git a/test/unit/model/scope.test.js b/test/unit/model/scope.test.js index d98768b29a5d..ede880a5836f 100644 --- a/test/unit/model/scope.test.js +++ b/test/unit/model/scope.test.js @@ -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 }],