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

Commit d2545644 by Felix Becker Committed by Jan Aagaard Meier

Make instances instanceof Model, with ES6 classes (#5924)

* Make Instance an ES6 class

* Add esversion: 6 to jshintrc

* Make Model an ES6 class

* Merge Model class into Instance class

* Rename instance.js to model.js

* Remove name parameter from Model.init()

The name is set when subclassing the Model class

* Change sequelize.define() to a Model class factory

* Delete model.js

* Remove Model.Instance and Model.prototype.Model

* Fix JSHint issue

* Remove leftover require('./instance')

* Fix reference to old Instance class

* Remove references to old Instance class from main module

* Reorder imports to hopefully trigger Travis Build

* Fix _addOptionalClassMethods call

* Fix more references to .Instance

* Correct uses of .Model

* Fix use of this in closure

* Fix usage of instanceof Model

* Fix usage of .Model

* Fix usage of this in closure

* Fix usage of $Model

* Fix test for `plain` option

* Fix uses of .$Model

* Fix hooks

* Fix global hooks

* Make more tests pass

* Make more tests pass

* Fix Model.prototype references in findone test

* Change Model.schema()

* Fix instanceof checks in belongs-to-many association

* Fix Model.scope()

* Remove callback support from hooks

* Fix instanceof Model checks

* Fix usage of instanceof Model

* Change another occurence of .Model.name to .constructor.name

* Replace .constructor by .Model again

* Fix this inside closure

* Change test cosntructor assertions to instanceof checks
1 parent 738ea1b2
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
/* questionable */ /* questionable */
"loopfunc":true, "loopfunc":true,
"esversion": 6,
"globals": { "globals": {
"Promise": true "Promise": true
}, },
......
...@@ -8,7 +8,7 @@ Association.prototype.toInstanceArray = function (objs) { ...@@ -8,7 +8,7 @@ Association.prototype.toInstanceArray = function (objs) {
objs = [objs]; objs = [objs];
} }
return objs.map(function(obj) { return objs.map(function(obj) {
if (!(obj instanceof this.target.Instance)) { if (!(obj instanceof this.target)) {
var tmpInstance = {}; var tmpInstance = {};
tmpInstance[this.target.primaryKeyAttribute] = obj; tmpInstance[this.target.primaryKeyAttribute] = obj;
return this.target.build(tmpInstance, { return this.target.build(tmpInstance, {
......
...@@ -521,7 +521,7 @@ BelongsToMany.prototype.injectGetter = function(obj) { ...@@ -521,7 +521,7 @@ BelongsToMany.prototype.injectGetter = function(obj) {
}); });
where.$or = instances.map(function (instance) { where.$or = instances.map(function (instance) {
if (instance instanceof association.target.Instance) { if (instance instanceof association.target) {
return instance.where(); return instance.where();
} else { } else {
var $where = {}; var $where = {};
...@@ -592,7 +592,7 @@ BelongsToMany.prototype.injectSetter = function(obj) { ...@@ -592,7 +592,7 @@ BelongsToMany.prototype.injectSetter = function(obj) {
} else { } else {
var throughAttributes = newObj[association.through.model.name]; var throughAttributes = newObj[association.through.model.name];
// Quick-fix for subtle bug when using existing objects that might have the through model attached (not as an attribute object) // Quick-fix for subtle bug when using existing objects that might have the through model attached (not as an attribute object)
if (throughAttributes instanceof association.through.model.Instance) { if (throughAttributes instanceof association.through.model) {
throughAttributes = {}; throughAttributes = {};
} }
...@@ -712,7 +712,7 @@ BelongsToMany.prototype.injectSetter = function(obj) { ...@@ -712,7 +712,7 @@ BelongsToMany.prototype.injectSetter = function(obj) {
, attributes = _.defaults({}, throughAttributes, defaultAttributes) , attributes = _.defaults({}, throughAttributes, defaultAttributes)
, where = {}; , where = {};
// Quick-fix for subtle bug when using existing objects that might have the through model attached (not as an attribute object) // Quick-fix for subtle bug when using existing objects that might have the through model attached (not as an attribute object)
if (throughAttributes instanceof association.through.model.Instance) { if (throughAttributes instanceof association.through.model) {
throughAttributes = {}; throughAttributes = {};
} }
......
...@@ -213,7 +213,7 @@ BelongsTo.prototype.injectSetter = function(instancePrototype) { ...@@ -213,7 +213,7 @@ BelongsTo.prototype.injectSetter = function(instancePrototype) {
options = options || {}; options = options || {};
var value = associatedInstance; var value = associatedInstance;
if (associatedInstance instanceof association.target.Instance) { if (associatedInstance instanceof association.target) {
value = associatedInstance[association.targetKey]; value = associatedInstance[association.targetKey];
} }
......
...@@ -366,7 +366,7 @@ HasMany.prototype.has = function(sourceInstance, targetInstances, options) { ...@@ -366,7 +366,7 @@ HasMany.prototype.has = function(sourceInstance, targetInstances, options) {
}); });
where.$or = targetInstances.map(function (instance) { where.$or = targetInstances.map(function (instance) {
if (instance instanceof association.target.Instance) { if (instance instanceof association.target) {
return instance.where(); return instance.where();
} else { } else {
var _where = {}; var _where = {};
......
...@@ -228,7 +228,7 @@ HasOne.prototype.injectSetter = function(instancePrototype) { ...@@ -228,7 +228,7 @@ HasOne.prototype.injectSetter = function(instancePrototype) {
} }
}).then(function() { }).then(function() {
if (associatedInstance && !alreadyAssociated) { if (associatedInstance && !alreadyAssociated) {
if (!(associatedInstance instanceof association.target.Instance)) { if (!(associatedInstance instanceof association.target)) {
var tmpInstance = {}; var tmpInstance = {};
tmpInstance[association.target.primaryKeyAttribute] = associatedInstance; tmpInstance[association.target.primaryKeyAttribute] = associatedInstance;
associatedInstance = association.target.build(tmpInstance, { associatedInstance = association.target.build(tmpInstance, {
......
...@@ -91,8 +91,8 @@ var Mixin = module.exports = function() {}; ...@@ -91,8 +91,8 @@ var Mixin = module.exports = function() {};
// The logic for hasOne and belongsTo is exactly the same // The logic for hasOne and belongsTo is exactly the same
var singleLinked = function (Type) { var singleLinked = function (Type) {
return function(target, options) { // testhint options:none return function(target, options) { // testhint options:none
if (!(target instanceof this.sequelize.Model)) { if (!target.prototype || !(target.prototype instanceof this.sequelize.Model)) {
throw new Error(this.name + '.' + Utils.lowercaseFirst(Type.toString()) + ' called with something that\'s not an instance of Sequelize.Model'); throw new Error(this.name + '.' + Utils.lowercaseFirst(Type.toString()) + ' called with something that\'s not a subclass of Sequelize.Model');
} }
var source = this; var source = this;
...@@ -107,11 +107,11 @@ var singleLinked = function (Type) { ...@@ -107,11 +107,11 @@ var singleLinked = function (Type) {
source.associations[association.associationAccessor] = association.injectAttributes(); source.associations[association.associationAccessor] = association.injectAttributes();
if (association.mixin) { if (association.mixin) {
association.mixin(source.Instance.prototype); association.mixin(source.prototype);
} else { } else {
association.injectGetter(source.Instance.prototype); association.injectGetter(source.prototype);
association.injectSetter(source.Instance.prototype); association.injectSetter(source.prototype);
association.injectCreator(source.Instance.prototype); association.injectCreator(source.prototype);
} }
return association; return association;
...@@ -168,8 +168,8 @@ Mixin.belongsTo = singleLinked(BelongsTo); ...@@ -168,8 +168,8 @@ Mixin.belongsTo = singleLinked(BelongsTo);
* @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key. * @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key.
*/ */
Mixin.hasMany = function(target, options) { // testhint options:none Mixin.hasMany = function(target, options) { // testhint options:none
if (!(target instanceof this.sequelize.Model)) { if (!target.prototype || !(target.prototype instanceof this.sequelize.Model)) {
throw new Error(this.name + '.hasMany called with something that\'s not an instance of Sequelize.Model'); throw new Error(this.name + '.hasMany called with something that\'s not a subclass of Sequelize.Model');
} }
var source = this; var source = this;
...@@ -186,7 +186,7 @@ Mixin.hasMany = function(target, options) { // testhint options:none ...@@ -186,7 +186,7 @@ Mixin.hasMany = function(target, options) { // testhint options:none
source.associations[association.associationAccessor] = association; source.associations[association.associationAccessor] = association;
association.injectAttributes(); association.injectAttributes();
association.mixin(source.Instance.prototype); association.mixin(source.prototype);
return association; return association;
}; };
...@@ -247,8 +247,8 @@ Mixin.hasMany = function(target, options) { // testhint options:none ...@@ -247,8 +247,8 @@ Mixin.hasMany = function(target, options) { // testhint options:none
* @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key. * @param {boolean} [options.constraints=true] Should on update and on delete constraints be enabled on the foreign key.
*/ */
Mixin.belongsToMany = function(targetModel, options) { // testhint options:none Mixin.belongsToMany = function(targetModel, options) { // testhint options:none
if (!(targetModel instanceof this.sequelize.Model)) { if (!targetModel.prototype || !(targetModel.prototype instanceof this.sequelize.Model)) {
throw new Error(this.name + '.belongsToMany called with something that\'s not an instance of Sequelize.Model'); throw new Error(this.name + '.belongsToMany called with something that\'s not a subclass of Sequelize.Model');
} }
var sourceModel = this; var sourceModel = this;
...@@ -264,9 +264,9 @@ Mixin.belongsToMany = function(targetModel, options) { // testhint options:none ...@@ -264,9 +264,9 @@ Mixin.belongsToMany = function(targetModel, options) { // testhint options:none
var association = new BelongsToMany(sourceModel, targetModel, options); var association = new BelongsToMany(sourceModel, targetModel, options);
sourceModel.associations[association.associationAccessor] = association.injectAttributes(); sourceModel.associations[association.associationAccessor] = association.injectAttributes();
association.injectGetter(sourceModel.Instance.prototype); association.injectGetter(sourceModel.prototype);
association.injectSetter(sourceModel.Instance.prototype); association.injectSetter(sourceModel.prototype);
association.injectCreator(sourceModel.Instance.prototype); association.injectCreator(sourceModel.prototype);
return association; return association;
}; };
......
...@@ -662,7 +662,7 @@ var QueryGenerator = { ...@@ -662,7 +662,7 @@ var QueryGenerator = {
options = this.nameIndexes([options], options.prefix)[0]; options = this.nameIndexes([options], options.prefix)[0];
} }
options = Model.prototype.$conformIndex(options); options = Model.$conformIndex(options);
if (!this._dialect.supports.index.type) { if (!this._dialect.supports.index.type) {
delete options.type; delete options.type;
...@@ -823,7 +823,7 @@ var QueryGenerator = { ...@@ -823,7 +823,7 @@ var QueryGenerator = {
break; break;
} }
if (item instanceof Model) { if (typeof item === 'function' && item.prototype instanceof Model) {
model = item; model = item;
as = undefined; as = undefined;
} else { } else {
...@@ -1528,7 +1528,7 @@ var QueryGenerator = { ...@@ -1528,7 +1528,7 @@ var QueryGenerator = {
} else { } else {
query += ' FOR UPDATE'; query += ' FOR UPDATE';
} }
if (this._dialect.supports.lockOf && options.lock.of instanceof Model) { if (this._dialect.supports.lockOf && options.lock.of && options.lock.of.prototype instanceof Model) {
query += ' OF ' + this.quoteTable(options.lock.of.name); query += ' OF ' + this.quoteTable(options.lock.of.name);
} }
} }
...@@ -1562,7 +1562,7 @@ var QueryGenerator = { ...@@ -1562,7 +1562,7 @@ var QueryGenerator = {
if (Array.isArray(options.order)) { if (Array.isArray(options.order)) {
options.order.forEach(function(t) { options.order.forEach(function(t) {
if (Array.isArray(t) && _.size(t) > 1) { if (Array.isArray(t) && _.size(t) > 1) {
if (t[0] instanceof Model || t[0].model instanceof Model) { if ((typeof t[0] === 'function' && t[0].prototype instanceof Model) || (typeof t[0].model === 'function' && t[0].model.prototype instanceof Model)) {
if (typeof t[t.length - 2] === 'string') { if (typeof t[t.length - 2] === 'string') {
validateOrder(_.last(t)); validateOrder(_.last(t));
} }
...@@ -1571,7 +1571,7 @@ var QueryGenerator = { ...@@ -1571,7 +1571,7 @@ var QueryGenerator = {
} }
} }
if (subQuery && (Array.isArray(t) && !(t[0] instanceof Model) && !(t[0].model instanceof Model))) { if (subQuery && (Array.isArray(t) && !(typeof t[0] === 'function' && t[0].prototype instanceof Model) && !(t[0] && typeof t[0].model === 'function' && t[0].model.prototype instanceof Model))) {
subQueryOrder.push(this.quote(t, model)); subQueryOrder.push(this.quote(t, model));
} }
......
...@@ -100,14 +100,9 @@ var Hooks = { ...@@ -100,14 +100,9 @@ var Hooks = {
runHooks: function(hooks) { runHooks: function(hooks) {
var self = this var self = this
, fn
, fnArgs = Utils.sliceArgs(arguments, 1) , fnArgs = Utils.sliceArgs(arguments, 1)
, hookType; , hookType;
if (typeof fnArgs[fnArgs.length - 1] === 'function') {
fn = fnArgs.pop();
}
if (typeof hooks === 'string') { if (typeof hooks === 'string') {
hookType = hooks; hookType = hooks;
hooks = this.options.hooks[hookType] || []; hooks = this.options.hooks[hookType] || [];
...@@ -141,10 +136,6 @@ var Hooks = { ...@@ -141,10 +136,6 @@ var Hooks = {
return hook.apply(self, fnArgs); return hook.apply(self, fnArgs);
}).return(); }).return();
if (fn) {
return promise.nodeify(fn);
}
return promise; return promise;
}, },
...@@ -467,7 +458,7 @@ module.exports = { ...@@ -467,7 +458,7 @@ module.exports = {
var allHooks = Object.keys(hookTypes).concat(Object.keys(hookAliases)); var allHooks = Object.keys(hookTypes).concat(Object.keys(hookAliases));
allHooks.forEach(function(hook) { allHooks.forEach(function(hook) {
Model.prototype[hook] = function(name, callback) { Model[hook] = Model.prototype[hook] = function(name, callback) {
return this.addHook(hook, name, callback); return this.addHook(hook, name, callback);
}; };
}); });
......
...@@ -19,7 +19,7 @@ var InstanceValidator = module.exports = function(modelInstance, options) { ...@@ -19,7 +19,7 @@ var InstanceValidator = module.exports = function(modelInstance, options) {
options = _.clone(options) || {}; options = _.clone(options) || {};
if (options.fields && !options.skip) { if (options.fields && !options.skip) {
options.skip = Utils._.difference(Object.keys(modelInstance.Model.attributes), options.fields); options.skip = Utils._.difference(Object.keys(modelInstance.constructor.attributes), options.fields);
} }
// assign defined and default options // assign defined and default options
...@@ -87,14 +87,14 @@ InstanceValidator.prototype._validate = function() { ...@@ -87,14 +87,14 @@ InstanceValidator.prototype._validate = function() {
*/ */
InstanceValidator.prototype.validate = function() { InstanceValidator.prototype.validate = function() {
if (this.options.hooks) { if (this.options.hooks) {
return this.modelInstance.Model.runHooks('beforeValidate', this.modelInstance, this.options).bind(this).then(function() { return this.modelInstance.constructor.runHooks('beforeValidate', this.modelInstance, this.options).bind(this).then(function() {
return this._validate().bind(this).catch(function(error) { return this._validate().bind(this).catch(function(error) {
return this.modelInstance.Model.runHooks('validationFailed', this.modelInstance, this.options, error).then(function(newError) { return this.modelInstance.constructor.runHooks('validationFailed', this.modelInstance, this.options, error).then(function(newError) {
throw newError || error; throw newError || error;
}); });
}); });
}).then(function() { }).then(function() {
return this.modelInstance.Model.runHooks('afterValidate', this.modelInstance, this.options); return this.modelInstance.constructor.runHooks('afterValidate', this.modelInstance, this.options);
}).return(this.modelInstance); }).return(this.modelInstance);
} }
return this._validate(); return this._validate();
......
'use strict';
var Utils = require('./utils')
, BelongsTo = require('./associations/belongs-to')
, BelongsToMany = require('./associations/belongs-to-many')
, InstanceValidator = require('./instance-validator')
, QueryTypes = require('./query-types')
, sequelizeErrors = require('./errors')
, Dottie = require('dottie')
, Promise = require('./promise')
, _ = require('lodash')
, defaultsOptions = { raw: true };
// private
var initValues = function(values, options) {
var defaults
, key;
values = values && _.clone(values) || {};
if (options.isNewRecord) {
defaults = {};
if (this.Model._hasDefaultValues) {
defaults = _.mapValues(this.Model._defaultValues, function(valueFn) {
var value = valueFn();
return (value && value._isSequelizeMethod) ? value : _.cloneDeep(value);
});
}
// set id to null if not passed as value, a newly created dao has no id
// removing this breaks bulkCreate
// do after default values since it might have UUID as a default value
if (!defaults.hasOwnProperty(this.Model.primaryKeyAttribute)) {
defaults[this.Model.primaryKeyAttribute] = null;
}
if (this.Model._timestampAttributes.createdAt && defaults[this.Model._timestampAttributes.createdAt]) {
this.dataValues[this.Model._timestampAttributes.createdAt] = Utils.toDefaultValue(defaults[this.Model._timestampAttributes.createdAt]);
delete defaults[this.Model._timestampAttributes.createdAt];
}
if (this.Model._timestampAttributes.updatedAt && defaults[this.Model._timestampAttributes.updatedAt]) {
this.dataValues[this.Model._timestampAttributes.updatedAt] = Utils.toDefaultValue(defaults[this.Model._timestampAttributes.updatedAt]);
delete defaults[this.Model._timestampAttributes.updatedAt];
}
if (this.Model._timestampAttributes.deletedAt && defaults[this.Model._timestampAttributes.deletedAt]) {
this.dataValues[this.Model._timestampAttributes.deletedAt] = Utils.toDefaultValue(defaults[this.Model._timestampAttributes.deletedAt]);
delete defaults[this.Model._timestampAttributes.deletedAt];
}
if (Object.keys(defaults).length) {
for (key in defaults) {
if (values[key] === undefined) {
this.set(key, Utils.toDefaultValue(defaults[key]), defaultsOptions);
delete values[key];
}
}
}
}
this.set(values, options);
};
/**
* This class represents an single instance, a database row. You might see it referred to as both Instance and instance. You should not
* instantiate the Instance class directly, instead you access it using the finder and creation methods on the model.
*
* Instance instances operate with the concept of a `dataValues` property, which stores the actual values represented by the instance.
* By default, the values from dataValues can also be accessed directly from the Instance, that is:
* ```js
* instance.field
* // is the same as
* instance.get('field')
* // is the same as
* instance.getDataValue('field')
* ```
* However, if getters and/or setters are defined for `field` they will be invoked, instead of returning the value from `dataValues`.
* Accessing properties directly or using `get` is preferred for regular use, `getDataValue` should only be used for custom getters.
*
* @see {Sequelize#define} for more information about getters and setters
* @class Instance
*/
var Instance = function(values, options) {
this.dataValues = {};
this._previousDataValues = {};
this._changed = {};
this.$modelOptions = this.Model.options;
this.$options = options || {};
this.hasPrimaryKeys = this.Model.options.hasPrimaryKeys;
this.__eagerlyLoadedAssociations = [];
/**
* Returns true if this instance has not yet been persisted to the database
* @property isNewRecord
* @return {Boolean}
*/
this.isNewRecord = options.isNewRecord;
/**
* Returns the Model the instance was created from.
* @see {Model}
* @property Model
* @return {Model}
*/
initValues.call(this, values, options);
};
/**
* A reference to the sequelize instance
* @see {Sequelize}
* @property sequelize
* @return {Sequelize}
*/
Object.defineProperty(Instance.prototype, 'sequelize', {
get: function() { return this.Model.modelManager.sequelize; }
});
/**
* Get an object representing the query for this instance, use with `options.where`
*
* @property where
* @return {Object}
*/
Instance.prototype.where = function() {
var where;
where = this.Model.primaryKeyAttributes.reduce(function (result, attribute) {
result[attribute] = this.get(attribute, {raw: true});
return result;
}.bind(this), {});
if (_.size(where) === 0) {
return this.$modelOptions.whereCollection;
}
return Utils.mapWhereFieldNames(where, this.$Model);
};
Instance.prototype.toString = function () {
return '[object SequelizeInstance:'+this.Model.name+']';
};
/**
* Get the value of the underlying data value
*
* @param {String} key
* @return {any}
*/
Instance.prototype.getDataValue = function(key) {
return this.dataValues[key];
};
/**
* Update the underlying data value
*
* @param {String} key
* @param {any} value
*/
Instance.prototype.setDataValue = function(key, value) {
var originalValue = this._previousDataValues[key];
if (!Utils.isPrimitive(value) || value !== originalValue) {
this.changed(key, true);
}
this.dataValues[key] = value;
};
/**
* If no key is given, returns all values of the instance, also invoking virtual getters.
*
* If key is given and a field or virtual getter is present for the key it will call that getter - else it will return the value for key.
*
* @param {String} [key]
* @param {Object} [options]
* @param {Boolean} [options.plain=false] If set to true, included instances will be returned as plain objects
* @param {Boolean} [options.raw=false] If set to true, field and virtual setters will be ignored
* @return {Object|any}
*/
Instance.prototype.get = function(key, options) { // testhint options:none
if (options === undefined && typeof key === 'object') {
options = key;
key = undefined;
}
options = options || {};
if (key) {
if (this._customGetters[key] && !options.raw) {
return this._customGetters[key].call(this, key);
}
if (options.plain && this.$options.include && this.$options.includeNames.indexOf(key) !== -1) {
if (Array.isArray(this.dataValues[key])) {
return this.dataValues[key].map(function (instance) {
return instance.get({plain: options.plain});
});
} else if (this.dataValues[key] instanceof Instance) {
return this.dataValues[key].get({plain: options.plain});
} else {
return this.dataValues[key];
}
}
return this.dataValues[key];
}
if (this._hasCustomGetters || (options.plain && this.$options.include) || options.clone) {
var values = {}
, _key;
if (this._hasCustomGetters) {
for (_key in this._customGetters) {
if (this._customGetters.hasOwnProperty(_key)) {
values[_key] = this.get(_key, options);
}
}
}
for (_key in this.dataValues) {
if (!values.hasOwnProperty(_key) && this.dataValues.hasOwnProperty(_key)) {
values[_key] = this.get(_key, options);
}
}
return values;
}
return this.dataValues;
};
/**
* 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
* will be called instead. To bypass the setter, you can pass `raw: true` in the options object.
*
* If set is called with an object, it will loop over the object, and call set recursively for each key, value pair. If you set raw to true, the underlying dataValues will either be
* set directly to the object passed, or used to extend dataValues, if dataValues already contain values.
*
* When set is called, the previous value of the field is stored and sets a changed flag(see `changed`).
*
* Set can also be used to build instances for associations, if you have values for those.
* When using set with associations you need to make sure the property key matches the alias of the association
* while also making sure that the proper include options have been set (from .build() or .find())
*
* If called with a dot.separated key on a JSON/JSONB attribute it will set the value nested and flag the entire object as changed.
*
* @see {Model#find} for more information about includes
* @param {String|Object} key
* @param {any} value
* @param {Object} [options]
* @param {Boolean} [options.raw=false] If set to true, field and virtual setters will be ignored
* @param {Boolean} [options.reset=false] Clear all previously set data values
* @alias setAttributes
*/
Instance.prototype.set = function(key, value, options) { // testhint options:none
var values
, originalValue
, keys
, i
, length;
if (typeof key === 'object' && key !== null) {
values = key;
options = value || {};
if (options.reset) {
this.dataValues = {};
}
// If raw, and we're not dealing with includes or special attributes, just set it straight on the dataValues object
if (options.raw && !(this.$options && this.$options.include) && !(options && options.attributes) && !this.Model._hasBooleanAttributes && !this.Model._hasDateAttributes) {
if (Object.keys(this.dataValues).length) {
this.dataValues = _.extend(this.dataValues, values);
} else {
this.dataValues = values;
}
// If raw, .changed() shouldn't be true
this._previousDataValues = _.clone(this.dataValues);
} else {
// Loop and call set
if (options.attributes) {
keys = options.attributes;
if (this.Model._hasVirtualAttributes) {
keys = keys.concat(this.Model._virtualAttributes);
}
if (this.$options.includeNames) {
keys = keys.concat(this.$options.includeNames);
}
for (i = 0, length = keys.length; i < length; i++) {
if (values[keys[i]] !== undefined) {
this.set(keys[i], values[keys[i]], options);
}
}
} else {
for (key in values) {
this.set(key, values[key], options);
}
}
if (options.raw) {
// If raw, .changed() shouldn't be true
this._previousDataValues = _.clone(this.dataValues);
}
}
} else {
if (!options)
options = {};
if (!options.raw) {
originalValue = this.dataValues[key];
}
// If not raw, and there's a customer setter
if (!options.raw && this._customSetters[key]) {
this._customSetters[key].call(this, value, key);
} else {
// Check if we have included models, and if this key matches the include model names/aliases
if (this.$options && this.$options.include && this.$options.includeNames.indexOf(key) !== -1) {
// Pass it on to the include handler
this._setInclude(key, value, options);
return this;
} else {
// Bunch of stuff we won't do when its raw
if (!options.raw) {
// If attribute is not in model definition, return
if (!this._isAttribute(key)) {
if (key.indexOf('.') > -1 && this.Model._isJsonAttribute(key.split('.')[0])) {
var previousDottieValue = Dottie.get(this.dataValues, key);
if (!_.isEqual(previousDottieValue, value)) {
Dottie.set(this.dataValues, key, value);
this.changed(key.split('.')[0], true);
}
}
return this;
}
// If attempting to set primary key and primary key is already defined, return
if (this.Model._hasPrimaryKeys && originalValue && this.Model._isPrimaryKey(key)) {
return this;
}
// If attempting to set read only attributes, return
if (!this.isNewRecord && this.Model._hasReadOnlyAttributes && this.Model._isReadOnlyAttribute(key)) {
return this;
}
// Convert date fields to real date objects
if (this.Model._hasDateAttributes && this.Model._isDateAttribute(key) && !!value && !value._isSequelizeMethod) {
if (!(value instanceof Date)) {
value = new Date(value);
}
if (!(originalValue instanceof Date)) {
originalValue = new Date(originalValue);
}
if (originalValue && value.getTime() === originalValue.getTime()) {
return this;
}
}
}
// Convert boolean-ish values to booleans
if (this.Model._hasBooleanAttributes && this.Model._isBooleanAttribute(key) && value !== null && value !== undefined && !value._isSequelizeMethod) {
if (Buffer.isBuffer(value) && value.length === 1) {
// Bit fields are returned as buffers
value = value[0];
}
if (_.isString(value)) {
// Only take action on valid boolean strings.
value = (value === 'true') ? true : (value === 'false') ? false : value;
} else if (_.isNumber(value)) {
// Only take action on valid boolean integers.
value = (value === 1) ? true : (value === 0) ? false : value;
}
}
if (!options.raw && ((!Utils.isPrimitive(value) && value !== null) || value !== originalValue)) {
this._previousDataValues[key] = originalValue;
this.changed(key, true);
}
this.dataValues[key] = value;
}
}
}
return this;
};
Instance.prototype.setAttributes = function(updates) {
return this.set(updates);
};
/**
* 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 and no keys have changed, it will return `false`.
*
* @param {String} [key]
* @return {Boolean|Array}
*/
Instance.prototype.changed = function(key, value) {
if (key) {
if (value !== undefined) {
this._changed[key] = value;
return this;
}
return this._changed[key] || false;
}
var changed = Object.keys(this.dataValues).filter(function(key) {
return this.changed(key);
}.bind(this));
return changed.length ? changed : false;
};
/**
* Returns the previous value for key from `_previousDataValues`.
*
* If called without a key, returns the previous values for all values which have changed
*
* @param {String} [key]
* @return {any|Array<any>}
*/
Instance.prototype.previous = function(key) {
if (key) {
return this._previousDataValues[key];
}
return _.pickBy(this._previousDataValues, function(value, key) {
return this.changed(key);
}.bind(this));
};
Instance.prototype._setInclude = function(key, value, options) {
if (!Array.isArray(value)) value = [value];
if (value[0] instanceof Instance) {
value = value.map(function(instance) {
return instance.dataValues;
});
}
var include = this.$options.includeMap[key]
, association = include.association
, self = this
, accessor = key
, childOptions
, primaryKeyAttribute = include.model.primaryKeyAttribute
, isEmpty;
if (!isEmpty) {
childOptions = {
isNewRecord: this.isNewRecord,
include: include.include,
includeNames: include.includeNames,
includeMap: include.includeMap,
includeValidated: true,
raw: options.raw,
attributes: include.originalAttributes
};
}
if (include.originalAttributes === undefined || include.originalAttributes.length) {
if (association.isSingleAssociation) {
if (Array.isArray(value)) {
value = value[0];
}
isEmpty = (value && value[primaryKeyAttribute] === null) || (value === null);
self[accessor] = self.dataValues[accessor] = isEmpty ? null : include.model.build(value, childOptions);
} else {
isEmpty = value[0] && value[0][primaryKeyAttribute] === null;
self[accessor] = self.dataValues[accessor] = isEmpty ? [] : include.model.bulkBuild(value, childOptions);
}
}
};
/**
* 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`.
* This error will have a property for each of the fields for which validation failed, with the error message for that field.
*
* @param {Object} [options]
* @param {string[]} [options.fields] An optional array of strings, representing database columns. If fields is provided, only those columns will be validated and saved.
* @param {Boolean} [options.silent=false] If true, the updatedAt timestamp will not be updated.
* @param {Boolean} [options.validate=true] If false, validations won't be run.
* @param {Boolean} [options.hooks=true] Run before and after create / update + validate hooks
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
*
* @return {Promise<this|Errors.ValidationError>}
*/
Instance.prototype.save = function(options) {
if (arguments.length > 1) {
throw new Error('The second argument was removed in favor of the options object.');
}
options = Utils.cloneDeep(options);
options = _.defaults(options, {
hooks: true,
validate: true
});
if (!options.fields) {
if (this.isNewRecord) {
options.fields = Object.keys(this.Model.attributes);
} else {
options.fields = _.intersection(this.changed(), Object.keys(this.Model.attributes));
}
options.defaultFields = options.fields;
}
if (options.returning === undefined) {
if (options.association) {
options.returning = false;
} else if (this.isNewRecord) {
options.returning = true;
}
}
var self = this
, primaryKeyName = this.Model.primaryKeyAttribute
, primaryKeyAttribute = primaryKeyName && this.Model.rawAttributes[primaryKeyName]
, updatedAtAttr = this.Model._timestampAttributes.updatedAt
, createdAtAttr = this.Model._timestampAttributes.createdAt
, hook = self.isNewRecord ? 'Create' : 'Update'
, wasNewRecord = this.isNewRecord
, now = Utils.now(this.sequelize.options.dialect);
if (updatedAtAttr && options.fields.length >= 1 && options.fields.indexOf(updatedAtAttr) === -1) {
options.fields.push(updatedAtAttr);
}
if (options.silent === true && !(this.isNewRecord && this.get(updatedAtAttr, {raw: true}))) {
// UpdateAtAttr might have been added as a result of Object.keys(Model.attributes). In that case we have to remove it again
Utils._.remove(options.fields, function(val) {
return val === updatedAtAttr;
});
updatedAtAttr = false;
}
if (this.isNewRecord === true) {
if (createdAtAttr && options.fields.indexOf(createdAtAttr) === -1) {
options.fields.push(createdAtAttr);
}
if (primaryKeyAttribute && primaryKeyAttribute.defaultValue && options.fields.indexOf(primaryKeyName) < 0) {
options.fields.unshift(primaryKeyName);
}
}
if (this.isNewRecord === false) {
if (primaryKeyName && this.get(primaryKeyName, {raw: true}) === undefined) {
throw new Error('You attempted to save an instance with no primary key, this is not allowed since it would result in a global update');
}
}
if (updatedAtAttr && !options.silent && options.fields.indexOf(updatedAtAttr) !== -1) {
this.dataValues[updatedAtAttr] = this.Model.$getDefaultTimestamp(updatedAtAttr) || now;
}
if (this.isNewRecord && createdAtAttr && !this.dataValues[createdAtAttr]) {
this.dataValues[createdAtAttr] = this.Model.$getDefaultTimestamp(createdAtAttr) || now;
}
return Promise.bind(this).then(function() {
// Validate
if (options.validate) {
return this.validate(options);
}
}).then(function() {
return Promise.bind(this).then(function() {
// Run before hook
if (options.hooks) {
var beforeHookValues = _.pick(this.dataValues, options.fields)
, afterHookValues
, hookChanged
, ignoreChanged = _.difference(this.changed(), options.fields); // In case of update where it's only supposed to update the passed values and the hook values
if (updatedAtAttr && options.fields.indexOf(updatedAtAttr) !== -1) {
ignoreChanged = _.without(ignoreChanged, updatedAtAttr);
}
return this.Model.runHooks('before' + hook, this, options).bind(this).then(function() {
if (options.defaultFields && !this.isNewRecord) {
afterHookValues = _.pick(this.dataValues, _.difference(this.changed(), ignoreChanged));
hookChanged = [];
Object.keys(afterHookValues).forEach(function (key) {
if (afterHookValues[key] !== beforeHookValues[key]) {
hookChanged.push(key);
}
});
options.fields = _.uniq(options.fields.concat(hookChanged));
}
if (hookChanged) {
if (options.validate) {
// Validate again
options.skip = _.difference(Object.keys(this.Model.rawAttributes), hookChanged);
return this.validate(options).then(function() {
delete options.skip;
});
}
}
});
}
}).then(function() {
if (!options.fields.length) return this;
if (!this.isNewRecord) return this;
if (!this.$options.include || !this.$options.include.length) return this;
// Nested creation for BelongsTo relations
return Promise.map(this.$options.include.filter(function (include) {
return include.association instanceof BelongsTo;
}), function (include) {
var instance = self.get(include.as);
if (!instance) return Promise.resolve();
var includeOptions = _(Utils.cloneDeep(include))
.omit(['association'])
.defaults({
transaction: options.transaction,
logging: options.logging,
parentRecord: self
}).value();
return instance.save(includeOptions).then(function () {
return self[include.association.accessors.set](instance, {save: false, logging: options.logging});
});
});
})
.then(function() {
if (!options.fields.length) return this;
if (!this.changed() && !this.isNewRecord) return this;
var values = Utils.mapValueFieldNames(this.dataValues, options.fields, this.Model)
, query = null
, args = [];
if (self.isNewRecord) {
query = 'insert';
args = [self, self.$Model.getTableName(options), values, options];
} else {
var where = this.where();
where = Utils.mapValueFieldNames(where, Object.keys(where), this.Model);
query = 'update';
args = [self, self.$Model.getTableName(options), values, where, options];
}
return self.sequelize.getQueryInterface()[query].apply(self.sequelize.getQueryInterface(), args)
.then(function(result) {
// Transfer database generated values (defaults, autoincrement, etc)
Object.keys(self.Model.rawAttributes).forEach(function (attr) {
if (self.Model.rawAttributes[attr].field &&
values[self.Model.rawAttributes[attr].field] !== undefined &&
self.Model.rawAttributes[attr].field !== attr
) {
values[attr] = values[self.Model.rawAttributes[attr].field];
delete values[self.Model.rawAttributes[attr].field];
}
});
values = _.extend(values, result.dataValues);
result.dataValues = _.extend(result.dataValues, values);
return result;
})
.tap(function(result) {
if (!wasNewRecord) return self;
if (!self.$options.include || !self.$options.include.length) return self;
// Nested creation for HasOne/HasMany/BelongsToMany relations
return Promise.map(self.$options.include.filter(function (include) {
return !(include.association instanceof BelongsTo);
}), function (include) {
var instances = self.get(include.as);
if (!instances) return Promise.resolve();
if (!Array.isArray(instances)) instances = [instances];
if (!instances.length) return Promise.resolve();
var includeOptions = _(Utils.cloneDeep(include))
.omit(['association'])
.defaults({
transaction: options.transaction,
logging: options.logging,
parentRecord: self
}).value();
// Instances will be updated in place so we can safely treat HasOne like a HasMany
return Promise.map(instances, function (instance) {
if (include.association instanceof BelongsToMany) {
return instance.save(includeOptions).then(function () {
var values = {};
values[include.association.foreignKey] = self.get(self.Model.primaryKeyAttribute, {raw: true});
values[include.association.otherKey] = instance.get(instance.Model.primaryKeyAttribute, {raw: true});
return include.association.throughModel.create(values, includeOptions);
});
} else {
instance.set(include.association.foreignKey, self.get(self.Model.primaryKeyAttribute, {raw: true}));
return instance.save(includeOptions);
}
});
});
})
.tap(function(result) {
// Run after hook
if (options.hooks) {
return self.Model.runHooks('after' + hook, result, options);
}
})
.then(function(result) {
options.fields.forEach(function (field) {
result._previousDataValues[field] = result.dataValues[field];
self.changed(field, false);
});
self.isNewRecord = false;
return result;
});
});
});
};
/*
* Refresh the current instance in-place, i.e. update the object with current data from the DB and return the same object.
* This is different from doing a `find(Instance.id)`, because that would create and return a new instance. With this method,
* all references to the Instance are updated with the new data and no new objects are created.
*
* @see {Model#find}
* @param {Object} [options] Options that are passed on to `Model.find`
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @return {Promise<this>}
*/
Instance.prototype.reload = function(options) {
options = _.defaults({}, options, {
where: this.where(),
include: this.$options.include || null
});
return this.Model.findOne(options).bind(this)
.tap(function (reload) {
if (!reload) {
throw new sequelizeErrors.InstanceError(
'Instance could not be reloaded because it does not exist anymore (find call returned null)'
);
}
})
.then(function(reload) {
// update the internal options of the instance
this.$options = reload.$options;
// re-set instance values
this.set(reload.dataValues, {
raw: true,
reset: true && !options.attributes
});
}).return(this);
};
/*
* Validate the attribute of this instance according to validation rules set in the model definition.
*
* Emits null if and only if validation successful; otherwise an Error instance containing { field name : [error msgs] } entries.
*
* @param {Object} [options] Options that are passed to the validator
* @param {Array} [options.skip] An array of strings. All properties that are in this array will not be validated
* @param {Boolean} [options.hooks=true] Run before and after validate hooks
* @see {InstanceValidator}
*
* @return {Promise<undefined|Errors.ValidationError>}
*/
Instance.prototype.validate = function(options) {
return new InstanceValidator(this, options).validate();
};
/**
* This is the same as calling `set` and then calling `save` but it only saves the
* exact values passed to it, making it more atomic and safer.
*
* @see {Instance#set}
* @see {Instance#save}
* @param {Object} updates See `set`
* @param {Object} options See `save`
*
* @return {Promise<this>}
* @alias updateAttributes
*/
Instance.prototype.update = function(values, options) {
var changedBefore = this.changed() || []
, sideEffects
, fields
, setOptions;
options = options || {};
if (Array.isArray(options)) options = {fields: options};
options = Utils.cloneDeep(options);
setOptions = Utils.cloneDeep(options);
setOptions.attributes = options.fields;
this.set(values, setOptions);
// Now we need to figure out which fields were actually affected by the setter.
sideEffects = _.without.apply(this, [this.changed() || []].concat(changedBefore));
fields = _.union(Object.keys(values), sideEffects);
if (!options.fields) {
options.fields = _.intersection(fields, this.changed());
options.defaultFields = options.fields;
}
return this.save(options);
};
Instance.prototype.updateAttributes = Instance.prototype.update;
/**
* 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.
*
* @param {Object} [options={}]
* @param {Boolean} [options.force=false] If set to true, paranoid models will actually be deleted
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
*
* @return {Promise<undefined>}
*/
Instance.prototype.destroy = function(options) {
options = Utils._.extend({
hooks: true,
force: false
}, options);
return Promise.bind(this).then(function() {
// Run before hook
if (options.hooks) {
return this.Model.runHooks('beforeDestroy', this, options);
}
}).then(function() {
var where = this.where();
if (this.Model._timestampAttributes.deletedAt && options.force === false) {
var attribute = this.Model.rawAttributes[this.Model._timestampAttributes.deletedAt]
, field = attribute.field || this.Model._timestampAttributes.deletedAt
, values = {};
values[field] = new Date();
where[field] = attribute.hasOwnProperty('defaultValue') ? attribute.defaultValue : null;
this.setDataValue(field, values[field]);
return this.sequelize.getQueryInterface().update(this, this.$Model.getTableName(options), values, where, _.defaults({ hooks: false }, options));
} else {
return this.sequelize.getQueryInterface().delete(this, this.$Model.getTableName(options), where, _.assign({ type: QueryTypes.DELETE, limit: null }, options));
}
}).tap(function() {
// Run after hook
if (options.hooks) {
return this.Model.runHooks('afterDestroy', this, options);
}
}).then(function(result) {
return result;
});
};
/**
* Restore the row corresponding to this instance. Only available for paranoid models.
*
* @param {Object} [options={}]
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction]
*
* @return {Promise<undefined>}
*/
Instance.prototype.restore = function(options) {
if (!this.Model._timestampAttributes.deletedAt) throw new Error('Model is not paranoid');
options = Utils._.extend({
hooks: true,
force: false
}, options);
return Promise.bind(this).then(function() {
// Run before hook
if (options.hooks) {
return this.Model.runHooks('beforeRestore', this, options);
}
}).then(function() {
var deletedAtCol = this.Model._timestampAttributes.deletedAt
, deletedAtAttribute = this.Model.rawAttributes[deletedAtCol]
, deletedAtDefaultValue = deletedAtAttribute.hasOwnProperty('defaultValue') ? deletedAtAttribute.defaultValue : null;
this.setDataValue(deletedAtCol, deletedAtDefaultValue);
return this.save(_.extend({}, options, {hooks : false, omitNull : false}));
}).tap(function() {
// Run after hook
if (options.hooks) {
return this.Model.runHooks('afterRestore', this, options);
}
});
};
/**
* 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
* ```sql
* SET column = column + X
* ```
* query. To get the correct value after an increment into the Instance you should do a reload.
*
*```js
* instance.increment('number') // increment number by 1
* instance.increment(['number', 'count'], { by: 2 }) // increment number and count by 2
* instance.increment({ answer: 42, tries: 1}, { by: 2 }) // increment answer by 42, and tries by 1.
* // `by` is ignored, since each column has its own value
* ```
*
* @see {Instance#reload}
* @param {String|Array|Object} fields If a string is provided, that column is incremented by the value of `by` given in options. If an array is provided, the same is true for each column. If and object is provided, each column is incremented by the value given.
* @param {Object} [options]
* @param {Integer} [options.by=1] The number to increment by
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
*
* @return {Promise<this>}
*/
Instance.prototype.increment = function(fields, options) {
var identifier = this.where()
, updatedAtAttr = this.Model._timestampAttributes.updatedAt
, values = {}
, where;
options = _.defaults({}, options, {
by: 1,
attributes: {},
where: {}
});
where = _.extend({}, options.where, identifier);
if (Utils._.isString(fields)) {
values[fields] = options.by;
} else if (Utils._.isArray(fields)) {
Utils._.each(fields, function(field) {
values[field] = options.by;
});
} else { // Assume fields is key-value pairs
values = fields;
}
if (updatedAtAttr && !values[updatedAtAttr]) {
options.attributes[updatedAtAttr] = this.Model.$getDefaultTimestamp(updatedAtAttr) || Utils.now(this.sequelize.options.dialect);
}
Object.keys(values).forEach(function(attr) {
// Field name mapping
if (this.Model.rawAttributes[attr] && this.Model.rawAttributes[attr].field && this.Model.rawAttributes[attr].field !== attr) {
values[this.Model.rawAttributes[attr].field] = values[attr];
delete values[attr];
}
}, this);
return this.sequelize.getQueryInterface().increment(this, this.$Model.getTableName(options), values, where, options).return(this);
};
/**
* 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
* ```sql
* SET column = column - X
* ```
* query. To get the correct value after an decrement into the Instance you should do a reload.
*
* ```js
* instance.decrement('number') // decrement number by 1
* instance.decrement(['number', 'count'], { by: 2 }) // decrement number and count by 2
* instance.decrement({ answer: 42, tries: 1}, { by: 2 }) // decrement answer by 42, and tries by 1.
* // `by` is ignored, since each column has its own value
* ```
*
* @see {Instance#reload}
* @param {String|Array|Object} fields If a string is provided, that column is decremented by the value of `by` given in options. If an array is provided, the same is true for each column. If and object is provided, each column is decremented by the value given
* @param {Object} [options]
* @param {Integer} [options.by=1] The number to decrement by
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
*
* @return {Promise}
*/
Instance.prototype.decrement = function(fields, options) {
options = _.defaults({}, options, {
by: 1
});
if (!Utils._.isString(fields) && !Utils._.isArray(fields)) { // Assume fields is key-value pairs
Utils._.each(fields, function(value, field) {
fields[field] = -value;
});
}
options.by = 0 - options.by;
return this.increment(fields, options);
};
/**
* Check whether this and `other` Instance refer to the same row
*
* @param {Instance} other
* @return {Boolean}
*/
Instance.prototype.equals = function(other) {
var self = this;
if (!other || !other.Model) {
return false;
}
if (other.Model !== this.Model) {
return false;
}
return Utils._.every(this.Model.primaryKeyAttributes, function(attribute) {
return self.get(attribute, {raw: true}) === other.get(attribute, {raw: true});
});
};
/**
* Check if this is equal to one of `others` by calling equals
*
* @param {Array} others
* @return {Boolean}
*/
Instance.prototype.equalsOneOf = function(others) {
var self = this;
return _.some(others, function(other) {
return self.equals(other);
});
};
Instance.prototype.setValidators = function(attribute, validators) {
this.validators[attribute] = validators;
};
/**
* 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.
*
* @see {Instance#get}
* @return {object}
*/
Instance.prototype.toJSON = function() {
return this.get({
plain: true
});
};
module.exports = Instance;
'use strict'; 'use strict';
/* jshint -W110 */
var Utils = require('./utils') var Utils = require('./utils')
, Instance = require('./instance') , BelongsTo = require('./associations/belongs-to')
, BelongsToMany = require('./associations/belongs-to-many')
, InstanceValidator = require('./instance-validator')
, QueryTypes = require('./query-types')
, sequelizeErrors = require('./errors')
, Dottie = require('dottie')
, Promise = require('./promise')
, _ = require('lodash')
, Association = require('./associations/base') , Association = require('./associations/base')
, HasMany = require('./associations/has-many') , HasMany = require('./associations/has-many')
, DataTypes = require('./data-types') , DataTypes = require('./data-types')
, Util = require('util')
, Promise = require('./promise')
, QueryTypes = require('./query-types')
, Hooks = require('./hooks') , Hooks = require('./hooks')
, sequelizeErrors = require('./errors') , associationsMixin = require('./associations/mixin')
, _ = require('lodash') , defaultsOptions = { raw: true };
, associationsMixin = require('./associations/mixin');
/** /**
* 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. Instances of this class represent a database row.
* This class should _not_ be instantiated directly, it is created using `sequelize.define`, and already created models can be loaded using `sequelize.import` *
* Model instances operate with the concept of a `dataValues` property, which stores the actual values represented by the instance.
* By default, the values from dataValues can also be accessed directly from the Instance, that is:
* ```js
* instance.field
* // is the same as
* instance.get('field')
* // is the same as
* instance.getDataValue('field')
* ```
* However, if getters and/or setters are defined for `field` they will be invoked, instead of returning the value from `dataValues`.
* Accessing properties directly or using `get` is preferred for regular use, `getDataValue` should only be used for custom getters.
* *
* @see {Sequelize#define} for more information about getters and setters
* @class Model * @class Model
* @mixes Hooks * @mixes Hooks
* @mixes Associations * @mixes Associations
*/ */
var Model = function(name, attributes, options) { class Model {
this.options = Utils._.extend({
timestamps: true,
instanceMethods: {},
classMethods: {},
validate: {},
freezeTableName: false,
underscored: false,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
hooks: {},
indexes: []
}, options || {});
this.associations = {};
this.modelManager = null;
this.name = name;
this.options.hooks = _.mapValues(this.replaceHookAliases(this.options.hooks), function (hooks) {
if (!Array.isArray(hooks)) hooks = [hooks];
return hooks;
});
this.sequelize = options.sequelize;
this.underscored = this.underscored || this.underscoredAll;
if (!this.options.tableName) {
this.tableName = this.options.freezeTableName ? name : Utils.underscoredIf(Utils.pluralize(name), this.options.underscoredAll);
} else {
this.tableName = this.options.tableName;
}
this.$schema = this.options.schema;
this.$schemaDelimiter = this.options.schemaDelimiter;
// error check options
_.each(options.validate, function(validator, validatorType) {
if (_.includes(Utils._.keys(attributes), validatorType)) {
throw new Error('A model validator function must not have the same name as a field. Model: ' + name + ', field/validation name: ' + validatorType);
}
if (!_.isFunction(validator)) {
throw new Error('Members of the validate option must be functions. Model: ' + name + ', error with validate member ' + validatorType);
}
});
this.attributes = this.rawAttributes = _.mapValues(attributes, function(attribute, name) {
if (!Utils._.isPlainObject(attribute)) {
attribute = { type: attribute };
}
attribute = this.sequelize.normalizeAttribute(attribute); static get QueryInterface() {
if (attribute.references) {
if (attribute.references.model instanceof Model) {
attribute.references.model = attribute.references.model.tableName;
}
}
if (attribute.type === undefined) {
throw new Error('Unrecognized data type for field ' + name);
}
return attribute;
}.bind(this));
};
Object.defineProperty(Model.prototype, 'QueryInterface', {
get: function() {
return this.modelManager.sequelize.getQueryInterface(); return this.modelManager.sequelize.getQueryInterface();
} }
});
Object.defineProperty(Model.prototype, 'QueryGenerator', { static get QueryGenerator() {
get: function() {
return this.QueryInterface.QueryGenerator; return this.QueryInterface.QueryGenerator;
} }
});
Model.prototype.toString = function () { static toString() {
return '[object SequelizeModel:'+this.name+']'; return '[object SequelizeModel:'+this.name+']';
}; }
// private
// validateIncludedElements should have been called before this method // validateIncludedElements should have been called before this method
var paranoidClause = function(model, options) { static _paranoidClause(model, options) {
options = options || {}; options = options || {};
// Apply on each include // Apply on each include
...@@ -125,8 +59,8 @@ var paranoidClause = function(model, options) { ...@@ -125,8 +59,8 @@ var paranoidClause = function(model, options) {
// otherwise this code will never run on includes of a already conditionable where // otherwise this code will never run on includes of a already conditionable where
if (options.include) { if (options.include) {
options.include.forEach(function(include) { options.include.forEach(function(include) {
paranoidClause(include.model, include); this._paranoidClause(include.model, include);
}); }.bind(this));
} }
if (!model.options.timestamps || !model.options.paranoid || options.paranoid === false) { if (!model.options.timestamps || !model.options.paranoid || options.paranoid === false) {
...@@ -150,14 +84,14 @@ var paranoidClause = function(model, options) { ...@@ -150,14 +84,14 @@ var paranoidClause = function(model, options) {
} }
return options; return options;
}; }
var addOptionalClassMethods = function() { static _addOptionalClassMethods() {
var self = this; var self = this;
Utils._.each(this.options.classMethods || {}, function(fct, name) { self[name] = fct; }); Utils._.each(this.options.classMethods || {}, function(fct, name) { self[name] = fct; });
}; }
var addDefaultAttributes = function() { static _addDefaultAttributes() {
var self = this var self = this
, tail = {} , tail = {}
, head = {}; , head = {};
...@@ -167,7 +101,7 @@ var addDefaultAttributes = function() { ...@@ -167,7 +101,7 @@ var addDefaultAttributes = function() {
if (!_.some(this.rawAttributes, 'primaryKey')) { if (!_.some(this.rawAttributes, 'primaryKey')) {
if ('id' in this.rawAttributes) { if ('id' in this.rawAttributes) {
// Something is fishy here! // Something is fishy here!
throw new Error("A column called 'id' was added to the attributes of '" + this.tableName + "' but not marked with 'primaryKey: true'"); throw new Error(`A column called 'id' was added to the attributes of \'${this.tableName}\' but not marked with 'primaryKey: true'`);
} }
head = { head = {
...@@ -222,9 +156,9 @@ var addDefaultAttributes = function() { ...@@ -222,9 +156,9 @@ var addDefaultAttributes = function() {
if (!Object.keys(this.primaryKeys).length) { if (!Object.keys(this.primaryKeys).length) {
self.primaryKeys.id = self.rawAttributes.id; self.primaryKeys.id = self.rawAttributes.id;
} }
}; }
var findAutoIncrementField = function() { static _findAutoIncrementField() {
var fields = this.QueryGenerator.findAutoIncrementField(this); var fields = this.QueryGenerator.findAutoIncrementField(this);
this.autoIncrementField = null; this.autoIncrementField = null;
...@@ -236,9 +170,9 @@ var findAutoIncrementField = function() { ...@@ -236,9 +170,9 @@ var findAutoIncrementField = function() {
this.autoIncrementField = field; this.autoIncrementField = field;
} }
}.bind(this)); }.bind(this));
}; }
function conformOptions(options, self) { static _conformOptions(options, self) {
if (self) { if (self) {
self.$expandAttributes(options); self.$expandAttributes(options);
} }
...@@ -256,15 +190,13 @@ function conformOptions(options, self) { ...@@ -256,15 +190,13 @@ function conformOptions(options, self) {
// convert all included elements to { model: Model } form // convert all included elements to { model: Model } form
options.include = options.include.map(function(include) { options.include = options.include.map(function(include) {
include = conformInclude(include, self); include = this._conformInclude(include, self);
return include; return include;
}); }.bind(this));
} }
Model.$conformOptions = conformOptions;
function conformInclude(include, self) { static _conformInclude(include, self) {
var model; var model;
if (include._pseudo) return include; if (include._pseudo) return include;
...@@ -277,7 +209,7 @@ function conformInclude(include, self) { ...@@ -277,7 +209,7 @@ function conformInclude(include, self) {
} }
include = { model: model, association: include, as: include.as }; include = { model: model, association: include, as: include.as };
} else if (include instanceof Model) { } else if (include.prototype && include.prototype instanceof Model) {
include = { model: include }; include = { model: include };
} else if (_.isPlainObject(include)) { } else if (_.isPlainObject(include)) {
if (include.association) { if (include.association) {
...@@ -297,17 +229,15 @@ function conformInclude(include, self) { ...@@ -297,17 +229,15 @@ function conformInclude(include, self) {
model = include.model; model = include.model;
} }
conformOptions(include, model); this._conformOptions(include, model);
} else { } else {
throw new Error('Include unexpected. Element has to be either a Model, an Association or an object.'); throw new Error('Include unexpected. Element has to be either a Model, an Association or an object.');
} }
return include; return include;
} }
Model.$conformInclude = conformInclude;
var expandIncludeAllElement = function(includes, include) { static _expandIncludeAllElement(includes, include) {
// check 'all' attribute provided is valid // check 'all' attribute provided is valid
var all = include.all; var all = include.all;
delete include.all; delete include.all;
...@@ -407,10 +337,9 @@ var expandIncludeAllElement = function(includes, include) { ...@@ -407,10 +337,9 @@ var expandIncludeAllElement = function(includes, include) {
}); });
used.pop(); used.pop();
})(this, includes); })(this, includes);
}; }
var validateIncludedElement; static _validateIncludedElements(options, tableNames) {
var validateIncludedElements = function(options, tableNames) {
if (!options.model) options.model = this; if (!options.model) options.model = this;
tableNames = tableNames || {}; tableNames = tableNames || {};
...@@ -427,10 +356,10 @@ var validateIncludedElements = function(options, tableNames) { ...@@ -427,10 +356,10 @@ var validateIncludedElements = function(options, tableNames) {
} }
options.include = options.include.map(function (include) { options.include = options.include.map(function (include) {
include = conformInclude(include); include = this._conformInclude(include);
include.parent = options; include.parent = options;
validateIncludedElement.call(options.model, include, tableNames, options); this._validateIncludedElement.call(options.model, include, tableNames, options);
if (include.duplicating === undefined) { if (include.duplicating === undefined) {
include.duplicating = include.association.isMultiAssociation; include.duplicating = include.association.isMultiAssociation;
...@@ -444,7 +373,7 @@ var validateIncludedElements = function(options, tableNames) { ...@@ -444,7 +373,7 @@ var validateIncludedElements = function(options, tableNames) {
options.hasWhere = options.hasWhere || include.hasWhere || !!include.where; options.hasWhere = options.hasWhere || include.hasWhere || !!include.where;
return include; return include;
}); }.bind(this));
options.include.forEach(function (include) { options.include.forEach(function (include) {
include.hasParentWhere = options.hasParentWhere || !!options.where; include.hasParentWhere = options.hasParentWhere || !!options.where;
...@@ -499,10 +428,9 @@ var validateIncludedElements = function(options, tableNames) { ...@@ -499,10 +428,9 @@ var validateIncludedElements = function(options, tableNames) {
options.subQuery = false; options.subQuery = false;
} }
return options; return options;
}; }
Model.$validateIncludedElements = validateIncludedElements;
validateIncludedElement = function(include, tableNames, options) { static _validateIncludedElement(include, tableNames, options) {
tableNames[include.model.getTableName()] = true; tableNames[include.model.getTableName()] = true;
if (include.attributes && !options.raw) { if (include.attributes && !options.raw) {
...@@ -623,13 +551,13 @@ validateIncludedElement = function(include, tableNames, options) { ...@@ -623,13 +551,13 @@ validateIncludedElement = function(include, tableNames, options) {
// Validate child includes // Validate child includes
if (include.hasOwnProperty('include')) { if (include.hasOwnProperty('include')) {
validateIncludedElements.call(include.model, include, tableNames, options); this._validateIncludedElements.call(include.model, include, tableNames, options);
} }
return include; return include;
}; }
var expandIncludeAll = Model.$expandIncludeAll = function(options) { static _expandIncludeAll(options) {
var includes = options.include; var includes = options.include;
if (!includes) { if (!includes) {
return; return;
...@@ -642,18 +570,86 @@ var expandIncludeAll = Model.$expandIncludeAll = function(options) { ...@@ -642,18 +570,86 @@ var expandIncludeAll = Model.$expandIncludeAll = function(options) {
includes.splice(index, 1); includes.splice(index, 1);
index--; index--;
expandIncludeAllElement.call(this, includes, include); this._expandIncludeAllElement.call(this, includes, include);
} }
} }
Utils._.forEach(includes, function(include) { Utils._.forEach(includes, function(include) {
expandIncludeAll.call(include.model, include); this._expandIncludeAll.call(include.model, include);
}); }.bind(this));
}; }
Model.prototype.init = function(modelManager) { static init(attributes, options, modelManager) {
var self = this; var self = this;
this.options = Utils._.extend({
timestamps: true,
instanceMethods: {},
classMethods: {},
validate: {},
freezeTableName: false,
underscored: false,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: null,
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
hooks: {},
indexes: []
}, options || {});
this.associations = {};
this.modelManager = null;
this.options.hooks = _.mapValues(this.replaceHookAliases(this.options.hooks), function (hooks) {
if (!Array.isArray(hooks)) hooks = [hooks];
return hooks;
});
this.sequelize = options.sequelize;
this.underscored = this.underscored || this.underscoredAll;
if (!this.options.tableName) {
this.tableName = this.options.freezeTableName ? this.name : Utils.underscoredIf(Utils.pluralize(this.name), this.options.underscoredAll);
} else {
this.tableName = this.options.tableName;
}
this.$schema = this.options.schema;
this.$schemaDelimiter = this.options.schemaDelimiter;
// error check options
_.each(options.validate, function(validator, validatorType) {
if (_.includes(Utils._.keys(attributes), validatorType)) {
throw new Error('A model validator function must not have the same name as a field. Model: ' + this.name + ', field/validation name: ' + validatorType);
}
if (!_.isFunction(validator)) {
throw new Error('Members of the validate option must be functions. Model: ' + this.name + ', error with validate member ' + validatorType);
}
}.bind(this));
this.attributes = this.rawAttributes = _.mapValues(attributes, function(attribute, name) {
if (!Utils._.isPlainObject(attribute)) {
attribute = { type: attribute };
}
attribute = this.sequelize.normalizeAttribute(attribute);
if (attribute.references && attribute.references.model && attribute.references.model.prototype instanceof Model) {
attribute.references.model = attribute.references.model.tableName;
}
if (attribute.type === undefined) {
throw new Error('Unrecognized data type for field ' + name);
}
return attribute;
}.bind(this));
this.modelManager = modelManager; this.modelManager = modelManager;
this.primaryKeys = {}; this.primaryKeys = {};
...@@ -672,14 +668,7 @@ Model.prototype.init = function(modelManager) { ...@@ -672,14 +668,7 @@ Model.prototype.init = function(modelManager) {
} }
// Add head and tail default attributes (id, timestamps) // Add head and tail default attributes (id, timestamps)
addOptionalClassMethods.call(this); this._addOptionalClassMethods();
// Instance prototype
this.Instance = function() {
Instance.apply(this, arguments);
};
Util.inherits(this.Instance, Instance);
this._readOnlyAttributes = Utils._.values(this._timestampAttributes); this._readOnlyAttributes = Utils._.values(this._timestampAttributes);
this._hasReadOnlyAttributes = this._readOnlyAttributes && this._readOnlyAttributes.length; this._hasReadOnlyAttributes = this._readOnlyAttributes && this._readOnlyAttributes.length;
...@@ -689,36 +678,33 @@ Model.prototype.init = function(modelManager) { ...@@ -689,36 +678,33 @@ Model.prototype.init = function(modelManager) {
if (this.options.instanceMethods) { if (this.options.instanceMethods) {
Utils._.each(this.options.instanceMethods, function(fct, name) { Utils._.each(this.options.instanceMethods, function(fct, name) {
self.Instance.prototype[name] = fct; self.prototype[name] = fct;
}); });
} }
addDefaultAttributes.call(this); this._addDefaultAttributes();
this.refreshAttributes(); this.refreshAttributes();
findAutoIncrementField.call(this); this._findAutoIncrementField();
this.$scope = this.options.defaultScope; this.$scope = this.options.defaultScope;
if (_.isPlainObject(this.$scope)) { if (_.isPlainObject(this.$scope)) {
conformOptions(this.$scope, this); this._conformOptions(this.$scope, this);
} }
_.each(this.options.scopes, function (scope) { _.each(this.options.scopes, function (scope) {
if (_.isPlainObject(scope)) { if (_.isPlainObject(scope)) {
conformOptions(scope, this); this._conformOptions(scope, this);
} }
}.bind(this)); }.bind(this));
this.options.indexes = this.options.indexes.map(this.$conformIndex); this.options.indexes = this.options.indexes.map(this.$conformIndex);
this.Instance.prototype.$Model =
this.Instance.prototype.Model = this;
return this; return this;
}; }
Model.prototype.$conformIndex = function (index) { static $conformIndex(index) {
index = _.defaults(index, { index = _.defaults(index, {
type: '', type: '',
parser: null parser: null
...@@ -729,19 +715,19 @@ Model.prototype.$conformIndex = function (index) { ...@@ -729,19 +715,19 @@ Model.prototype.$conformIndex = function (index) {
delete index.type; delete index.type;
} }
return index; return index;
}; }
Model.prototype.refreshAttributes = function() { static refreshAttributes() {
var self = this var self = this
, attributeManipulation = {}; , attributeManipulation = {};
this.Instance.prototype._customGetters = {}; this.prototype._customGetters = {};
this.Instance.prototype._customSetters = {}; this.prototype._customSetters = {};
Utils._.each(['get', 'set'], function(type) { Utils._.each(['get', 'set'], function(type) {
var opt = type + 'terMethods' var opt = type + 'terMethods'
, funcs = Utils._.clone(Utils._.isObject(self.options[opt]) ? self.options[opt] : {}) , funcs = Utils._.clone(Utils._.isObject(self.options[opt]) ? self.options[opt] : {})
, _custom = type === 'get' ? self.Instance.prototype._customGetters : self.Instance.prototype._customSetters; , _custom = type === 'get' ? self.prototype._customGetters : self.prototype._customSetters;
Utils._.each(funcs, function(method, attribute) { Utils._.each(funcs, function(method, attribute) {
_custom[attribute] = method; _custom[attribute] = method;
...@@ -793,7 +779,7 @@ Model.prototype.refreshAttributes = function() { ...@@ -793,7 +779,7 @@ Model.prototype.refreshAttributes = function() {
this._geometryAttributes = []; this._geometryAttributes = [];
this._virtualAttributes = []; this._virtualAttributes = [];
this._defaultValues = {}; this._defaultValues = {};
this.Instance.prototype.validators = {}; this.prototype.validators = {};
this.fieldRawAttributesMap = {}; this.fieldRawAttributesMap = {};
...@@ -858,7 +844,7 @@ Model.prototype.refreshAttributes = function() { ...@@ -858,7 +844,7 @@ Model.prototype.refreshAttributes = function() {
} }
if (definition.hasOwnProperty('validate')) { if (definition.hasOwnProperty('validate')) {
self.Instance.prototype.validators[name] = definition.validate; self.prototype.validators[name] = definition.validate;
} }
if (definition.index === true && definition.type instanceof DataTypes.JSONB) { if (definition.index === true && definition.type instanceof DataTypes.JSONB) {
...@@ -920,22 +906,22 @@ Model.prototype.refreshAttributes = function() { ...@@ -920,22 +906,22 @@ Model.prototype.refreshAttributes = function() {
this.attributes = this.rawAttributes; this.attributes = this.rawAttributes;
this.tableAttributes = Utils._.omit(this.rawAttributes, this._virtualAttributes); this.tableAttributes = Utils._.omit(this.rawAttributes, this._virtualAttributes);
this.Instance.prototype._hasCustomGetters = Object.keys(this.Instance.prototype._customGetters).length; this.prototype._hasCustomGetters = Object.keys(this.prototype._customGetters).length;
this.Instance.prototype._hasCustomSetters = Object.keys(this.Instance.prototype._customSetters).length; this.prototype._hasCustomSetters = Object.keys(this.prototype._customSetters).length;
Object.keys(attributeManipulation).forEach((function(key){ Object.keys(attributeManipulation).forEach((function(key){
if (Instance.prototype.hasOwnProperty(key)) { if (Model.prototype.hasOwnProperty(key)) {
this.sequelize.log("Not overriding built-in method from model attribute: " + key); this.sequelize.log('Not overriding built-in method from model attribute: ' + key);
return; return;
} }
Object.defineProperty(this.Instance.prototype, key, attributeManipulation[key]); Object.defineProperty(this.prototype, key, attributeManipulation[key]);
}).bind(this)); }).bind(this));
this.Instance.prototype.rawAttributes = this.rawAttributes; this.prototype.rawAttributes = this.rawAttributes;
this.Instance.prototype.attributes = Object.keys(this.Instance.prototype.rawAttributes); this.prototype.attributes = Object.keys(this.prototype.rawAttributes);
this.Instance.prototype._isAttribute = Utils._.memoize(function(key) { this.prototype._isAttribute = Utils._.memoize(function(key) {
return self.Instance.prototype.attributes.indexOf(key) !== -1; return self.prototype.attributes.indexOf(key) !== -1;
}); });
// Primary key convenience variables // Primary key convenience variables
...@@ -952,23 +938,23 @@ Model.prototype.refreshAttributes = function() { ...@@ -952,23 +938,23 @@ Model.prototype.refreshAttributes = function() {
return self.primaryKeyAttributes.indexOf(key) !== -1; return self.primaryKeyAttributes.indexOf(key) !== -1;
}); });
}; }
/** /**
* Remove attribute from model definition * Remove attribute from model definition
* @param {String} [attribute] * @param {String} [attribute]
*/ */
Model.prototype.removeAttribute = function(attribute) { static removeAttribute(attribute) {
delete this.rawAttributes[attribute]; delete this.rawAttributes[attribute];
this.refreshAttributes(); this.refreshAttributes();
}; }
/** /**
* Sync this Model to the DB, that is create the table. Upon success, the callback will be called with the model instance (this) * Sync this Model to the DB, that is create the table. Upon success, the callback will be called with the model instance (this)
* @see {Sequelize#sync} for options * @see {Sequelize#sync} for options
* @return {Promise<this>} * @return {Promise<this>}
*/ */
Model.prototype.sync = function(options) { static sync(options) {
options = _.extend({}, this.options, options); options = _.extend({}, this.options, options);
options.hooks = options.hooks === undefined ? true : !!options.hooks; options.hooks = options.hooks === undefined ? true : !!options.hooks;
...@@ -1010,9 +996,9 @@ Model.prototype.sync = function(options) { ...@@ -1010,9 +996,9 @@ Model.prototype.sync = function(options) {
return self.runHooks('afterSync', options); return self.runHooks('afterSync', options);
} }
}).return(this); }).return(this);
}; }
/** /**
* Drop the table represented by this Model * Drop the table represented by this Model
* @param {Object} [options] * @param {Object} [options]
* @param {Boolean} [options.cascade=false] Also drop all objects depending on this table, such as views. Only works in postgres * @param {Boolean} [options.cascade=false] Also drop all objects depending on this table, such as views. Only works in postgres
...@@ -1020,15 +1006,15 @@ Model.prototype.sync = function(options) { ...@@ -1020,15 +1006,15 @@ Model.prototype.sync = function(options) {
* @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging). * @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging).
* @return {Promise} * @return {Promise}
*/ */
Model.prototype.drop = function(options) { static drop(options) {
return this.QueryInterface.dropTable(this.getTableName(options), options); return this.QueryInterface.dropTable(this.getTableName(options), options);
}; }
Model.prototype.dropSchema = function(schema) { static dropSchema(schema) {
return this.QueryInterface.dropSchema(schema); return this.QueryInterface.dropSchema(schema);
}; }
/** /**
* Apply a schema to this model. For postgres, this will actually place the schema in front of the table name - `"schema"."tableName"`, * Apply a schema to this model. For postgres, this will actually place the schema in front of the table name - `"schema"."tableName"`,
* while the schema will be prepended to the table name for mysql and sqlite - `'schema.tablename'`. * while the schema will be prepended to the table name for mysql and sqlite - `'schema.tablename'`.
* *
...@@ -1048,9 +1034,10 @@ Model.prototype.dropSchema = function(schema) { ...@@ -1048,9 +1034,10 @@ Model.prototype.dropSchema = function(schema) {
* *
* @return {this} * @return {this}
*/ */
Model.prototype.schema = function(schema, options) { // testhint options:none static schema(schema, options) { // testhint options:none
var self = this;
var clone = Object.create(self); var clone = class extends this {};
Object.defineProperty(clone, 'name', {value: this.name});
clone.$schema = schema; clone.$schema = schema;
...@@ -1064,16 +1051,10 @@ Model.prototype.schema = function(schema, options) { // testhint options:none ...@@ -1064,16 +1051,10 @@ Model.prototype.schema = function(schema, options) { // testhint options:none
} }
} }
clone.Instance = function() {
self.Instance.apply(this, arguments);
};
clone.Instance.prototype = Object.create(self.Instance.prototype);
clone.Instance.prototype.$Model = clone;
return clone; return clone;
}; }
/** /**
* Get the tablename of the model, taking schema into account. The method will return The name as a string if the model has no schema, * Get the tablename of the model, taking schema into account. The method will return The name as a string if the model has no schema,
* or an object with `tableName`, `schema` and `delimiter` properties. * or an object with `tableName`, `schema` and `delimiter` properties.
* *
...@@ -1082,18 +1063,18 @@ Model.prototype.schema = function(schema, options) { // testhint options:none ...@@ -1082,18 +1063,18 @@ Model.prototype.schema = function(schema, options) { // testhint options:none
* @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging). * @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging).
* @return {String|Object} * @return {String|Object}
*/ */
Model.prototype.getTableName = function(options) { // testhint options:none static getTableName(options) { // testhint options:none
return this.QueryGenerator.addSchema(this); return this.QueryGenerator.addSchema(this);
}; }
/** /**
* @return {Model} * @return {Model}
*/ */
Model.prototype.unscoped = function () { static unscoped() {
return this.scope(); return this.scope();
}; }
/** /**
* 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. * 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. * 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.
...@@ -1103,7 +1084,7 @@ Model.prototype.unscoped = function () { ...@@ -1103,7 +1084,7 @@ Model.prototype.unscoped = function () {
* @param {Object} [options] * @param {Object} [options]
* @param {Boolean} [options.override=false] * @param {Boolean} [options.override=false]
*/ */
Model.prototype.addScope = function (name, scope, options) { static addScope(name, scope, options) {
options = _.assign({ options = _.assign({
override: false override: false
}, options); }, options);
...@@ -1112,16 +1093,16 @@ Model.prototype.addScope = function (name, scope, options) { ...@@ -1112,16 +1093,16 @@ Model.prototype.addScope = function (name, scope, options) {
throw new Error('The scope ' + name + ' already exists. Pass { override: true } as options to silence this error'); throw new Error('The scope ' + name + ' already exists. Pass { override: true } as options to silence this error');
} }
conformOptions(scope, this); this._conformOptions(scope, this);
if (name === 'defaultScope') { if (name === 'defaultScope') {
this.options.defaultScope = this.$scope = scope; this.options.defaultScope = this.$scope = scope;
} else { } else {
this.options.scopes[name] = scope; this.options.scopes[name] = scope;
} }
}; }
/** /**
* Apply a scope created in `define` to the model. First let's look at how to create scopes: * Apply a scope created in `define` to the model. First let's look at how to create scopes:
* ```js * ```js
* var Model = sequelize.define('model', attributes, { * var Model = sequelize.define('model', attributes, {
...@@ -1167,11 +1148,12 @@ Model.prototype.addScope = function (name, scope, options) { ...@@ -1167,11 +1148,12 @@ Model.prototype.addScope = function (name, scope, options) {
* @param {Array|Object|String|null} options* The scope(s) to apply. Scopes can either be passed as consecutive arguments, or as an array of arguments. To apply simple scopes and scope functions with no arguments, pass them as strings. For scope function, pass an object, with a `method` property. The value can either be a string, if the method does not take any arguments, or an array, where the first element is the name of the method, and consecutive elements are arguments to that method. Pass null to remove all scopes, including the default. * @param {Array|Object|String|null} options* The scope(s) to apply. Scopes can either be passed as consecutive arguments, or as an array of arguments. To apply simple scopes and scope functions with no arguments, pass them as strings. For scope function, pass an object, with a `method` property. The value can either be a string, if the method does not take any arguments, or an array, where the first element is the name of the method, and consecutive elements are arguments to that method. Pass null to remove all scopes, including the default.
* @return {Model} A reference to the model, with the scope(s) applied. Calling scope again on the returned model will clear the previous scope. * @return {Model} A reference to the model, with the scope(s) applied. Calling scope again on the returned model will clear the previous scope.
*/ */
Model.prototype.scope = function(option) { static scope(option) {
var self = Object.create(this) var self = class extends this {}
, options , options
, scope , scope
, scopeName; , scopeName;
Object.defineProperty(self, 'name', {value: this.name});
self.$scope = {}; self.$scope = {};
self.scoped = true; self.scoped = true;
...@@ -1207,7 +1189,7 @@ Model.prototype.scope = function(option) { ...@@ -1207,7 +1189,7 @@ Model.prototype.scope = function(option) {
if (_.isFunction(scope)) { if (_.isFunction(scope)) {
scope = scope(); scope = scope();
conformOptions(scope, self); this._conformOptions(scope, self);
} }
} }
} }
...@@ -1225,16 +1207,16 @@ Model.prototype.scope = function(option) { ...@@ -1225,16 +1207,16 @@ Model.prototype.scope = function(option) {
} else { } else {
throw new Error('Invalid scope ' + scopeName + ' called.'); throw new Error('Invalid scope ' + scopeName + ' called.');
} }
}); }.bind(this));
return self; return self;
}; }
Model.prototype.all = function(options) { static all(options) {
return this.findAll(options); return this.findAll(options);
}; }
/** /**
* Search for multiple instances. * Search for multiple instances.
* *
* __Simple search using AND and =__ * __Simple search using AND and =__
...@@ -1335,7 +1317,7 @@ Model.prototype.all = function(options) { ...@@ -1335,7 +1317,7 @@ Model.prototype.all = function(options) {
* @alias all * @alias all
*/ */
Model.prototype.findAll = function(options) { static findAll(options) {
if (options !== undefined && !_.isPlainObject(options)) { if (options !== undefined && !_.isPlainObject(options)) {
throw new Error('The argument passed to findAll must be an options object, use findById if you wish to pass a single primary key value'); throw new Error('The argument passed to findAll must be an options object, use findById if you wish to pass a single primary key value');
} }
...@@ -1355,14 +1337,14 @@ Model.prototype.findAll = function(options) { ...@@ -1355,14 +1337,14 @@ Model.prototype.findAll = function(options) {
options.rejectOnEmpty = options.rejectOnEmpty || this.options.rejectOnEmpty; options.rejectOnEmpty = options.rejectOnEmpty || this.options.rejectOnEmpty;
return Promise.bind(this).then(function() { return Promise.bind(this).then(function() {
conformOptions(options, this); this._conformOptions(options, this);
this.$injectScope(options); this.$injectScope(options);
if (options.hooks) { if (options.hooks) {
return this.runHooks('beforeFind', options); return this.runHooks('beforeFind', options);
} }
}).then(function() { }).then(function() {
expandIncludeAll.call(this, options); this._expandIncludeAll.call(this, options);
if (options.hooks) { if (options.hooks) {
return this.runHooks('beforeFindAfterExpandIncludeAll', options); return this.runHooks('beforeFindAfterExpandIncludeAll', options);
...@@ -1371,7 +1353,7 @@ Model.prototype.findAll = function(options) { ...@@ -1371,7 +1353,7 @@ Model.prototype.findAll = function(options) {
if (options.include) { if (options.include) {
options.hasJoin = true; options.hasJoin = true;
validateIncludedElements.call(this, options, tableNames); this._validateIncludedElements(options, tableNames);
// If we're not raw, we have to make sure we include the primary key for deduplication // If we're not raw, we have to make sure we include the primary key for deduplication
if (options.attributes && !options.raw && this.primaryKeyAttribute && options.attributes.indexOf(this.primaryKeyAttribute) === -1) { if (options.attributes && !options.raw && this.primaryKeyAttribute && options.attributes.indexOf(this.primaryKeyAttribute) === -1) {
...@@ -1389,7 +1371,7 @@ Model.prototype.findAll = function(options) { ...@@ -1389,7 +1371,7 @@ Model.prototype.findAll = function(options) {
Utils.mapFinderOptions(options, this); Utils.mapFinderOptions(options, this);
options = paranoidClause(this, options); options = this._paranoidClause(this, options);
if (options.hooks) { if (options.hooks) {
return this.runHooks('beforeFindAfterOptions', options); return this.runHooks('beforeFindAfterOptions', options);
...@@ -1417,9 +1399,9 @@ Model.prototype.findAll = function(options) { ...@@ -1417,9 +1399,9 @@ Model.prototype.findAll = function(options) {
return Model.$findSeparate(results, originalOptions); return Model.$findSeparate(results, originalOptions);
}); });
}; }
Model.$findSeparate = function(results, options) { static $findSeparate(results, options) {
if (!options.include || options.raw || !results) return Promise.resolve(results); if (!options.include || options.raw || !results) return Promise.resolve(results);
var original = results; var original = results;
...@@ -1465,21 +1447,21 @@ Model.$findSeparate = function(results, options) { ...@@ -1465,21 +1447,21 @@ Model.$findSeparate = function(results, options) {
}); });
}); });
}).return(original); }).return(original);
}; }
/** /**
* Search for a single instance by its primary key. * Search for a single instance by its primary key.
* *
* @param {Number|String|Buffer} id The value of the desired instance's primary key. * @param {Number|String|Buffer} id The value of the desired instance's primary key.
* @param {Object} [options] * @param {Object} [options]
* @param {Transaction} [options.transaction] Transaction to run query under * @param {Transaction} [options.transaction] Transaction to run query under
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only) * @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
* *
* @see {Model#findAll} for a full explanation of options * @see {Model#findAll} for a full explanation of options
* @return {Promise<Instance>} * @return {Promise<Instance>}
* @alias findByPrimary * @alias findByPrimary
*/ */
Model.prototype.findById = function(param, options) { static findById(param, options) {
// return Promise resolved with null if no arguments are passed // return Promise resolved with null if no arguments are passed
if ([null, undefined].indexOf(param) !== -1) { if ([null, undefined].indexOf(param) !== -1) {
return Promise.resolve(null); return Promise.resolve(null);
...@@ -1495,22 +1477,21 @@ Model.prototype.findById = function(param, options) { ...@@ -1495,22 +1477,21 @@ Model.prototype.findById = function(param, options) {
} }
// Bypass a possible overloaded findOne // Bypass a possible overloaded findOne
return Model.prototype.findOne.call(this, options); return this.findOne(options);
}; }
Model.prototype.findByPrimary = Model.prototype.findById;
/** /**
* Search for a single instance. This applies LIMIT 1, so the listener will always be called with a single instance. * Search for a single instance. This applies LIMIT 1, so the listener will always be called with a single instance.
* *
* @param {Object} [options] A hash of options to describe the scope of the search * @param {Object} [options] A hash of options to describe the scope of the search
* @param {Transaction} [options.transaction] Transaction to run query under * @param {Transaction} [options.transaction] Transaction to run query under
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only) * @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
* *
* @see {Model#findAll} for an explanation of options * @see {Model#findAll} for an explanation of options
* @return {Promise<Instance>} * @return {Promise<Instance>}
* @alias find * @alias find
*/ */
Model.prototype.findOne = function(options) { static findOne(options) {
if (options !== undefined && !_.isPlainObject(options)) { if (options !== undefined && !_.isPlainObject(options)) {
throw new Error('The argument passed to findOne must be an options object, use findById if you wish to pass a single primary key value'); throw new Error('The argument passed to findOne must be an options object, use findById if you wish to pass a single primary key value');
} }
...@@ -1526,14 +1507,13 @@ Model.prototype.findOne = function(options) { ...@@ -1526,14 +1507,13 @@ Model.prototype.findOne = function(options) {
} }
// Bypass a possible overloaded findAll. // Bypass a possible overloaded findAll.
return Model.prototype.findAll.call(this, _.defaults(options, { return this.findAll(_.defaults(options, {
plain: true, plain: true,
rejectOnEmpty: false rejectOnEmpty: false
})); }));
}; }
Model.prototype.find = Model.prototype.findOne;
/** /**
* Run an aggregation method on the specified field * Run an aggregation method on the specified field
* *
* @param {String} field The field to aggregate over. Can be a field name or * * @param {String} field The field to aggregate over. Can be a field name or *
...@@ -1549,15 +1529,15 @@ Model.prototype.find = Model.prototype.findOne; ...@@ -1549,15 +1529,15 @@ Model.prototype.find = Model.prototype.findOne;
* *
* @return {Promise<options.dataType|object>} Returns the aggregate result cast to `options.dataType`, unless `options.plain` is false, in which case the complete data result is returned. * @return {Promise<options.dataType|object>} Returns the aggregate result cast to `options.dataType`, unless `options.plain` is false, in which case the complete data result is returned.
*/ */
Model.prototype.aggregate = function(attribute, aggregateFunction, options) { static aggregate(attribute, aggregateFunction, options) {
options = Utils.cloneDeep(options); options = Utils.cloneDeep(options);
options = _.defaults(options, { attributes: [] }); options = _.defaults(options, { attributes: [] });
conformOptions(options, this); this._conformOptions(options, this);
this.$injectScope(options); this.$injectScope(options);
if (options.include) { if (options.include) {
expandIncludeAll.call(this, options); this._expandIncludeAll(options);
validateIncludedElements.call(this, options); this._validateIncludedElements(options);
} }
var attrOptions = this.rawAttributes[attribute] var attrOptions = this.rawAttributes[attribute]
...@@ -1581,12 +1561,12 @@ Model.prototype.aggregate = function(attribute, aggregateFunction, options) { ...@@ -1581,12 +1561,12 @@ Model.prototype.aggregate = function(attribute, aggregateFunction, options) {
} }
Utils.mapOptionFieldNames(options, this); Utils.mapOptionFieldNames(options, this);
options = paranoidClause(this, options); options = this._paranoidClause(this, options);
return this.QueryInterface.rawSelect(this.getTableName(options), options, aggregateFunction, this); return this.QueryInterface.rawSelect(this.getTableName(options), options, aggregateFunction, this);
}; }
/** /**
* Count the number of records matching the provided where clause. * Count the number of records matching the provided where clause.
* *
* If you provide an `include` option, the number of matching associations will be counted instead. * If you provide an `include` option, the number of matching associations will be counted instead.
...@@ -1604,14 +1584,14 @@ Model.prototype.aggregate = function(attribute, aggregateFunction, options) { ...@@ -1604,14 +1584,14 @@ Model.prototype.aggregate = function(attribute, aggregateFunction, options) {
* *
* @return {Promise<Integer>} * @return {Promise<Integer>}
*/ */
Model.prototype.count = function(options) { static count(options) {
options = Utils.cloneDeep(options); options = Utils.cloneDeep(options);
_.defaults(options, { hooks: true }); _.defaults(options, { hooks: true });
var col = '*'; var col = '*';
return Promise.bind(this).then(function() { return Promise.bind(this).then(function() {
conformOptions(options, this); this._conformOptions(options, this);
this.$injectScope(options); this.$injectScope(options);
if (options.hooks) { if (options.hooks) {
...@@ -1620,8 +1600,8 @@ Model.prototype.count = function(options) { ...@@ -1620,8 +1600,8 @@ Model.prototype.count = function(options) {
}).then(function() { }).then(function() {
if (options.include) { if (options.include) {
col = this.name + '.' + this.primaryKeyField; col = this.name + '.' + this.primaryKeyField;
expandIncludeAll.call(this, options); this._expandIncludeAll(options);
validateIncludedElements.call(this, options); this._validateIncludedElements(options);
} }
Utils.mapOptionFieldNames(options, this); Utils.mapOptionFieldNames(options, this);
...@@ -1636,9 +1616,9 @@ Model.prototype.count = function(options) { ...@@ -1636,9 +1616,9 @@ Model.prototype.count = function(options) {
return this.aggregate(col, 'count', options); return this.aggregate(col, 'count', options);
}); });
}; }
/** /**
* 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 useful 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 useful for paging
* *
* ```js * ```js
...@@ -1671,7 +1651,7 @@ Model.prototype.count = function(options) { ...@@ -1671,7 +1651,7 @@ Model.prototype.count = function(options) {
* @return {Promise<Object>} * @return {Promise<Object>}
* @alias findAndCountAll * @alias findAndCountAll
*/ */
Model.prototype.findAndCount = function(options) { static findAndCount(options) {
if (options !== undefined && !_.isPlainObject(options)) { if (options !== undefined && !_.isPlainObject(options)) {
throw new Error('The argument passed to findAndCount must be an options object, use findById if you wish to pass a single primary key value'); throw new Error('The argument passed to findAndCount must be an options object, use findById if you wish to pass a single primary key value');
} }
...@@ -1680,7 +1660,7 @@ Model.prototype.findAndCount = function(options) { ...@@ -1680,7 +1660,7 @@ Model.prototype.findAndCount = function(options) {
// no limit, offset, order, attributes for the options given to count() // no limit, offset, order, attributes for the options given to count()
, countOptions = _.omit(_.clone(options), ['offset', 'limit', 'order', 'attributes']); , countOptions = _.omit(_.clone(options), ['offset', 'limit', 'order', 'attributes']);
conformOptions(countOptions, this); this._conformOptions(countOptions, this);
if (countOptions.include) { if (countOptions.include) {
countOptions.include = _.cloneDeepWith(countOptions.include, function (element) { countOptions.include = _.cloneDeepWith(countOptions.include, function (element) {
...@@ -1689,9 +1669,9 @@ Model.prototype.findAndCount = function(options) { ...@@ -1689,9 +1669,9 @@ Model.prototype.findAndCount = function(options) {
return undefined; return undefined;
}); });
expandIncludeAll.call(this, countOptions); this._expandIncludeAll(countOptions);
validateIncludedElements.call(this, countOptions); this._validateIncludedElements(countOptions);
var keepNeeded = function(includes) { var keepNeeded = function(includes) {
return includes.filter(function (include) { return includes.filter(function (include) {
...@@ -1722,11 +1702,9 @@ Model.prototype.findAndCount = function(options) { ...@@ -1722,11 +1702,9 @@ Model.prototype.findAndCount = function(options) {
}; };
}); });
}); });
}; }
Model.prototype.findAndCountAll = Model.prototype.findAndCount;
/** /**
* Find the maximum value of field * Find the maximum value of field
* *
* @param {String} field * @param {String} field
...@@ -1735,11 +1713,11 @@ Model.prototype.findAndCountAll = Model.prototype.findAndCount; ...@@ -1735,11 +1713,11 @@ Model.prototype.findAndCountAll = Model.prototype.findAndCount;
* *
* @return {Promise<Any>} * @return {Promise<Any>}
*/ */
Model.prototype.max = function(field, options) { static max(field, options) {
return this.aggregate(field, 'max', options); return this.aggregate(field, 'max', options);
}; }
/** /**
* Find the minimum value of field * Find the minimum value of field
* *
* @param {String} field * @param {String} field
...@@ -1748,11 +1726,11 @@ Model.prototype.max = function(field, options) { ...@@ -1748,11 +1726,11 @@ Model.prototype.max = function(field, options) {
* *
* @return {Promise<Any>} * @return {Promise<Any>}
*/ */
Model.prototype.min = function(field, options) { static min(field, options) {
return this.aggregate(field, 'min', options); return this.aggregate(field, 'min', options);
}; }
/** /**
* Find the sum of field * Find the sum of field
* *
* @param {String} field * @param {String} field
...@@ -1761,11 +1739,11 @@ Model.prototype.min = function(field, options) { ...@@ -1761,11 +1739,11 @@ Model.prototype.min = function(field, options) {
* *
* @return {Promise<Number>} * @return {Promise<Number>}
*/ */
Model.prototype.sum = function(field, options) { static sum(field, options) {
return this.aggregate(field, 'sum', options); return this.aggregate(field, 'sum', options);
}; }
/** /**
* Builds a new model instance. Values is an object of key value pairs, must be defined but can be empty. * Builds a new model instance. Values is an object of key value pairs, must be defined but can be empty.
* @param {Object} values * @param {Object} values
...@@ -1776,7 +1754,7 @@ Model.prototype.sum = function(field, options) { ...@@ -1776,7 +1754,7 @@ Model.prototype.sum = function(field, options) {
* *
* @return {Instance} * @return {Instance}
*/ */
Model.prototype.build = function(values, options) { // testhint options:none static build(values, options) { // testhint options:none
if (Array.isArray(values)) { if (Array.isArray(values)) {
return this.bulkBuild(values, options); return this.bulkBuild(values, options);
} }
...@@ -1793,27 +1771,26 @@ Model.prototype.build = function(values, options) { // testhint options:none ...@@ -1793,27 +1771,26 @@ Model.prototype.build = function(values, options) { // testhint options:none
} }
if (!options.includeValidated) { if (!options.includeValidated) {
conformOptions(options, this); this._conformOptions(options, this);
if (options.include) { if (options.include) {
expandIncludeAll.call(this, options); this._expandIncludeAll(options);
validateIncludedElements.call(this, options); this._validateIncludedElements(options);
} }
} }
return new this.Instance(values, options); return new this(values, options);
}; }
Model.prototype.bulkBuild = function(valueSets, options) { // testhint options:none static bulkBuild(valueSets, options) { // testhint options:none
options = _.extend({ options = _.extend({
isNewRecord: true isNewRecord: true
}, options || {}); }, options || {});
if (!options.includeValidated) { if (!options.includeValidated) {
conformOptions(options, this); this._conformOptions(options, this);
if (options.include) { if (options.include) {
expandIncludeAll.call(this, options); this._expandIncludeAll(options);
validateIncludedElements.call(this, options); this._validateIncludedElements(options);
} }
} }
...@@ -1826,9 +1803,9 @@ Model.prototype.bulkBuild = function(valueSets, options) { // testhint options:n ...@@ -1826,9 +1803,9 @@ Model.prototype.bulkBuild = function(valueSets, options) { // testhint options:n
return valueSets.map(function(values) { return valueSets.map(function(values) {
return this.build(values, options); return this.build(values, options);
}.bind(this)); }.bind(this));
}; }
/** /**
* Builds a new model instance and calls save on it. * Builds a new model instance and calls save on it.
* @see {Instance#build} * @see {Instance#build}
...@@ -1848,7 +1825,7 @@ Model.prototype.bulkBuild = function(valueSets, options) { // testhint options:n ...@@ -1848,7 +1825,7 @@ Model.prototype.bulkBuild = function(valueSets, options) { // testhint options:n
* *
* @return {Promise<Instance>} * @return {Promise<Instance>}
*/ */
Model.prototype.create = function(values, options) { static create(values, options) {
options = Utils.cloneDeep(options || {}); options = Utils.cloneDeep(options || {});
return this.build(values, { return this.build(values, {
...@@ -1858,9 +1835,9 @@ Model.prototype.create = function(values, options) { ...@@ -1858,9 +1835,9 @@ Model.prototype.create = function(values, options) {
raw: options.raw, raw: options.raw,
silent: options.silent silent: options.silent
}).save(options); }).save(options);
}; }
/** /**
* Find a row that matches the query, or build (but don't save) the row if none is found. * Find a row that matches the query, or build (but don't save) the row if none is found.
* The successful result of the promise will be (instance, initialized) - Make sure to use .spread() * The successful result of the promise will be (instance, initialized) - Make sure to use .spread()
* *
...@@ -1874,7 +1851,7 @@ Model.prototype.create = function(values, options) { ...@@ -1874,7 +1851,7 @@ Model.prototype.create = function(values, options) {
* @return {Promise<Instance,initialized>} * @return {Promise<Instance,initialized>}
* @alias findOrBuild * @alias findOrBuild
*/ */
Model.prototype.findOrInitialize = Model.prototype.findOrBuild = function(options) { static findOrBuild(options) {
if (!options || !options.where || arguments.length > 1) { if (!options || !options.where || arguments.length > 1) {
throw new Error( throw new Error(
'Missing where attribute in the options parameter passed to findOrInitialize. ' + 'Missing where attribute in the options parameter passed to findOrInitialize. ' +
...@@ -1899,9 +1876,9 @@ Model.prototype.findOrInitialize = Model.prototype.findOrBuild = function(option ...@@ -1899,9 +1876,9 @@ Model.prototype.findOrInitialize = Model.prototype.findOrBuild = function(option
return Promise.resolve([instance, false]); return Promise.resolve([instance, false]);
}); });
}; }
/** /**
* Find a row that matches the query, or build and save the row if none is found * Find a row that matches the query, or build and save the row if none is found
* The successful result of the promise will be (instance, created) - Make sure to use .spread() * The successful result of the promise will be (instance, created) - Make sure to use .spread()
* *
...@@ -1916,7 +1893,7 @@ Model.prototype.findOrInitialize = Model.prototype.findOrBuild = function(option ...@@ -1916,7 +1893,7 @@ Model.prototype.findOrInitialize = Model.prototype.findOrBuild = function(option
* @see {Model#findAll} for a full specification of find and options * @see {Model#findAll} for a full specification of find and options
* @return {Promise<Instance,created>} * @return {Promise<Instance,created>}
*/ */
Model.prototype.findOrCreate = function(options) { static findOrCreate(options) {
if (!options || !options.where || arguments.length > 1) { if (!options || !options.where || arguments.length > 1) {
throw new Error( throw new Error(
'Missing where attribute in the options parameter passed to findOrCreate. '+ 'Missing where attribute in the options parameter passed to findOrCreate. '+
...@@ -1992,9 +1969,9 @@ Model.prototype.findOrCreate = function(options) { ...@@ -1992,9 +1969,9 @@ Model.prototype.findOrCreate = function(options) {
return transaction.commit(); return transaction.commit();
} }
}); });
}; }
/** /**
* A more performant findOrCreate that will not work under a transaction (at least not in postgres) * A more performant findOrCreate that will not work under a transaction (at least not in postgres)
* Will execute a find call, if empty then attempt to create, if unique constraint then attempt to find again * Will execute a find call, if empty then attempt to create, if unique constraint then attempt to find again
* *
...@@ -2004,7 +1981,7 @@ Model.prototype.findOrCreate = function(options) { ...@@ -2004,7 +1981,7 @@ Model.prototype.findOrCreate = function(options) {
* @see {Model#findAll} for a full specification of find and options * @see {Model#findAll} for a full specification of find and options
* @return {Promise<Instance,created>} * @return {Promise<Instance,created>}
*/ */
Model.prototype.findCreateFind = function(options) { static findCreateFind(options) {
if (!options || !options.where) { if (!options || !options.where) {
throw new Error( throw new Error(
'Missing where attribute in the options parameter passed to findOrCreate.' 'Missing where attribute in the options parameter passed to findOrCreate.'
...@@ -2028,9 +2005,9 @@ Model.prototype.findCreateFind = function(options) { ...@@ -2028,9 +2005,9 @@ Model.prototype.findCreateFind = function(options) {
}); });
}); });
}); });
}; }
/** /**
* 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:**
...@@ -2053,7 +2030,7 @@ Model.prototype.findCreateFind = function(options) { ...@@ -2053,7 +2030,7 @@ Model.prototype.findCreateFind = function(options) {
* @alias insertOrUpdate * @alias insertOrUpdate
* @return {Promise<created>} Returns a boolean indicating whether the row was created or updated. * @return {Promise<created>} Returns a boolean indicating whether the row was created or updated.
*/ */
Model.prototype.upsert = function (values, options) { static upsert(values, options) {
options = Utils.cloneDeep(options) || {}; options = Utils.cloneDeep(options) || {};
if (!options.fields) { if (!options.fields) {
...@@ -2089,11 +2066,9 @@ Model.prototype.upsert = function (values, options) { ...@@ -2089,11 +2066,9 @@ Model.prototype.upsert = function (values, options) {
return this.QueryInterface.upsert(this.getTableName(options), insertValues, updateValues, instance.where(), this, options); return this.QueryInterface.upsert(this.getTableName(options), insertValues, updateValues, instance.where(), this, options);
}); });
}; }
Model.prototype.insertOrUpdate = Model.prototype.upsert;
/** /**
* Create and insert multiple instances in bulk. * Create and insert multiple instances in bulk.
* *
* 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
...@@ -2118,7 +2093,7 @@ Model.prototype.insertOrUpdate = Model.prototype.upsert; ...@@ -2118,7 +2093,7 @@ Model.prototype.insertOrUpdate = Model.prototype.upsert;
* *
* @return {Promise<Array<Instance>>} * @return {Promise<Array<Instance>>}
*/ */
Model.prototype.bulkCreate = function(records, options) { static bulkCreate(records, options) {
if (!records.length) { if (!records.length) {
return Promise.resolve([]); return Promise.resolve([]);
} }
...@@ -2246,14 +2221,14 @@ Model.prototype.bulkCreate = function(records, options) { ...@@ -2246,14 +2221,14 @@ Model.prototype.bulkCreate = function(records, options) {
}).then(function() { }).then(function() {
return instances; return instances;
}); });
}; }
/** /**
* 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 }).
* *
* @param {object} [options] The options passed to Model.destroy in addition to truncate * @param {object} [options] The options passed to Model.destroy in addition to truncate
* @param {Boolean|function} [options.transaction] Transaction to run query under * @param {Boolean|function} [options.transaction] Transaction to run query under
* @param {Boolean|function} [options.cascade=false] Only used in conjunction 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. * @param {Boolean|function} [options.cascade = false] Only used in conjunction 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.
* @param {Transaction} [options.transaction] Transaction to run query under * @param {Transaction} [options.transaction] Transaction to run query under
* @param {Boolean|function} [options.logging] A function that logs sql queries, or false for no logging * @param {Boolean|function} [options.logging] A function that logs sql queries, or false for no logging
* @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging). * @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging).
...@@ -2263,13 +2238,13 @@ Model.prototype.bulkCreate = function(records, options) { ...@@ -2263,13 +2238,13 @@ Model.prototype.bulkCreate = function(records, options) {
* *
* @see {Model#destroy} for more information * @see {Model#destroy} for more information
*/ */
Model.prototype.truncate = function(options) { static truncate(options) {
options = Utils.cloneDeep(options) || {}; options = Utils.cloneDeep(options) || {};
options.truncate = true; options.truncate = true;
return this.destroy(options); return this.destroy(options);
}; }
/** /**
* Delete multiple instances, or set their deletedAt timestamp to the current time if `paranoid` is enabled. * Delete multiple instances, or set their deletedAt timestamp to the current time if `paranoid` is enabled.
* *
* @param {Object} options * @param {Object} options
...@@ -2285,7 +2260,7 @@ Model.prototype.truncate = function(options) { ...@@ -2285,7 +2260,7 @@ Model.prototype.truncate = function(options) {
* @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging). * @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging).
* @return {Promise<Integer>} The number of destroyed rows * @return {Promise<Integer>} The number of destroyed rows
*/ */
Model.prototype.destroy = function(options) { static destroy(options) {
var self = this var self = this
, instances; , instances;
...@@ -2357,23 +2332,23 @@ Model.prototype.destroy = function(options) { ...@@ -2357,23 +2332,23 @@ Model.prototype.destroy = function(options) {
}).then(function(affectedRows) { }).then(function(affectedRows) {
return affectedRows; return affectedRows;
}); });
}; }
/** /**
* Restore multiple instances if `paranoid` is enabled. * Restore multiple instances if `paranoid` is enabled.
* *
* @param {Object} options * @param {Object} options
* @param {Object} [options.where] Filter the restore * @param {Object} [options.where] Filter the restore
* @param {Boolean} [options.hooks=true] Run before / after bulk restore hooks? * @param {Boolean} [options.hooks=true] Run before / after bulk restore hooks?
* @param {Boolean} [options.individualHooks=false] If set to true, restore will find all records within the where parameter and will execute before / after bulkRestore hooks on each row * @param {Boolean} [options.individualHooks=false] If set to true, restore will find all records within the where parameter and will execute before / after bulkRestore hooks on each row
* @param {Number} [options.limit] How many rows to undelete * @param {Number} [options.limit] How many rows to undelete (only for mysql)
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql. * @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging). * @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging).
* @param {Transaction} [options.transaction] Transaction to run query under * @param {Transaction} [options.transaction] Transaction to run query under
* *
* @return {Promise<undefined>} * @return {Promise<undefined>}
*/ */
Model.prototype.restore = function(options) { static restore(options) {
if (!this._timestampAttributes.deletedAt) throw new Error('Model is not paranoid'); if (!this._timestampAttributes.deletedAt) throw new Error('Model is not paranoid');
options = Utils._.extend({ options = Utils._.extend({
...@@ -2430,9 +2405,9 @@ Model.prototype.restore = function(options) { ...@@ -2430,9 +2405,9 @@ Model.prototype.restore = function(options) {
}).then(function(affectedRows) { }).then(function(affectedRows) {
return affectedRows; return affectedRows;
}); });
}; }
/** /**
* Update multiple instances that match the where options. The promise returns an array with one or two elements. The first element is always the number * Update multiple instances that match the where options. The promise returns an array with one or two elements. The first element is always the number
* of affected rows, while the second element is the actual affected rows (only supported in postgres with `options.returning` true.) * of affected rows, while the second element is the actual affected rows (only supported in postgres with `options.returning` true.)
* *
...@@ -2445,7 +2420,7 @@ Model.prototype.restore = function(options) { ...@@ -2445,7 +2420,7 @@ Model.prototype.restore = function(options) {
* @param {Boolean} [options.sideEffects=true] Whether or not to update the side effects of any virtual setters. * @param {Boolean} [options.sideEffects=true] Whether or not to update the side effects of any virtual setters.
* @param {Boolean} [options.individualHooks=false] Run before / after update hooks?. If true, this will execute a SELECT followed by individual UPDATEs. A select is needed, because the row data needs to be passed to the hooks * @param {Boolean} [options.individualHooks=false] Run before / after update hooks?. If true, this will execute a SELECT followed by individual UPDATEs. A select is needed, because the row data needs to be passed to the hooks
* @param {Boolean} [options.returning=false] Return the affected rows (only for postgres) * @param {Boolean} [options.returning=false] Return the affected rows (only for postgres)
* @param {Number} [options.limit] How many rows to update (only for mysql) * @param {Number} [options.limit] How many rows to update (only for mysql and mariadb)
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql. * @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging). * @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging).
* @param {Transaction} [options.transaction] Transaction to run query under * @param {Transaction} [options.transaction] Transaction to run query under
...@@ -2453,7 +2428,7 @@ Model.prototype.restore = function(options) { ...@@ -2453,7 +2428,7 @@ Model.prototype.restore = function(options) {
* *
* @return {Promise<Array<affectedCount,affectedRows>>} * @return {Promise<Array<affectedCount,affectedRows>>}
*/ */
Model.prototype.update = function(values, options) { static update(values, options) {
var self = this; var self = this;
if (!options || !options.where) { if (!options || !options.where) {
...@@ -2649,25 +2624,25 @@ Model.prototype.update = function(values, options) { ...@@ -2649,25 +2624,25 @@ Model.prototype.update = function(values, options) {
// Return result in form [affectedRows, instances] (instances missed off if options.individualHooks != true) // Return result in form [affectedRows, instances] (instances missed off if options.individualHooks != true)
return result; return result;
}); });
}; }
/** /**
* Run a describe query on the table. The result will be return to the listener as a hash of attributes and their types. * Run a describe query on the table. The result will be return to the listener as a hash of attributes and their types.
* *
* @return {Promise} * @return {Promise}
*/ */
Model.prototype.describe = function(schema, options) { static describe(schema, options) {
return this.QueryInterface.describeTable(this.tableName, _.assign({schema: schema || this.$schema || undefined}, options)); return this.QueryInterface.describeTable(this.tableName, _.assign({schema: schema || this.$schema || undefined}, options));
}; }
Model.prototype.$getDefaultTimestamp = function(attr) { static $getDefaultTimestamp(attr) {
if (!!this.rawAttributes[attr] && !!this.rawAttributes[attr].defaultValue) { if (!!this.rawAttributes[attr] && !!this.rawAttributes[attr].defaultValue) {
return Utils.toDefaultValue(this.rawAttributes[attr].defaultValue); return Utils.toDefaultValue(this.rawAttributes[attr].defaultValue);
} }
return undefined; return undefined;
}; }
Model.prototype.$expandAttributes = function (options) { static $expandAttributes(options) {
if (_.isPlainObject(options.attributes)) { if (_.isPlainObject(options.attributes)) {
var attributes = Object.keys(this.rawAttributes); var attributes = Object.keys(this.rawAttributes);
...@@ -2682,10 +2657,10 @@ Model.prototype.$expandAttributes = function (options) { ...@@ -2682,10 +2657,10 @@ Model.prototype.$expandAttributes = function (options) {
options.attributes = attributes; options.attributes = attributes;
} }
}; }
// Inject $scope into options. Includes should have been conformed (conformOptions) before calling this // Inject $scope into options. Includes should have been conformed (conformOptions) before calling this
Model.prototype.$injectScope = function (options) { static $injectScope(options) {
var self = this; var self = this;
var scope = Utils.cloneDeep(this.$scope); var scope = Utils.cloneDeep(this.$scope);
...@@ -2715,13 +2690,1056 @@ Model.prototype.$injectScope = function (options) { ...@@ -2715,13 +2690,1056 @@ Model.prototype.$injectScope = function (options) {
} }
}); });
} }
}; }
Model.prototype.inspect = function() { static inspect() {
return this.name; return this.name;
}; }
constructor(values, options) {
this.dataValues = {};
this._previousDataValues = {};
this._changed = {};
this.$modelOptions = this.constructor.options;
this.$options = options || {};
this.hasPrimaryKeys = this.constructor.options.hasPrimaryKeys;
this.__eagerlyLoadedAssociations = [];
/**
* Returns true if this instance has not yet been persisted to the database
* @property isNewRecord
* @return {Boolean}
*/
this.isNewRecord = options.isNewRecord;
/**
* Returns the Model the instance was created from.
* @see {Model}
* @property Model
* @return {Model}
*/
this._initValues(values, options);
}
_initValues(values, options) {
var defaults
, key;
values = values && _.clone(values) || {};
if (options.isNewRecord) {
defaults = {};
if (this.constructor._hasDefaultValues) {
defaults = _.mapValues(this.constructor._defaultValues, function(valueFn) {
var value = valueFn();
return (value && value._isSequelizeMethod) ? value : _.cloneDeep(value);
});
}
// set id to null if not passed as value, a newly created dao has no id
// removing this breaks bulkCreate
// do after default values since it might have UUID as a default value
if (!defaults.hasOwnProperty(this.constructor.primaryKeyAttribute)) {
defaults[this.constructor.primaryKeyAttribute] = null;
}
if (this.constructor._timestampAttributes.createdAt && defaults[this.constructor._timestampAttributes.createdAt]) {
this.dataValues[this.constructor._timestampAttributes.createdAt] = Utils.toDefaultValue(defaults[this.constructor._timestampAttributes.createdAt]);
delete defaults[this.constructor._timestampAttributes.createdAt];
}
if (this.constructor._timestampAttributes.updatedAt && defaults[this.constructor._timestampAttributes.updatedAt]) {
this.dataValues[this.constructor._timestampAttributes.updatedAt] = Utils.toDefaultValue(defaults[this.constructor._timestampAttributes.updatedAt]);
delete defaults[this.constructor._timestampAttributes.updatedAt];
}
if (this.constructor._timestampAttributes.deletedAt && defaults[this.constructor._timestampAttributes.deletedAt]) {
this.dataValues[this.constructor._timestampAttributes.deletedAt] = Utils.toDefaultValue(defaults[this.constructor._timestampAttributes.deletedAt]);
delete defaults[this.constructor._timestampAttributes.deletedAt];
}
if (Object.keys(defaults).length) {
for (key in defaults) {
if (values[key] === undefined) {
this.set(key, Utils.toDefaultValue(defaults[key]), defaultsOptions);
delete values[key];
}
}
}
}
this.set(values, options);
}
/**
* A reference to the sequelize instance
* @see {Sequelize}
* @property sequelize
* @return {Sequelize}
*/
get sequelize() {
return this.constructor.modelManager.sequelize;
}
/**
* Get an object representing the query for this instance, use with `options.where`
*
* @property where
* @return {Object}
*/
where() {
var where;
where = this.constructor.primaryKeyAttributes.reduce(function (result, attribute) {
result[attribute] = this.get(attribute, {raw: true});
return result;
}.bind(this), {});
if (_.size(where) === 0) {
return this.$modelOptions.whereCollection;
}
return Utils.mapWhereFieldNames(where, this.constructor);
}
toString() {
return '[object SequelizeInstance:'+this.constructor.name+']';
}
/**
* Get the value of the underlying data value
*
* @param {String} key
* @return {any}
*/
getDataValue(key) {
return this.dataValues[key];
}
/**
* Update the underlying data value
*
* @param {String} key
* @param {any} value
*/
setDataValue(key, value) {
var originalValue = this._previousDataValues[key];
if (!Utils.isPrimitive(value) || value !== originalValue) {
this.changed(key, true);
}
this.dataValues[key] = value;
}
/**
* If no key is given, returns all values of the instance, also invoking virtual getters.
*
* If key is given and a field or virtual getter is present for the key it will call that getter - else it will return the value for key.
*
* @param {String} [key]
* @param {Object} [options]
* @param {Boolean} [options.plain=false] If set to true, included instances will be returned as plain objects
* @param {Boolean} [options.raw=false] If set to true, field and virtual setters will be ignored
* @return {Object|any}
*/
get(key, options) { // testhint options:none
if (options === undefined && typeof key === 'object') {
options = key;
key = undefined;
}
options = options || {};
if (key) {
if (this._customGetters[key] && !options.raw) {
return this._customGetters[key].call(this, key);
}
if (options.plain && this.$options.include && this.$options.includeNames.indexOf(key) !== -1) {
if (Array.isArray(this.dataValues[key])) {
return this.dataValues[key].map(function (instance) {
return instance.get({plain: options.plain});
});
} else if (this.dataValues[key] instanceof Model) {
return this.dataValues[key].get({plain: options.plain});
} else {
return this.dataValues[key];
}
}
return this.dataValues[key];
}
if (this._hasCustomGetters || (options.plain && this.$options.include) || options.clone) {
var values = {}
, _key;
if (this._hasCustomGetters) {
for (_key in this._customGetters) {
if (this._customGetters.hasOwnProperty(_key)) {
values[_key] = this.get(_key, options);
}
}
}
for (_key in this.dataValues) {
if (!values.hasOwnProperty(_key) && this.dataValues.hasOwnProperty(_key)) {
values[_key] = this.get(_key, options);
}
}
return values;
}
return this.dataValues;
}
/**
* 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
* will be called instead. To bypass the setter, you can pass `raw: true` in the options object.
*
* If set is called with an object, it will loop over the object, and call set recursively for each key, value pair. If you set raw to true, the underlying dataValues will either be
* set directly to the object passed, or used to extend dataValues, if dataValues already contain values.
*
* When set is called, the previous value of the field is stored and sets a changed flag(see `changed`).
*
* Set can also be used to build instances for associations, if you have values for those.
* When using set with associations you need to make sure the property key matches the alias of the association
* while also making sure that the proper include options have been set (from .build() or .find())
*
* If called with a dot.separated key on a JSON/JSONB attribute it will set the value nested and flag the entire object as changed.
*
* @see {Model#find} for more information about includes
* @param {String|Object} key
* @param {any} value
* @param {Object} [options]
* @param {Boolean} [options.raw=false] If set to true, field and virtual setters will be ignored
* @param {Boolean} [options.reset=false] Clear all previously set data values
* @alias setAttributes
*/
set(key, value, options) { // testhint options:none
var values
, originalValue
, keys
, i
, length;
if (typeof key === 'object' && key !== null) {
values = key;
options = value || {};
if (options.reset) {
this.dataValues = {};
}
// If raw, and we're not dealing with includes or special attributes, just set it straight on the dataValues object
if (options.raw && !(this.$options && this.$options.include) && !(options && options.attributes) && !this.constructor._hasBooleanAttributes && !this.constructor._hasDateAttributes) {
if (Object.keys(this.dataValues).length) {
this.dataValues = _.extend(this.dataValues, values);
} else {
this.dataValues = values;
}
// If raw, .changed() shouldn't be true
this._previousDataValues = _.clone(this.dataValues);
} else {
// Loop and call set
if (options.attributes) {
keys = options.attributes;
if (this.constructor._hasVirtualAttributes) {
keys = keys.concat(this.constructor._virtualAttributes);
}
if (this.$options.includeNames) {
keys = keys.concat(this.$options.includeNames);
}
for (i = 0, length = keys.length; i < length; i++) {
if (values[keys[i]] !== undefined) {
this.set(keys[i], values[keys[i]], options);
}
}
} else {
for (key in values) {
this.set(key, values[key], options);
}
}
if (options.raw) {
// If raw, .changed() shouldn't be true
this._previousDataValues = _.clone(this.dataValues);
}
}
} else {
if (!options)
options = {};
if (!options.raw) {
originalValue = this.dataValues[key];
}
// If not raw, and there's a customer setter
if (!options.raw && this._customSetters[key]) {
this._customSetters[key].call(this, value, key);
} else {
// Check if we have included models, and if this key matches the include model names/aliases
if (this.$options && this.$options.include && this.$options.includeNames.indexOf(key) !== -1) {
// Pass it on to the include handler
this._setInclude(key, value, options);
return this;
} else {
// Bunch of stuff we won't do when its raw
if (!options.raw) {
// If attribute is not in model definition, return
if (!this._isAttribute(key)) {
if (key.indexOf('.') > -1 && this.constructor._isJsonAttribute(key.split('.')[0])) {
var previousDottieValue = Dottie.get(this.dataValues, key);
if (!_.isEqual(previousDottieValue, value)) {
Dottie.set(this.dataValues, key, value);
this.changed(key.split('.')[0], true);
}
}
return this;
}
// If attempting to set primary key and primary key is already defined, return
if (this.constructor._hasPrimaryKeys && originalValue && this.constructor._isPrimaryKey(key)) {
return this;
}
// If attempting to set read only attributes, return
if (!this.isNewRecord && this.constructor._hasReadOnlyAttributes && this.constructor._isReadOnlyAttribute(key)) {
return this;
}
// Convert date fields to real date objects
if (this.constructor._hasDateAttributes && this.constructor._isDateAttribute(key) && !!value && !value._isSequelizeMethod) {
if (!(value instanceof Date)) {
value = new Date(value);
}
if (!(originalValue instanceof Date)) {
originalValue = new Date(originalValue);
}
if (originalValue && value.getTime() === originalValue.getTime()) {
return this;
}
}
}
// Convert boolean-ish values to booleans
if (this.constructor._hasBooleanAttributes && this.constructor._isBooleanAttribute(key) && value !== null && value !== undefined && !value._isSequelizeMethod) {
if (Buffer.isBuffer(value) && value.length === 1) {
// Bit fields are returned as buffers
value = value[0];
}
if (_.isString(value)) {
// Only take action on valid boolean strings.
value = (value === 'true') ? true : (value === 'false') ? false : value;
} else if (_.isNumber(value)) {
// Only take action on valid boolean integers.
value = (value === 1) ? true : (value === 0) ? false : value;
}
}
if (!options.raw && ((!Utils.isPrimitive(value) && value !== null) || value !== originalValue)) {
this._previousDataValues[key] = originalValue;
this.changed(key, true);
}
this.dataValues[key] = value;
}
}
}
return this;
}
setAttributes(updates) {
return this.set(updates);
}
/**
* 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 and no keys have changed, it will return `false`.
*
* @param {String} [key]
* @return {Boolean|Array}
*/
changed(key, value) {
if (key) {
if (value !== undefined) {
this._changed[key] = value;
return this;
}
return this._changed[key] || false;
}
var changed = Object.keys(this.dataValues).filter(function(key) {
return this.changed(key);
}.bind(this));
return changed.length ? changed : false;
}
/**
* Returns the previous value for key from `_previousDataValues`.
*
* If called without a key, returns the previous values for all values which have changed
*
* @param {String} [key]
* @return {any|Array<any>}
*/
previous(key) {
if (key) {
return this._previousDataValues[key];
}
return _.pickBy(this._previousDataValues, function(value, key) {
return this.changed(key);
}.bind(this));
}
_setInclude(key, value, options) {
if (!Array.isArray(value)) value = [value];
if (value[0] instanceof Model) {
value = value.map(function(instance) {
return instance.dataValues;
});
}
var include = this.$options.includeMap[key]
, association = include.association
, self = this
, accessor = key
, childOptions
, primaryKeyAttribute = include.model.primaryKeyAttribute
, isEmpty;
if (!isEmpty) {
childOptions = {
isNewRecord: this.isNewRecord,
include: include.include,
includeNames: include.includeNames,
includeMap: include.includeMap,
includeValidated: true,
raw: options.raw,
attributes: include.originalAttributes
};
}
if (include.originalAttributes === undefined || include.originalAttributes.length) {
if (association.isSingleAssociation) {
if (Array.isArray(value)) {
value = value[0];
}
isEmpty = (value && value[primaryKeyAttribute] === null) || (value === null);
self[accessor] = self.dataValues[accessor] = isEmpty ? null : include.model.build(value, childOptions);
} else {
isEmpty = value[0] && value[0][primaryKeyAttribute] === null;
self[accessor] = self.dataValues[accessor] = isEmpty ? [] : include.model.bulkBuild(value, childOptions);
}
}
}
/**
* 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`.
* This error will have a property for each of the fields for which validation failed, with the error message for that field.
*
* @param {Object} [options]
* @param {string[]} [options.fields] An optional array of strings, representing database columns. If fields is provided, only those columns will be validated and saved.
* @param {Boolean} [options.silent=false] If true, the updatedAt timestamp will not be updated.
* @param {Boolean} [options.validate=true] If false, validations won't be run.
* @param {Boolean} [options.hooks=true] Run before and after create / update + validate hooks
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
*
* @return {Promise<this|Errors.ValidationError>}
*/
save(options) {
if (arguments.length > 1) {
throw new Error('The second argument was removed in favor of the options object.');
}
options = Utils.cloneDeep(options);
options = _.defaults(options, {
hooks: true,
validate: true
});
if (!options.fields) {
if (this.isNewRecord) {
options.fields = Object.keys(this.constructor.attributes);
} else {
options.fields = _.intersection(this.changed(), Object.keys(this.constructor.attributes));
}
options.defaultFields = options.fields;
}
if (options.returning === undefined) {
if (options.association) {
options.returning = false;
} else if (this.isNewRecord) {
options.returning = true;
}
}
var self = this
, primaryKeyName = this.constructor.primaryKeyAttribute
, primaryKeyAttribute = primaryKeyName && this.constructor.rawAttributes[primaryKeyName]
, updatedAtAttr = this.constructor._timestampAttributes.updatedAt
, createdAtAttr = this.constructor._timestampAttributes.createdAt
, hook = self.isNewRecord ? 'Create' : 'Update'
, wasNewRecord = this.isNewRecord
, now = Utils.now(this.sequelize.options.dialect);
if (updatedAtAttr && options.fields.length >= 1 && options.fields.indexOf(updatedAtAttr) === -1) {
options.fields.push(updatedAtAttr);
}
if (options.silent === true && !(this.isNewRecord && this.get(updatedAtAttr, {raw: true}))) {
// UpdateAtAttr might have been added as a result of Object.keys(Model.attributes). In that case we have to remove it again
Utils._.remove(options.fields, function(val) {
return val === updatedAtAttr;
});
updatedAtAttr = false;
}
if (this.isNewRecord === true) {
if (createdAtAttr && options.fields.indexOf(createdAtAttr) === -1) {
options.fields.push(createdAtAttr);
}
if (primaryKeyAttribute && primaryKeyAttribute.defaultValue && options.fields.indexOf(primaryKeyName) < 0) {
options.fields.unshift(primaryKeyName);
}
}
if (this.isNewRecord === false) {
if (primaryKeyName && this.get(primaryKeyName, {raw: true}) === undefined) {
throw new Error('You attempted to save an instance with no primary key, this is not allowed since it would result in a global update');
}
}
if (updatedAtAttr && !options.silent && options.fields.indexOf(updatedAtAttr) !== -1) {
this.dataValues[updatedAtAttr] = this.constructor.$getDefaultTimestamp(updatedAtAttr) || now;
}
if (this.isNewRecord && createdAtAttr && !this.dataValues[createdAtAttr]) {
this.dataValues[createdAtAttr] = this.constructor.$getDefaultTimestamp(createdAtAttr) || now;
}
return Promise.bind(this).then(function() {
// Validate
if (options.validate) {
return this.validate(options);
}
}).then(function() {
return Promise.bind(this).then(function() {
// Run before hook
if (options.hooks) {
var beforeHookValues = _.pick(this.dataValues, options.fields)
, afterHookValues
, hookChanged
, ignoreChanged = _.difference(this.changed(), options.fields); // In case of update where it's only supposed to update the passed values and the hook values
if (updatedAtAttr && options.fields.indexOf(updatedAtAttr) !== -1) {
ignoreChanged = _.without(ignoreChanged, updatedAtAttr);
}
return this.constructor.runHooks('before' + hook, this, options).bind(this).then(function() {
if (options.defaultFields && !this.isNewRecord) {
afterHookValues = _.pick(this.dataValues, _.difference(this.changed(), ignoreChanged));
hookChanged = [];
Object.keys(afterHookValues).forEach(function (key) {
if (afterHookValues[key] !== beforeHookValues[key]) {
hookChanged.push(key);
}
});
options.fields = _.uniq(options.fields.concat(hookChanged));
}
if (hookChanged) {
if (options.validate) {
// Validate again
options.skip = _.difference(Object.keys(this.constructor.rawAttributes), hookChanged);
return this.validate(options).then(function() {
delete options.skip;
});
}
}
});
}
}).then(function() {
if (!options.fields.length) return this;
if (!this.isNewRecord) return this;
if (!this.$options.include || !this.$options.include.length) return this;
// Nested creation for BelongsTo relations
return Promise.map(this.$options.include.filter(function (include) {
return include.association instanceof BelongsTo;
}), function (include) {
var instance = self.get(include.as);
if (!instance) return Promise.resolve();
var includeOptions = _(Utils.cloneDeep(include))
.omit(['association'])
.defaults({
transaction: options.transaction,
logging: options.logging,
parentRecord: self
}).value();
return instance.save(includeOptions).then(function () {
return self[include.association.accessors.set](instance, {save: false, logging: options.logging});
});
});
})
.then(function() {
if (!options.fields.length) return this;
if (!this.changed() && !this.isNewRecord) return this;
var values = Utils.mapValueFieldNames(this.dataValues, options.fields, this.constructor)
, query = null
, args = [];
if (self.isNewRecord) {
query = 'insert';
args = [self, self.constructor.getTableName(options), values, options];
} else {
var where = this.where();
where = Utils.mapValueFieldNames(where, Object.keys(where), this.constructor);
query = 'update';
args = [self, self.constructor.getTableName(options), values, where, options];
}
return self.sequelize.getQueryInterface()[query].apply(self.sequelize.getQueryInterface(), args)
.then(function(result) {
// Transfer database generated values (defaults, autoincrement, etc)
Object.keys(self.constructor.rawAttributes).forEach(function (attr) {
if (self.constructor.rawAttributes[attr].field &&
values[self.constructor.rawAttributes[attr].field] !== undefined &&
self.constructor.rawAttributes[attr].field !== attr
) {
values[attr] = values[self.constructor.rawAttributes[attr].field];
delete values[self.constructor.rawAttributes[attr].field];
}
});
values = _.extend(values, result.dataValues);
result.dataValues = _.extend(result.dataValues, values);
return result;
})
.tap(function(result) {
if (!wasNewRecord) return self;
if (!self.$options.include || !self.$options.include.length) return self;
// Nested creation for HasOne/HasMany/BelongsToMany relations
return Promise.map(self.$options.include.filter(function (include) {
return !(include.association instanceof BelongsTo);
}), function (include) {
var instances = self.get(include.as);
if (!instances) return Promise.resolve();
if (!Array.isArray(instances)) instances = [instances];
if (!instances.length) return Promise.resolve();
var includeOptions = _(Utils.cloneDeep(include))
.omit(['association'])
.defaults({
transaction: options.transaction,
logging: options.logging,
parentRecord: self
}).value();
// Instances will be updated in place so we can safely treat HasOne like a HasMany
return Promise.map(instances, function (instance) {
if (include.association instanceof BelongsToMany) {
return instance.save(includeOptions).then(function () {
var values = {};
values[include.association.foreignKey] = self.get(self.constructor.primaryKeyAttribute, {raw: true});
values[include.association.otherKey] = instance.get(instance.constructor.primaryKeyAttribute, {raw: true});
return include.association.throughModel.create(values, includeOptions);
});
} else {
instance.set(include.association.foreignKey, self.get(self.constructor.primaryKeyAttribute, {raw: true}));
return instance.save(includeOptions);
}
});
});
})
.tap(function(result) {
// Run after hook
if (options.hooks) {
return self.constructor.runHooks('after' + hook, result, options);
}
})
.then(function(result) {
options.fields.forEach(function (field) {
result._previousDataValues[field] = result.dataValues[field];
self.changed(field, false);
});
self.isNewRecord = false;
return result;
});
});
});
}
/*
* Refresh the current instance in-place, i.e. update the object with current data from the DB and return the same object.
* This is different from doing a `find(Instance.id)`, because that would create and return a new instance. With this method,
* all references to the Instance are updated with the new data and no new objects are created.
*
* @see {Model#find}
* @param {Object} [options] Options that are passed on to `Model.find`
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @return {Promise<this>}
*/
reload(options) {
options = _.defaults({}, options, {
where: this.where(),
include: this.$options.include || null
});
return this.constructor.findOne(options).bind(this)
.tap(function (reload) {
if (!reload) {
throw new sequelizeErrors.InstanceError(
'Instance could not be reloaded because it does not exist anymore (find call returned null)'
);
}
})
.then(function(reload) {
// update the internal options of the instance
this.$options = reload.$options;
// re-set instance values
this.set(reload.dataValues, {
raw: true,
reset: true && !options.attributes
});
}).return(this);
}
/*
* Validate the attribute of this instance according to validation rules set in the model definition.
*
* Emits null if and only if validation successful; otherwise an Error instance containing { field name : [error msgs] } entries.
*
* @param {Object} [options] Options that are passed to the validator
* @param {Array} [options.skip] An array of strings. All properties that are in this array will not be validated
* @param {Boolean} [options.hooks=true] Run before and after validate hooks
* @see {InstanceValidator}
*
* @return {Promise<undefined|Errors.ValidationError>}
*/
validate(options) {
return new InstanceValidator(this, options).validate();
}
/**
* This is the same as calling `set` and then calling `save` but it only saves the
* exact values passed to it, making it more atomic and safer.
*
* @see {Instance#set}
* @see {Instance#save}
* @param {Object} updates See `set`
* @param {Object} options See `save`
*
* @return {Promise<this>}
* @alias updateAttributes
*/
update(values, options) {
var changedBefore = this.changed() || []
, sideEffects
, fields
, setOptions;
options = options || {};
if (Array.isArray(options)) options = {fields: options};
options = Utils.cloneDeep(options);
setOptions = Utils.cloneDeep(options);
setOptions.attributes = options.fields;
this.set(values, setOptions);
// Now we need to figure out which fields were actually affected by the setter.
sideEffects = _.without.apply(this, [this.changed() || []].concat(changedBefore));
fields = _.union(Object.keys(values), sideEffects);
if (!options.fields) {
options.fields = _.intersection(fields, this.changed());
options.defaultFields = options.fields;
}
return this.save(options);
}
/**
* 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.
*
* @param {Object} [options={}]
* @param {Boolean} [options.force=false] If set to true, paranoid models will actually be deleted
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
*
* @return {Promise<undefined>}
*/
destroy(options) {
options = Utils._.extend({
hooks: true,
force: false
}, options);
return Promise.bind(this).then(function() {
// Run before hook
if (options.hooks) {
return this.constructor.runHooks('beforeDestroy', this, options);
}
}).then(function() {
var where = this.where();
if (this.constructor._timestampAttributes.deletedAt && options.force === false) {
var attribute = this.constructor.rawAttributes[this.constructor._timestampAttributes.deletedAt]
, field = attribute.field || this.constructor._timestampAttributes.deletedAt
, values = {};
values[field] = new Date();
where[field] = attribute.hasOwnProperty('defaultValue') ? attribute.defaultValue : null;
this.setDataValue(field, values[field]);
return this.sequelize.getQueryInterface().update(this, this.constructor.getTableName(options), values, where, _.defaults({ hooks: false }, options));
} else {
return this.sequelize.getQueryInterface().delete(this, this.constructor.getTableName(options), where, _.assign({ type: QueryTypes.DELETE, limit: null }, options));
}
}).tap(function() {
// Run after hook
if (options.hooks) {
return this.constructor.runHooks('afterDestroy', this, options);
}
}).then(function(result) {
return result;
});
}
/**
* Restore the row corresponding to this instance. Only available for paranoid models.
*
* @param {Object} [options={}]
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction]
*
* @return {Promise<undefined>}
*/
restore(options) {
if (!this.constructor._timestampAttributes.deletedAt) throw new Error('Model is not paranoid');
options = Utils._.extend({
hooks: true,
force: false
}, options);
return Promise.bind(this).then(function() {
// Run before hook
if (options.hooks) {
return this.constructor.runHooks('beforeRestore', this, options);
}
}).then(function() {
var deletedAtCol = this.constructor._timestampAttributes.deletedAt
, deletedAtAttribute = this.constructor.rawAttributes[deletedAtCol]
, deletedAtDefaultValue = deletedAtAttribute.hasOwnProperty('defaultValue') ? deletedAtAttribute.defaultValue : null;
this.setDataValue(deletedAtCol, deletedAtDefaultValue);
return this.save(_.extend({}, options, {hooks : false, omitNull : false}));
}).tap(function() {
// Run after hook
if (options.hooks) {
return this.constructor.runHooks('afterRestore', this, options);
}
});
}
/**
* 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
* ```sql
* SET column = column + X
* ```
* query. To get the correct value after an increment into the Instance you should do a reload.
*
*```js
* instance.increment('number') // increment number by 1
* instance.increment(['number', 'count'], { by: 2 }) // increment number and count by 2
* instance.increment({ answer: 42, tries: 1}, { by: 2 }) // increment answer by 42, and tries by 1.
* // `by` is ignored, since each column has its own value
* ```
*
* @see {Instance#reload}
* @param {String|Array|Object} fields If a string is provided, that column is incremented by the value of `by` given in options. If an array is provided, the same is true for each column. If and object is provided, each column is incremented by the value given.
* @param {Object} [options]
* @param {Integer} [options.by=1] The number to increment by
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
*
* @return {Promise<this>}
*/
increment(fields, options) {
var identifier = this.where()
, updatedAtAttr = this.constructor._timestampAttributes.updatedAt
, values = {}
, where;
options = _.defaults({}, options, {
by: 1,
attributes: {},
where: {}
});
where = _.extend({}, options.where, identifier);
if (Utils._.isString(fields)) {
values[fields] = options.by;
} else if (Utils._.isArray(fields)) {
Utils._.each(fields, function(field) {
values[field] = options.by;
});
} else { // Assume fields is key-value pairs
values = fields;
}
if (updatedAtAttr && !values[updatedAtAttr]) {
options.attributes[updatedAtAttr] = this.constructor.$getDefaultTimestamp(updatedAtAttr) || Utils.now(this.sequelize.options.dialect);
}
Object.keys(values).forEach(function(attr) {
// Field name mapping
if (this.constructor.rawAttributes[attr] && this.constructor.rawAttributes[attr].field && this.constructor.rawAttributes[attr].field !== attr) {
values[this.constructor.rawAttributes[attr].field] = values[attr];
delete values[attr];
}
}, this);
return this.sequelize.getQueryInterface().increment(this, this.constructor.getTableName(options), values, where, options).return(this);
}
/**
* 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
* ```sql
* SET column = column - X
* ```
* query. To get the correct value after an decrement into the Instance you should do a reload.
*
* ```js
* instance.decrement('number') // decrement number by 1
* instance.decrement(['number', 'count'], { by: 2 }) // decrement number and count by 2
* instance.decrement({ answer: 42, tries: 1}, { by: 2 }) // decrement answer by 42, and tries by 1.
* // `by` is ignored, since each column has its own value
* ```
*
* @see {Instance#reload}
* @param {String|Array|Object} fields If a string is provided, that column is decremented by the value of `by` given in options. If an array is provided, the same is true for each column. If and object is provided, each column is decremented by the value given
* @param {Object} [options]
* @param {Integer} [options.by=1] The number to decrement by
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Transaction} [options.transaction]
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
*
* @return {Promise}
*/
decrement(fields, options) {
options = _.defaults({}, options, {
by: 1
});
if (!Utils._.isString(fields) && !Utils._.isArray(fields)) { // Assume fields is key-value pairs
Utils._.each(fields, function(value, field) {
fields[field] = -value;
});
}
options.by = 0 - options.by;
return this.increment(fields, options);
}
/**
* Check whether this and `other` Instance refer to the same row
*
* @param {Instance} other
* @return {Boolean}
*/
equals(other) {
var self = this;
if (!other || !other.constructor) {
return false;
}
if (!(other instanceof self.constructor)) {
return false;
}
return Utils._.every(this.constructor.primaryKeyAttributes, function(attribute) {
return self.get(attribute, {raw: true}) === other.get(attribute, {raw: true});
});
}
/**
* Check if this is equal to one of `others` by calling equals
*
* @param {Array} others
* @return {Boolean}
*/
equalsOneOf(others) {
var self = this;
return _.some(others, function(other) {
return self.equals(other);
});
}
setValidators(attribute, validators) {
this.validators[attribute] = validators;
}
/**
* 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.
*
* @see {Instance#get}
* @return {object}
*/
toJSON() {
return this.get({
plain: true
});
}
}
// Aliases
Model.prototype.updateAttributes = Model.prototype.update;
Model.$conformOptions = Model._conformOptions;
Model.$validateIncludedElements = Model._validateIncludedElements;
Model.$expandIncludeAll = Model._expandIncludeAll;
Model.findByPrimary = Model.findById;
Model.find = Model.findOne;
Model.findAndCountAll = Model.findAndCount;
Model.findOrInitialize = Model.findOrBuild;
Model.insertOrUpdate = Model.upsert;
Utils._.extend(Model.prototype, associationsMixin); Utils._.extend(Model, associationsMixin);
Hooks.applyTo(Model); Hooks.applyTo(Model);
module.exports = Model; module.exports = Model;
...@@ -492,8 +492,8 @@ QueryInterface.prototype.removeIndex = function(tableName, indexNameOrAttributes ...@@ -492,8 +492,8 @@ QueryInterface.prototype.removeIndex = function(tableName, indexNameOrAttributes
QueryInterface.prototype.insert = function(instance, tableName, values, options) { QueryInterface.prototype.insert = function(instance, tableName, values, options) {
options = Utils.cloneDeep(options); options = Utils.cloneDeep(options);
options.hasTrigger = instance && instance.Model.options.hasTrigger; options.hasTrigger = instance && instance.constructor.options.hasTrigger;
var sql = this.QueryGenerator.insertQuery(tableName, values, instance && instance.Model.rawAttributes, options); var sql = this.QueryGenerator.insertQuery(tableName, values, instance && instance.constructor.rawAttributes, options);
options.type = QueryTypes.INSERT; options.type = QueryTypes.INSERT;
options.instance = instance; options.instance = instance;
...@@ -575,18 +575,18 @@ QueryInterface.prototype.update = function(instance, tableName, values, identifi ...@@ -575,18 +575,18 @@ QueryInterface.prototype.update = function(instance, tableName, values, identifi
var self = this var self = this
, restrict = false , restrict = false
, sql = self.QueryGenerator.updateQuery(tableName, values, identifier, options, instance.Model.rawAttributes); , sql = self.QueryGenerator.updateQuery(tableName, values, identifier, options, instance.constructor.rawAttributes);
options.type = QueryTypes.UPDATE; options.type = QueryTypes.UPDATE;
// Check for a restrict field // Check for a restrict field
if (!!instance.Model && !!instance.Model.associations) { if (instance.constructor && instance.constructor.associations) {
var keys = Object.keys(instance.Model.associations) var keys = Object.keys(instance.constructor.associations)
, length = keys.length; , length = keys.length;
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
if (instance.Model.associations[keys[i]].options && instance.Model.associations[keys[i]].options.onUpdate && instance.Model.associations[keys[i]].options.onUpdate === 'restrict') { if (instance.constructor.associations[keys[i]].options && instance.constructor.associations[keys[i]].options.onUpdate && instance.constructor.associations[keys[i]].options.onUpdate === 'restrict') {
restrict = true; restrict = true;
} }
} }
...@@ -611,18 +611,18 @@ QueryInterface.prototype.bulkUpdate = function(tableName, values, identifier, op ...@@ -611,18 +611,18 @@ QueryInterface.prototype.bulkUpdate = function(tableName, values, identifier, op
QueryInterface.prototype.delete = function(instance, tableName, identifier, options) { QueryInterface.prototype.delete = function(instance, tableName, identifier, options) {
var self = this var self = this
, cascades = [] , cascades = []
, sql = self.QueryGenerator.deleteQuery(tableName, identifier, null, instance.Model); , sql = self.QueryGenerator.deleteQuery(tableName, identifier, null, instance.constructor);
options = _.clone(options) || {}; options = _.clone(options) || {};
// Check for a restrict field // Check for a restrict field
if (!!instance.Model && !!instance.Model.associations) { if (!!instance.constructor && !!instance.constructor.associations) {
var keys = Object.keys(instance.Model.associations) var keys = Object.keys(instance.constructor.associations)
, length = keys.length , length = keys.length
, association; , association;
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
association = instance.Model.associations[keys[i]]; association = instance.constructor.associations[keys[i]];
if (association.options && association.options.onDelete && if (association.options && association.options.onDelete &&
association.options.onDelete.toLowerCase() === 'cascade' && association.options.onDelete.toLowerCase() === 'cascade' &&
association.options.useHooks === true) { association.options.useHooks === true) {
......
...@@ -14,7 +14,6 @@ var url = require('url') ...@@ -14,7 +14,6 @@ var url = require('url')
, sequelizeErrors = require('./errors') , sequelizeErrors = require('./errors')
, Promise = require('./promise') , Promise = require('./promise')
, Hooks = require('./hooks') , Hooks = require('./hooks')
, Instance = require('./instance')
, Association = require('./associations/index') , Association = require('./associations/index')
, _ = require('lodash'); , _ = require('lodash');
...@@ -328,13 +327,6 @@ Sequelize.prototype.Transaction = Sequelize.Transaction = Transaction; ...@@ -328,13 +327,6 @@ Sequelize.prototype.Transaction = Sequelize.Transaction = Transaction;
Sequelize.prototype.Deferrable = Sequelize.Deferrable = Deferrable; Sequelize.prototype.Deferrable = Sequelize.Deferrable = Deferrable;
/** /**
* A reference to the sequelize instance class.
* @property Instance
* @see {Instance}
*/
Sequelize.prototype.Instance = Sequelize.Instance = Instance;
/**
* A reference to the sequelize association class. * A reference to the sequelize association class.
* @property Association * @property Association
* @see {Association} * @see {Association}
...@@ -623,8 +615,9 @@ Sequelize.prototype.define = function(modelName, attributes, options) { // testh ...@@ -623,8 +615,9 @@ Sequelize.prototype.define = function(modelName, attributes, options) { // testh
modelName = options.modelName; modelName = options.modelName;
delete options.modelName; delete options.modelName;
var model = new Model(modelName, attributes, options); var model = class extends Model {};
model = model.init(this.modelManager); Object.defineProperty(model, 'name', {value: modelName});
model.init(attributes, options, this.modelManager);
this.modelManager.addModel(model); this.modelManager.addModel(model);
this.runHooks('afterDefine', model); this.runHooks('afterDefine', model);
...@@ -743,7 +736,7 @@ Sequelize.prototype.query = function(sql, options) { ...@@ -743,7 +736,7 @@ Sequelize.prototype.query = function(sql, options) {
options = _.assign({}, this.options.query, options); options = _.assign({}, this.options.query, options);
if (options.instance && !options.model) { if (options.instance && !options.model) {
options.model = options.instance.Model; options.model = options.instance.constructor;
} }
// Map raw fields to model field names using the `fieldAttributeMap` // Map raw fields to model field names using the `fieldAttributeMap`
......
...@@ -632,7 +632,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() { ...@@ -632,7 +632,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
this.task = task; this.task = task;
return task.createUser({ username: 'foo' }); return task.createUser({ username: 'foo' });
}).then(function(createdUser) { }).then(function(createdUser) {
expect(createdUser.Model).to.equal(User); expect(createdUser).to.be.instanceof(User);
expect(createdUser.username).to.equal('foo'); expect(createdUser.username).to.equal('foo');
return this.task.getUsers(); return this.task.getUsers();
}).then(function(_users) { }).then(function(_users) {
...@@ -717,7 +717,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() { ...@@ -717,7 +717,7 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
this.task = task; this.task = task;
return task.createUser({ username: 'foo' }, {fields: ['username']}); return task.createUser({ username: 'foo' }, {fields: ['username']});
}).then(function(createdUser) { }).then(function(createdUser) {
expect(createdUser.Model).to.equal(User); expect(createdUser).to.be.instanceof(User);
expect(createdUser.username).to.equal('foo'); expect(createdUser.username).to.equal('foo');
return this.task.getUsers(); return this.task.getUsers();
}).then(function(_users) { }).then(function(_users) {
......
...@@ -586,7 +586,7 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -586,7 +586,7 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
// the `UPDATE` query generated by `save()` uses `id` in the // the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause // `WHERE` clause
var tableName = user.sequelize.getQueryInterface().QueryGenerator.addSchema(user.Model); var tableName = user.sequelize.getQueryInterface().QueryGenerator.addSchema(user.constructor);
return expect( return expect(
user.sequelize.getQueryInterface().update(user, tableName, {id: 999}, {id: user.id}) user.sequelize.getQueryInterface().update(user, tableName, {id: 999}, {id: user.id})
).to.eventually.be.rejectedWith(Sequelize.ForeignKeyConstraintError).then(function () { ).to.eventually.be.rejectedWith(Sequelize.ForeignKeyConstraintError).then(function () {
...@@ -620,7 +620,7 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -620,7 +620,7 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
// the `UPDATE` query generated by `save()` uses `id` in the // the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause // `WHERE` clause
var tableName = user.sequelize.getQueryInterface().QueryGenerator.addSchema(user.Model); var tableName = user.sequelize.getQueryInterface().QueryGenerator.addSchema(user.constructor);
return user.sequelize.getQueryInterface().update(user, tableName, {id: 999}, {id: user.id}) return user.sequelize.getQueryInterface().update(user, tableName, {id: 999}, {id: user.id})
.then(function() { .then(function() {
return Task.findAll().then(function(tasks) { return Task.findAll().then(function(tasks) {
......
...@@ -1052,7 +1052,7 @@ describe(Support.getTestDialectTeaser('HasMany'), function() { ...@@ -1052,7 +1052,7 @@ describe(Support.getTestDialectTeaser('HasMany'), function() {
// the `UPDATE` query generated by `save()` uses `id` in the // the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause // `WHERE` clause
var tableName = user.sequelize.getQueryInterface().QueryGenerator.addSchema(user.Model); var tableName = user.sequelize.getQueryInterface().QueryGenerator.addSchema(user.constructor);
return user.sequelize.getQueryInterface().update(user, tableName, {id: 999}, {id: user.id}); return user.sequelize.getQueryInterface().update(user, tableName, {id: 999}, {id: user.id});
}).then(function() { }).then(function() {
return Task.findAll(); return Task.findAll();
...@@ -1109,7 +1109,7 @@ describe(Support.getTestDialectTeaser('HasMany'), function() { ...@@ -1109,7 +1109,7 @@ describe(Support.getTestDialectTeaser('HasMany'), function() {
// the `UPDATE` query generated by `save()` uses `id` in the // the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause // `WHERE` clause
var tableName = user.sequelize.getQueryInterface().QueryGenerator.addSchema(user.Model); var tableName = user.sequelize.getQueryInterface().QueryGenerator.addSchema(user.constructor);
return user.sequelize.getQueryInterface().update(user, tableName, {id: 999}, {id: user.id}) return user.sequelize.getQueryInterface().update(user, tableName, {id: 999}, {id: user.id})
.catch (self.sequelize.ForeignKeyConstraintError, function() { .catch (self.sequelize.ForeignKeyConstraintError, function() {
// Should fail due to FK violation // Should fail due to FK violation
......
...@@ -493,7 +493,7 @@ describe(Support.getTestDialectTeaser('HasOne'), function() { ...@@ -493,7 +493,7 @@ describe(Support.getTestDialectTeaser('HasOne'), function() {
// the `UPDATE` query generated by `save()` uses `id` in the // the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause // `WHERE` clause
var tableName = user.sequelize.getQueryInterface().QueryGenerator.addSchema(user.Model); var tableName = user.sequelize.getQueryInterface().QueryGenerator.addSchema(user.constructor);
return user.sequelize.getQueryInterface().update(user, tableName, {id: 999}, {id: user.id}).then(function() { return user.sequelize.getQueryInterface().update(user, tableName, {id: 999}, {id: user.id}).then(function() {
return Task.findAll().then(function(tasks) { return Task.findAll().then(function(tasks) {
expect(tasks).to.have.length(1); expect(tasks).to.have.length(1);
...@@ -549,7 +549,7 @@ describe(Support.getTestDialectTeaser('HasOne'), function() { ...@@ -549,7 +549,7 @@ describe(Support.getTestDialectTeaser('HasOne'), function() {
// the `UPDATE` query generated by `save()` uses `id` in the // the `UPDATE` query generated by `save()` uses `id` in the
// `WHERE` clause // `WHERE` clause
var tableName = user.sequelize.getQueryInterface().QueryGenerator.addSchema(user.Model); var tableName = user.sequelize.getQueryInterface().QueryGenerator.addSchema(user.constructor);
return expect( return expect(
user.sequelize.getQueryInterface().update(user, tableName, {id: 999}, {id: user.id}) user.sequelize.getQueryInterface().update(user, tableName, {id: 999}, {id: user.id})
).to.eventually.be.rejectedWith(Sequelize.ForeignKeyConstraintError).then(function () { ).to.eventually.be.rejectedWith(Sequelize.ForeignKeyConstraintError).then(function () {
......
...@@ -199,9 +199,9 @@ describe(Support.getTestDialectTeaser('associations'), function() { ...@@ -199,9 +199,9 @@ describe(Support.getTestDialectTeaser('associations'), function() {
questionComment.getItem() questionComment.getItem()
); );
}).spread(function(post, image, question) { }).spread(function(post, image, question) {
expect(post.Model).to.equal(self.Post); expect(post).to.be.instanceof(self.Post);
expect(image.Model).to.equal(self.Image); expect(image).to.be.instanceof(self.Image);
expect(question.Model).to.equal(self.Question); expect(question).to.be.instanceof(self.Question);
}).then(function() { }).then(function() {
return Promise.join( return Promise.join(
self.Post.find({ self.Post.find({
......
...@@ -113,12 +113,12 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -113,12 +113,12 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
}, { }, {
hooks: { hooks: {
beforeValidate: function(user, options, fn) { beforeValidate: function(user, options, fn) {
expect(user).to.be.instanceof(User.Instance); expect(user).to.be.instanceof(User);
beforeHooked = true; beforeHooked = true;
fn(); fn();
}, },
afterValidate: function(user, options, fn) { afterValidate: function(user, options, fn) {
expect(user).to.be.instanceof(User.Instance); expect(user).to.be.instanceof(User);
afterHooked = true; afterHooked = true;
fn(); fn();
} }
...@@ -143,12 +143,12 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -143,12 +143,12 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
}, { }, {
hooks: { hooks: {
beforeCreate: function(user, options, fn) { beforeCreate: function(user, options, fn) {
expect(user).to.be.instanceof(User.Instance); expect(user).to.be.instanceof(User);
beforeHooked = true; beforeHooked = true;
fn(); fn();
}, },
afterCreate: function(user, options, fn) { afterCreate: function(user, options, fn) {
expect(user).to.be.instanceof(User.Instance); expect(user).to.be.instanceof(User);
afterHooked = true; afterHooked = true;
fn(); fn();
} }
...@@ -173,12 +173,12 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -173,12 +173,12 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
}, { }, {
hooks: { hooks: {
beforeDestroy: function(user, options, fn) { beforeDestroy: function(user, options, fn) {
expect(user).to.be.instanceof(User.Instance); expect(user).to.be.instanceof(User);
beforeHooked = true; beforeHooked = true;
fn(); fn();
}, },
afterDestroy: function(user, options, fn) { afterDestroy: function(user, options, fn) {
expect(user).to.be.instanceof(User.Instance); expect(user).to.be.instanceof(User);
afterHooked = true; afterHooked = true;
fn(); fn();
} }
...@@ -205,12 +205,12 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -205,12 +205,12 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
}, { }, {
hooks: { hooks: {
beforeDelete: function(user, options, fn) { beforeDelete: function(user, options, fn) {
expect(user).to.be.instanceof(User.Instance); expect(user).to.be.instanceof(User);
beforeHooked = true; beforeHooked = true;
fn(); fn();
}, },
afterDelete: function(user, options, fn) { afterDelete: function(user, options, fn) {
expect(user).to.be.instanceof(User.Instance); expect(user).to.be.instanceof(User);
afterHooked = true; afterHooked = true;
fn(); fn();
} }
...@@ -237,12 +237,12 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -237,12 +237,12 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
}, { }, {
hooks: { hooks: {
beforeUpdate: function(user, options, fn) { beforeUpdate: function(user, options, fn) {
expect(user).to.be.instanceof(User.Instance); expect(user).to.be.instanceof(User);
beforeHooked = true; beforeHooked = true;
fn(); fn();
}, },
afterUpdate: function(user, options, fn) { afterUpdate: function(user, options, fn) {
expect(user).to.be.instanceof(User.Instance); expect(user).to.be.instanceof(User);
afterHooked = true; afterHooked = true;
fn(); fn();
} }
......
...@@ -183,9 +183,9 @@ describe(Support.getTestDialectTeaser('DAO'), function() { ...@@ -183,9 +183,9 @@ describe(Support.getTestDialectTeaser('DAO'), function() {
expect(product.tags).to.be.ok; expect(product.tags).to.be.ok;
expect(product.tags.length).to.equal(2); expect(product.tags.length).to.equal(2);
expect(product.tags[0].Model).to.equal(Tag); expect(product.tags[0]).to.be.instanceof(Tag);
expect(product.user).to.be.ok; expect(product.user).to.be.ok;
expect(product.user.Model).to.equal(User); expect(product.user).to.be.instanceof(User);
}); });
it('should support basic includes (with raw: true)', function() { it('should support basic includes (with raw: true)', function() {
...@@ -227,9 +227,9 @@ describe(Support.getTestDialectTeaser('DAO'), function() { ...@@ -227,9 +227,9 @@ describe(Support.getTestDialectTeaser('DAO'), function() {
expect(product.tags).to.be.ok; expect(product.tags).to.be.ok;
expect(product.tags.length).to.equal(2); expect(product.tags.length).to.equal(2);
expect(product.tags[0].Model).to.equal(Tag); expect(product.tags[0]).to.be.instanceof(Tag);
expect(product.user).to.be.ok; expect(product.user).to.be.ok;
expect(product.user.Model).to.equal(User); expect(product.user).to.be.instanceof(User);
}); });
}); });
}); });
...@@ -350,8 +350,8 @@ describe(Support.getTestDialectTeaser('DAO'), function() { ...@@ -350,8 +350,8 @@ describe(Support.getTestDialectTeaser('DAO'), function() {
} }
}, {raw: true}); }, {raw: true});
expect(product.get('user', {plain: true}).$Model).not.to.be.ok; expect(product.get('user', {plain: true})).not.to.be.instanceof(User);
expect(product.get({plain: true}).user.$Model).not.to.be.ok; expect(product.get({plain: true}).user).not.to.be.instanceof(User);
}); });
}); });
......
...@@ -623,9 +623,9 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -623,9 +623,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(product.Tags).to.be.ok; expect(product.Tags).to.be.ok;
expect(product.Tags.length).to.equal(2); expect(product.Tags.length).to.equal(2);
expect(product.Tags[0].Model).to.equal(Tag); expect(product.Tags[0]).to.be.instanceof(Tag);
expect(product.User).to.be.ok; expect(product.User).to.be.ok;
expect(product.User.Model).to.equal(User); expect(product.User).to.be.instanceof(User);
}); });
it('should support includes with aliases', function() { it('should support includes with aliases', function() {
...@@ -674,10 +674,10 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -674,10 +674,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(product.categories).to.be.ok; expect(product.categories).to.be.ok;
expect(product.categories.length).to.equal(4); expect(product.categories.length).to.equal(4);
expect(product.categories[0].Model).to.equal(Tag); expect(product.categories[0]).to.be.instanceof(Tag);
expect(product.followers).to.be.ok; expect(product.followers).to.be.ok;
expect(product.followers.length).to.equal(2); expect(product.followers.length).to.equal(2);
expect(product.followers[0].Model).to.equal(User); expect(product.followers[0]).to.be.instanceof(User);
}); });
}); });
}); });
......
...@@ -899,21 +899,21 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -899,21 +899,21 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('should return a DAO when queryOptions are not set', function() { it('should return a DAO when queryOptions are not set', function() {
var self = this; var self = this;
return this.User.findOne({ where: { username: 'barfooz'}}).then(function(user) { return this.User.findOne({ where: { username: 'barfooz'}}).then(function(user) {
expect(user).to.be.instanceOf(self.User.Instance); expect(user).to.be.instanceOf(self.User);
}); });
}); });
it('should return a DAO when raw is false', function() { it('should return a DAO when raw is false', function() {
var self = this; var self = this;
return this.User.findOne({ where: { username: 'barfooz'}, raw: false }).then(function(user) { return this.User.findOne({ where: { username: 'barfooz'}, raw: false }).then(function(user) {
expect(user).to.be.instanceOf(self.User.Instance); expect(user).to.be.instanceOf(self.User);
}); });
}); });
it('should return raw data when raw is true', function() { it('should return raw data when raw is true', function() {
var self = this; var self = this;
return this.User.findOne({ where: { username: 'barfooz'}, raw: true }).then(function(user) { return this.User.findOne({ where: { username: 'barfooz'}, raw: true }).then(function(user) {
expect(user).to.not.be.instanceOf(self.User.Instance); expect(user).to.not.be.instanceOf(self.User);
expect(user).to.be.instanceOf(Object); expect(user).to.be.instanceOf(Object);
}); });
}); });
......
...@@ -737,7 +737,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -737,7 +737,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
var self = this; var self = this;
return this.User.findAll({ where: { username: 'barfooz'}}).then(function(users) { return this.User.findAll({ where: { username: 'barfooz'}}).then(function(users) {
users.forEach(function(user) { users.forEach(function(user) {
expect(user).to.be.instanceOf(self.User.Instance); expect(user).to.be.instanceOf(self.User);
}); });
}); });
}); });
...@@ -746,7 +746,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -746,7 +746,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
var self = this; var self = this;
return this.User.findAll({ where: { username: 'barfooz'}, raw: false }).then(function(users) { return this.User.findAll({ where: { username: 'barfooz'}, raw: false }).then(function(users) {
users.forEach(function(user) { users.forEach(function(user) {
expect(user).to.be.instanceOf(self.User.Instance); expect(user).to.be.instanceOf(self.User);
}); });
}); });
}); });
...@@ -755,7 +755,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -755,7 +755,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
var self = this; var self = this;
return this.User.findAll({ where: { username: 'barfooz'}, raw: true }).then(function(users) { return this.User.findAll({ where: { username: 'barfooz'}, raw: true }).then(function(users) {
users.forEach(function(user) { users.forEach(function(user) {
expect(user).to.not.be.instanceOf(self.User.Instance); expect(user).to.not.be.instanceOf(self.User);
expect(users[0]).to.be.instanceOf(Object); expect(users[0]).to.be.instanceOf(Object);
}); });
}); });
......
...@@ -393,7 +393,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() { ...@@ -393,7 +393,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
model: this.User model: this.User
}); });
}).then(function(users) { }).then(function(users) {
expect(users[0].Model).to.equal(this.User); expect(users[0]).to.be.instanceof(this.User);
}); });
}); });
......
...@@ -11,8 +11,8 @@ var QueryInterface = require(__dirname + '/../lib/query-interface') ...@@ -11,8 +11,8 @@ var QueryInterface = require(__dirname + '/../lib/query-interface')
module.exports = function(Sequelize) { module.exports = function(Sequelize) {
// Shim all Sequelize methods // Shim all Sequelize methods
shimAll(Sequelize.prototype); shimAll(Sequelize.prototype);
shimAll(Sequelize.Model);
shimAll(Sequelize.Model.prototype); shimAll(Sequelize.Model.prototype);
shimAll(Sequelize.Instance.prototype);
shimAll(QueryInterface.prototype); shimAll(QueryInterface.prototype);
// Shim Model.prototype to then shim getter/setter methods // Shim Model.prototype to then shim getter/setter methods
...@@ -23,7 +23,7 @@ module.exports = function(Sequelize) { ...@@ -23,7 +23,7 @@ module.exports = function(Sequelize) {
association = original.apply(this, arguments); association = original.apply(this, arguments);
_.forIn(association.accessors, function(accessor) { _.forIn(association.accessors, function(accessor) {
shim(model.Instance.prototype, accessor, model.Instance.prototype[accessor].length); shim(model.prototype, accessor, model.prototype[accessor].length);
}); });
return association; return association;
......
...@@ -429,10 +429,10 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() { ...@@ -429,10 +429,10 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() {
}); });
it('should work for belongsTo associations defined before belongsToMany', function () { it('should work for belongsTo associations defined before belongsToMany', function () {
expect(this.UserProjects.Instance.prototype.getUser).to.be.ok; expect(this.UserProjects.prototype.getUser).to.be.ok;
}); });
it('should work for belongsTo associations defined after belongsToMany', function () { it('should work for belongsTo associations defined after belongsToMany', function () {
expect(this.UserProjects.Instance.prototype.getProject).to.be.ok; expect(this.UserProjects.prototype.getProject).to.be.ok;
}); });
}); });
...@@ -484,22 +484,21 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() { ...@@ -484,22 +484,21 @@ describe(Support.getTestDialectTeaser('belongsToMany'), function() {
it('works with singular and plural name for self-associations', function () { it('works with singular and plural name for self-associations', function () {
// Models taken from https://github.com/sequelize/sequelize/issues/3796 // Models taken from https://github.com/sequelize/sequelize/issues/3796
var Service = current.define('service', {}) var Service = current.define('service', {});
, Instance = Service.Instance;
Service.belongsToMany(Service, {through: 'Supplements', as: 'supplements'}); Service.belongsToMany(Service, {through: 'Supplements', as: 'supplements'});
Service.belongsToMany(Service, {through: 'Supplements', as: {singular: 'supplemented', plural: 'supplemented'}}); Service.belongsToMany(Service, {through: 'Supplements', as: {singular: 'supplemented', plural: 'supplemented'}});
expect(Instance.prototype).to.have.property('getSupplements').which.is.a.function; expect(Service.prototype).to.have.property('getSupplements').which.is.a.function;
expect(Instance.prototype).to.have.property('addSupplement').which.is.a.function; expect(Service.prototype).to.have.property('addSupplement').which.is.a.function;
expect(Instance.prototype).to.have.property('addSupplements').which.is.a.function; expect(Service.prototype).to.have.property('addSupplements').which.is.a.function;
expect(Instance.prototype).to.have.property('getSupplemented').which.is.a.function; expect(Service.prototype).to.have.property('getSupplemented').which.is.a.function;
expect(Instance.prototype).not.to.have.property('getSupplementeds').which.is.a.function; expect(Service.prototype).not.to.have.property('getSupplementeds').which.is.a.function;
expect(Instance.prototype).to.have.property('addSupplemented').which.is.a.function; expect(Service.prototype).to.have.property('addSupplemented').which.is.a.function;
expect(Instance.prototype).not.to.have.property('addSupplementeds').which.is.a.function; expect(Service.prototype).not.to.have.property('addSupplementeds').which.is.a.function;
}); });
}); });
......
...@@ -12,14 +12,14 @@ var chai = require('chai') ...@@ -12,14 +12,14 @@ var chai = require('chai')
describe(Support.getTestDialectTeaser('Model'), function() { describe(Support.getTestDialectTeaser('Model'), function() {
describe('method findOne', function () { describe('method findOne', function () {
before(function () { before(function () {
this.oldFindAll = current.Model.prototype.findAll; this.oldFindAll = current.Model.findAll;
}); });
after(function () { after(function () {
current.Model.prototype.findAll = this.oldFindAll; current.Model.findAll = this.oldFindAll;
}); });
beforeEach(function () { beforeEach(function () {
this.stub = current.Model.prototype.findAll = sinon.stub().returns(Promise.resolve()); this.stub = current.Model.findAll = sinon.stub().returns(Promise.resolve());
}); });
describe('should not add limit when querying on a primary key', function () { describe('should not add limit when querying on a primary key', function () {
......
...@@ -298,7 +298,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -298,7 +298,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
limit: 9 limit: 9
}; };
current.Model.prototype.$injectScope.call({ current.Model.$injectScope.call({
$scope: scope $scope: scope
}, options); }, options);
...@@ -322,7 +322,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -322,7 +322,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
var options = {}; var options = {};
current.Model.prototype.$injectScope.call({ current.Model.$injectScope.call({
$scope: scope $scope: scope
}, options); }, options);
...@@ -339,7 +339,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -339,7 +339,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
include: [{ model: Project, where: { something: true }}] include: [{ model: Project, where: { something: true }}]
}; };
current.Model.prototype.$injectScope.call({ current.Model.$injectScope.call({
$scope: scope $scope: scope
}, options); }, options);
...@@ -356,7 +356,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -356,7 +356,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
include: [{model: User, as: 'otherUser'}] include: [{model: User, as: 'otherUser'}]
}; };
current.Model.prototype.$injectScope.call({ current.Model.$injectScope.call({
$scope: scope $scope: scope
}, options); }, options);
...@@ -378,7 +378,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -378,7 +378,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
] ]
}; };
current.Model.prototype.$injectScope.call({ current.Model.$injectScope.call({
$scope: scope $scope: scope
}, options); }, options);
...@@ -401,7 +401,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -401,7 +401,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
] ]
}; };
current.Model.prototype.$injectScope.call({ current.Model.$injectScope.call({
$scope: scope $scope: scope
}, options); }, options);
...@@ -424,7 +424,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -424,7 +424,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
] ]
}; };
current.Model.prototype.$injectScope.call({ current.Model.$injectScope.call({
$scope: scope $scope: scope
}, options); }, options);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!