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

Commit ece76317 by Jan Aagaard Meier

bug(scopes) Set Default value for defaultScope to an empty object. Closes #5277

1 parent 7b44a87f
......@@ -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)
......
......@@ -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);
......
......@@ -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.
......
......@@ -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);
});
......
......@@ -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 }],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!