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

Commit 9e6b2adb by Mick Hansen

Use .Model

1 parent f99845b6
...@@ -83,7 +83,7 @@ module.exports = (function() { ...@@ -83,7 +83,7 @@ module.exports = (function() {
var self = this var self = this
obj[this.accessors.set] = function(associatedObject, options) { obj[this.accessors.set] = function(associatedObject, options) {
var primaryKeys = !!associatedObject && !!associatedObject.daoFactory ? Object.keys(associatedObject.daoFactory.primaryKeys) : [] var primaryKeys = !!associatedObject && !!associatedObject.Model ? Object.keys(associatedObject.Model.primaryKeys) : []
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id' , primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
this[self.identifier] = associatedObject ? associatedObject[primaryKey] : null this[self.identifier] = associatedObject ? associatedObject[primaryKey] : null
......
...@@ -23,7 +23,7 @@ module.exports = (function() { ...@@ -23,7 +23,7 @@ module.exports = (function() {
, targetAssociation = self.association.targetAssociation , targetAssociation = self.association.targetAssociation
//fully qualify //fully qualify
var instancePrimaryKeys = Object.keys(self.instance.daoFactory.primaryKeys) var instancePrimaryKeys = Object.keys(self.instance.Model.primaryKeys)
, instancePrimaryKey = instancePrimaryKeys.length > 0 ? instancePrimaryKeys[0] : 'id' , instancePrimaryKey = instancePrimaryKeys.length > 0 ? instancePrimaryKeys[0] : 'id'
where[through.tableName+"."+self.association.identifier] = self.instance[instancePrimaryKey] where[through.tableName+"."+self.association.identifier] = self.instance[instancePrimaryKey]
......
...@@ -32,7 +32,7 @@ module.exports = (function() { ...@@ -32,7 +32,7 @@ module.exports = (function() {
HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations, defaultAttributes) { HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations, defaultAttributes) {
var self = this var self = this
, associationKeys = Object.keys((oldAssociations[0] || newAssociations[0] || {daoFactory: {primaryKeys: {}}}).daoFactory.primaryKeys || {}) , associationKeys = Object.keys((oldAssociations[0] || newAssociations[0] || {Model: {primaryKeys: {}}}).Model.primaryKeys || {})
, associationKey = (associationKeys.length === 1) ? associationKeys[0] : 'id' , associationKey = (associationKeys.length === 1) ? associationKeys[0] : 'id'
, chainer = new Utils.QueryChainer() , chainer = new Utils.QueryChainer()
, options = {} , options = {}
...@@ -77,7 +77,7 @@ module.exports = (function() { ...@@ -77,7 +77,7 @@ module.exports = (function() {
if (unassociatedObjects.length > 0) { if (unassociatedObjects.length > 0) {
// For the self.instance // For the self.instance
var pkeys = Object.keys(self.instance.daoFactory.primaryKeys) var pkeys = Object.keys(self.instance.Model.primaryKeys)
, pkey = pkeys.length === 1 ? pkeys[0] : 'id' , pkey = pkeys.length === 1 ? pkeys[0] : 'id'
// For chainer // For chainer
, primaryKeys = Object.keys(this.__factory.target.primaryKeys) , primaryKeys = Object.keys(this.__factory.target.primaryKeys)
...@@ -109,7 +109,7 @@ module.exports = (function() { ...@@ -109,7 +109,7 @@ module.exports = (function() {
} }
HasManySingleLinked.prototype.injectAdder = function(emitterProxy, newAssociation, additionalAttributes) { HasManySingleLinked.prototype.injectAdder = function(emitterProxy, newAssociation, additionalAttributes) {
var primaryKeys = Object.keys(this.instance.daoFactory.primaryKeys) var primaryKeys = Object.keys(this.instance.Model.primaryKeys)
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id' , primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
, options = {} , options = {}
......
...@@ -268,12 +268,12 @@ module.exports = (function() { ...@@ -268,12 +268,12 @@ module.exports = (function() {
obj[this.accessors.add] = function(newAssociatedObject, additionalAttributes) { obj[this.accessors.add] = function(newAssociatedObject, additionalAttributes) {
var instance = this var instance = this
, primaryKeys = Object.keys(newAssociatedObject.daoFactory.primaryKeys || {}) , primaryKeys = Object.keys(newAssociatedObject.Model.primaryKeys || {})
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id' , primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
, where = {} , where = {}
where[newAssociatedObject.daoFactory.tableName+'.'+primaryKey] = newAssociatedObject[primaryKey] where[newAssociatedObject.Model.tableName+'.'+primaryKey] = newAssociatedObject[primaryKey]
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
instance[self.accessors.get]({ where: where }) instance[self.accessors.get]({ where: where })
.proxy(emitter, {events: ['error', 'sql']}) .proxy(emitter, {events: ['error', 'sql']})
......
...@@ -54,7 +54,7 @@ module.exports = (function() { ...@@ -54,7 +54,7 @@ module.exports = (function() {
, smart , smart
obj[this.accessors.get] = function(params) { obj[this.accessors.get] = function(params) {
var primaryKeys = Object.keys(this.daoFactory.primaryKeys) var primaryKeys = Object.keys(this.Model.primaryKeys)
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id' , primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
, where = {} , where = {}
, id = this[primaryKey] || this.id , id = this[primaryKey] || this.id
...@@ -91,7 +91,7 @@ module.exports = (function() { ...@@ -91,7 +91,7 @@ module.exports = (function() {
obj[this.accessors.set] = function(associatedObject, options) { obj[this.accessors.set] = function(associatedObject, options) {
var instance = this var instance = this
, instanceKeys = Object.keys(instance.daoFactory.primaryKeys) , instanceKeys = Object.keys(instance.Model.primaryKeys)
, instanceKey = instanceKeys.length === 1 ? instanceKeys[0] : 'id' , instanceKey = instanceKeys.length === 1 ? instanceKeys[0] : 'id'
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
......
...@@ -205,9 +205,10 @@ module.exports = (function() { ...@@ -205,9 +205,10 @@ module.exports = (function() {
this._booleanAttributes = [] this._booleanAttributes = []
this._dateAttributes = [] this._dateAttributes = []
this._defaultValues = {}
this.DAO.prototype.booleanValues = this._booleanAttributes this.DAO.prototype.booleanValues = this._booleanAttributes
this.DAO.prototype.dateAttributes = this._dateAttributes this.DAO.prototype.dateAttributes = this._dateAttributes
this.DAO.prototype.defaultValues = {} this.DAO.prototype.defaultValues = this._defaultValues
this.DAO.prototype.validators = {} this.DAO.prototype.validators = {}
Utils._.each(this.rawAttributes, function (definition, name) { Utils._.each(this.rawAttributes, function (definition, name) {
...@@ -218,7 +219,7 @@ module.exports = (function() { ...@@ -218,7 +219,7 @@ module.exports = (function() {
self._dateAttributes.push(name); self._dateAttributes.push(name);
} }
if (definition.hasOwnProperty('defaultValue')) { if (definition.hasOwnProperty('defaultValue')) {
self.DAO.prototype.defaultValues[name] = Utils._.partial( self._defaultValues[name] = Utils._.partial(
Utils.toDefaultValue, definition.defaultValue) Utils.toDefaultValue, definition.defaultValue)
} }
...@@ -241,11 +242,10 @@ module.exports = (function() { ...@@ -241,11 +242,10 @@ module.exports = (function() {
this.DAO.prototype._hasDateAttributes = this._hasDateAttributes this.DAO.prototype._hasDateAttributes = this._hasDateAttributes
this.DAO.prototype._isDateAttribute = this._isDateAttribute this.DAO.prototype._isDateAttribute = this._isDateAttribute
this.DAO.prototype.__factory = this this.DAO.prototype.Model = this
this.DAO.prototype.daoFactory = this
this.DAO.prototype.Model = this this._hasDefaultValues = !Utils._.isEmpty(this._defaultValues)
this.DAO.prototype.hasDefaultValues = !Utils._.isEmpty(this.DAO.prototype.defaultValues) this.DAO.prototype.hasDefaultValues = this._hasDefaultValues
this.DAO.prototype.daoFactoryName = this.name
return this return this
} }
......
...@@ -2,11 +2,11 @@ var Validator = require("validator") ...@@ -2,11 +2,11 @@ var Validator = require("validator")
, Utils = require("./utils") , Utils = require("./utils")
, sequelizeError = require("./errors") , sequelizeError = require("./errors")
var DaoValidator = module.exports = function(model, options) { var DaoValidator = module.exports = function(modelInstance, options) {
options = options || {} options = options || {}
options.skip = options.skip || [] options.skip = options.skip || []
this.model = model this.modelInstance = modelInstance
this.chainer = new Utils.QueryChainer() this.chainer = new Utils.QueryChainer()
this.options = options this.options = options
} }
...@@ -44,10 +44,9 @@ DaoValidator.prototype.validate = function() { ...@@ -44,10 +44,9 @@ DaoValidator.prototype.validate = function() {
DaoValidator.prototype.hookValidate = function() { DaoValidator.prototype.hookValidate = function() {
var self = this var self = this
, errors = {}
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
self.model.daoFactory.runHooks('beforeValidate', self.model, function(err) { self.modelInstance.Model.runHooks('beforeValidate', self.modelInstance, function(err) {
if (!!err) { if (!!err) {
return emitter.emit('error', err) return emitter.emit('error', err)
} }
...@@ -57,12 +56,12 @@ DaoValidator.prototype.hookValidate = function() { ...@@ -57,12 +56,12 @@ DaoValidator.prototype.hookValidate = function() {
return emitter.emit('error', error) return emitter.emit('error', error)
} }
self.model.daoFactory.runHooks('afterValidate', self.model, function(err) { self.modelInstance.Model.runHooks('afterValidate', self.modelInstance, function(err) {
if (!!err) { if (!!err) {
return emitter.emit('error', err) return emitter.emit('error', err)
} }
emitter.emit('success', self.model) emitter.emit('success', self.modelInstance)
}) })
}) })
}) })
...@@ -71,7 +70,7 @@ DaoValidator.prototype.hookValidate = function() { ...@@ -71,7 +70,7 @@ DaoValidator.prototype.hookValidate = function() {
// private // private
var validateModel = function() { var validateModel = function() {
Utils._.each(this.model.__options.validate, function(_validator, validatorType) { Utils._.each(this.modelInstance.__options.validate, function(_validator, validatorType) {
var validator = prepareValidationOfAttribute.call(this, undefined, _validator, validatorType, { omitValue: true }) var validator = prepareValidationOfAttribute.call(this, undefined, _validator, validatorType, { omitValue: true })
this.chainer.add(new Utils.CustomEventEmitter(function(emitter) { this.chainer.add(new Utils.CustomEventEmitter(function(emitter) {
...@@ -99,12 +98,12 @@ var validateAttributes = function() { ...@@ -99,12 +98,12 @@ var validateAttributes = function() {
var self = this var self = this
, errors = {} , errors = {}
Utils._.each(this.model.rawAttributes, function(rawAttribute, field) { Utils._.each(this.modelInstance.rawAttributes, function(rawAttribute, field) {
var value = self.model.dataValues[field] var value = self.modelInstance.dataValues[field]
, hasAllowedNull = ((rawAttribute === undefined || rawAttribute.allowNull === true) && ((value === null) || (value === undefined))) , hasAllowedNull = ((rawAttribute === undefined || rawAttribute.allowNull === true) && ((value === null) || (value === undefined)))
, isSkipped = self.options.skip.length > 0 && self.options.skip.indexOf(field) !== -1 , isSkipped = self.options.skip.length > 0 && self.options.skip.indexOf(field) !== -1
if (self.model.validators.hasOwnProperty(field) && !hasAllowedNull && !isSkipped) { if (self.modelInstance.validators.hasOwnProperty(field) && !hasAllowedNull && !isSkipped) {
errors = Utils._.merge(errors, validateAttribute.call(self, value, field)) errors = Utils._.merge(errors, validateAttribute.call(self, value, field))
} }
}) })
...@@ -114,7 +113,7 @@ var validateAttributes = function() { ...@@ -114,7 +113,7 @@ var validateAttributes = function() {
var validateAttribute = function(value, field) { var validateAttribute = function(value, field) {
// for each validator // for each validator
Utils._.each(this.model.validators[field], function(details, validatorType) { Utils._.each(this.modelInstance.validators[field], function(details, validatorType) {
var validator = prepareValidationOfAttribute.call(this, value, details, validatorType) var validator = prepareValidationOfAttribute.call(this, value, details, validatorType)
this.chainer.add(new Utils.CustomEventEmitter(function(emitter) { this.chainer.add(new Utils.CustomEventEmitter(function(emitter) {
...@@ -162,7 +161,7 @@ var prepareValidationOfAttribute = function(value, details, validatorType, optio ...@@ -162,7 +161,7 @@ var prepareValidationOfAttribute = function(value, details, validatorType, optio
} }
try { try {
details.apply(this.model, callArgs) details.apply(this.modelInstance, callArgs)
} catch(ex) { } catch(ex) {
return next(ex) return next(ex)
} }
......
...@@ -560,7 +560,7 @@ module.exports = (function() { ...@@ -560,7 +560,7 @@ module.exports = (function() {
} }
if (updatedAtAttr && !values[updatedAtAttr]) { if (updatedAtAttr && !values[updatedAtAttr]) {
countOrOptions.attributes[updatedAtAttr] = Utils.now(this.daoFactory.daoFactoryManager.sequelize.options.dialect) countOrOptions.attributes[updatedAtAttr] = Utils.now(this.Model.daoFactoryManager.sequelize.options.dialect)
} }
return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.Model.tableName, this.Model.options.schema), values, identifier, countOrOptions) return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.Model.tableName, this.Model.options.schema), values, identifier, countOrOptions)
......
...@@ -173,7 +173,7 @@ module.exports = (function() { ...@@ -173,7 +173,7 @@ module.exports = (function() {
var handleInsertQuery = function(results, metaData) { var handleInsertQuery = function(results, metaData) {
if (this.callee) { if (this.callee) {
// add the inserted row id to the instance // add the inserted row id to the instance
var autoIncrementField = this.callee.__factory.autoIncrementField var autoIncrementField = this.callee.Model.autoIncrementField
, id = null , id = null
id = id || (results && results[this.getInsertIdField()]) id = id || (results && results[this.getInsertIdField()])
......
...@@ -137,7 +137,7 @@ module.exports = (function() { ...@@ -137,7 +137,7 @@ module.exports = (function() {
for (var key in rows[0]) { for (var key in rows[0]) {
if (rows[0].hasOwnProperty(key)) { if (rows[0].hasOwnProperty(key)) {
var record = rows[0][key] var record = rows[0][key]
if (!!this.callee.daoFactory && !!this.callee.daoFactory.rawAttributes && !!this.callee.daoFactory.rawAttributes[key] && !!this.callee.daoFactory.rawAttributes[key].type && !!this.callee.daoFactory.rawAttributes[key].type.type && this.callee.daoFactory.rawAttributes[key].type.type === DataTypes.HSTORE.type) { if (!!this.callee.Model && !!this.callee.Model.rawAttributes && !!this.callee.Model.rawAttributes[key] && !!this.callee.Model.rawAttributes[key].type && !!this.callee.Model.rawAttributes[key].type.type && this.callee.Model.rawAttributes[key].type.type === DataTypes.HSTORE.type) {
record = hstore.parse(record) record = hstore.parse(record)
} }
this.callee.dataValues[key] = record this.callee.dataValues[key] = record
...@@ -152,7 +152,7 @@ module.exports = (function() { ...@@ -152,7 +152,7 @@ module.exports = (function() {
for (var key in rows[0]) { for (var key in rows[0]) {
if (rows[0].hasOwnProperty(key)) { if (rows[0].hasOwnProperty(key)) {
var record = rows[0][key] var record = rows[0][key]
if (!!this.callee.daoFactory && !!this.callee.daoFactory.rawAttributes && !!this.callee.daoFactory.rawAttributes[key] && !!this.callee.daoFactory.rawAttributes[key].type && !!this.callee.daoFactory.rawAttributes[key].type.type && this.callee.daoFactory.rawAttributes[key].type.type === DataTypes.HSTORE.type) { if (!!this.callee.Model && !!this.callee.Model.rawAttributes && !!this.callee.Model.rawAttributes[key] && !!this.callee.Model.rawAttributes[key].type && !!this.callee.Model.rawAttributes[key].type.type && this.callee.Model.rawAttributes[key].type.type === DataTypes.HSTORE.type) {
record = hstore.parse(record) record = hstore.parse(record)
} }
this.callee.dataValues[key] = record this.callee.dataValues[key] = record
......
...@@ -504,7 +504,7 @@ module.exports = (function() { ...@@ -504,7 +504,7 @@ module.exports = (function() {
} }
QueryInterface.prototype.insert = function(dao, tableName, values, options) { QueryInterface.prototype.insert = function(dao, tableName, values, options) {
var sql = this.QueryGenerator.insertQuery(tableName, values, dao.daoFactory.rawAttributes) var sql = this.QueryGenerator.insertQuery(tableName, values, dao.Model.rawAttributes)
return queryAndEmit.call(this, [sql, dao, options], 'insert', { return queryAndEmit.call(this, [sql, dao, options], 'insert', {
success: function(obj) { obj.isNewRecord = false } success: function(obj) { obj.isNewRecord = false }
}) })
...@@ -518,15 +518,15 @@ module.exports = (function() { ...@@ -518,15 +518,15 @@ module.exports = (function() {
QueryInterface.prototype.update = function(dao, tableName, values, identifier, options) { QueryInterface.prototype.update = function(dao, tableName, values, identifier, options) {
var self = this var self = this
, restrict = false , restrict = false
, sql = self.QueryGenerator.updateQuery(tableName, values, identifier, options, dao.daoFactory.rawAttributes) , sql = self.QueryGenerator.updateQuery(tableName, values, identifier, options, dao.Model.rawAttributes)
// Check for a restrict field // Check for a restrict field
if (!!dao.daoFactory && !!dao.daoFactory.associations) { if (!!dao.Model && !!dao.Model.associations) {
var keys = Object.keys(dao.daoFactory.associations) var keys = Object.keys(dao.Model.associations)
, length = keys.length , length = keys.length
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
if (dao.daoFactory.associations[keys[i]].options && dao.daoFactory.associations[keys[i]].options.onUpdate && dao.daoFactory.associations[keys[i]].options.onUpdate === "restrict") { if (dao.Model.associations[keys[i]].options && dao.Model.associations[keys[i]].options.onUpdate && dao.Model.associations[keys[i]].options.onUpdate === "restrict") {
restrict = true restrict = true
} }
} }
...@@ -581,20 +581,20 @@ module.exports = (function() { ...@@ -581,20 +581,20 @@ module.exports = (function() {
var self = this var self = this
, restrict = false , restrict = false
, cascades = [] , cascades = []
, sql = self.QueryGenerator.deleteQuery(tableName, identifier, null, dao.daoFactory) , sql = self.QueryGenerator.deleteQuery(tableName, identifier, null, dao.Model)
// Check for a restrict field // Check for a restrict field
if (!!dao.daoFactory && !!dao.daoFactory.associations) { if (!!dao.Model && !!dao.Model.associations) {
var keys = Object.keys(dao.daoFactory.associations) var keys = Object.keys(dao.Model.associations)
, length = keys.length , length = keys.length
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
if (dao.daoFactory.associations[keys[i]].options && dao.daoFactory.associations[keys[i]].options.onDelete) { if (dao.Model.associations[keys[i]].options && dao.Model.associations[keys[i]].options.onDelete) {
if (dao.daoFactory.associations[keys[i]].options.onDelete === "restrict") { if (dao.Model.associations[keys[i]].options.onDelete === "restrict") {
restrict = true restrict = true
} }
else if (dao.daoFactory.associations[keys[i]].options.onDelete === "cascade" && dao.daoFactory.associations[keys[i]].options.useHooks === true) { else if (dao.Model.associations[keys[i]].options.onDelete === "cascade" && dao.Model.associations[keys[i]].options.useHooks === true) {
cascades[cascades.length] = dao.daoFactory.associations[keys[i]].accessors.get cascades[cascades.length] = dao.Model.associations[keys[i]].accessors.get
} }
} }
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!