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

Commit ffeddc46 by Sascha Depold

use private method scoping

1 parent 59605e88
Showing with 176 additions and 172 deletions
...@@ -2,216 +2,220 @@ var Utils = require("./utils") ...@@ -2,216 +2,220 @@ var Utils = require("./utils")
, Model = require("./model") , Model = require("./model")
, DataTypes = require("./data-types") , DataTypes = require("./data-types")
var ModelFactory = module.exports = function(name, attributes, options) { module.exports = (function() {
var self = this var ModelFactory = function(name, attributes, options) {
var self = this
this.options = options || {}
this.options.timestamps = this.options.hasOwnProperty('timestamps') ? this.options.timestamps : true this.options = options || {}
this.name = name this.options.timestamps = this.options.hasOwnProperty('timestamps') ? this.options.timestamps : true
this.tableName = this.options.freezeTableName ? name : Utils.pluralize(name) this.name = name
this.attributes = Utils.simplifyAttributes(attributes) this.tableName = this.options.freezeTableName ? name : Utils.pluralize(name)
this.rawAttributes = attributes this.attributes = Utils.simplifyAttributes(attributes)
this.modelManager = null // defined by model-manager during addModel this.rawAttributes = attributes
this.associations = {} this.modelManager = null // defined by model-manager during addModel
this.associations = {}
this.__addDefaultAttributes()
this.__addOptionalClassMethods() addDefaultAttributes.call(this)
this.__findAutoIncrementField() addOptionalClassMethods.call(this)
} findAutoIncrementField.call(this)
}
ModelFactory.prototype.__defineGetter__('QueryGenerator', function() {
return this.modelManager.sequelize.connectorManager.getQueryGenerator()
})
ModelFactory.prototype.sync = function(options) {
options = Utils.merge(options || {}, this.options)
var self = this
var eventEmitter = new Utils.CustomEventEmitter(function() {
var doQuery = function() {
self.__query(self.QueryGenerator.createTableQuery(self.tableName, self.attributes, options))
.on('success', function() { eventEmitter.emit('success', self) })
.on('failure', function(err) { eventEmitter.emit('failure', err) })
}
if(options.force) { ModelFactory.prototype.__defineGetter__('QueryGenerator', function() {
self.drop() return this.modelManager.sequelize.connectorManager.getQueryGenerator()
.on('success', function() { doQuery() })
.on('failure', function(err) { eventEmitter.emit('failure', err) })
} else {
doQuery()
}
}) })
return eventEmitter.run() ModelFactory.prototype.sync = function(options) {
} options = Utils.merge(options || {}, this.options)
ModelFactory.prototype.drop = function() { var self = this
return this.__query(this.QueryGenerator.dropTableQuery(this.tableName, this.id)) var eventEmitter = new Utils.CustomEventEmitter(function() {
} var doQuery = function() {
query.call(self, self.QueryGenerator.createTableQuery(self.tableName, self.attributes, options))
.on('success', function() { eventEmitter.emit('success', self) })
.on('failure', function(err) { eventEmitter.emit('failure', err) })
}
if(options.force) {
self.drop()
.on('success', function() { doQuery() })
.on('failure', function(err) { eventEmitter.emit('failure', err) })
} else {
doQuery()
}
})
ModelFactory.prototype.all = function() { return eventEmitter.run()
return this.__query(this.QueryGenerator.selectQuery(this.tableName)) }
}
ModelFactory.prototype.count = function(options) { ModelFactory.prototype.drop = function() {
var self = this return query.call(this, this.QueryGenerator.dropTableQuery(this.tableName, this.id))
}
var emitter = new Utils.CustomEventEmitter(function() { ModelFactory.prototype.all = function() {
self.__query(self.QueryGenerator.countQuery(self.tableName, options), self, {plain: true}).on('success', function(obj) { return query.call(this, this.QueryGenerator.selectQuery(this.tableName))
emitter.emit('success', obj['count(*)']) }
})
})
return emitter.run()
}
ModelFactory.prototype.max = function(field, options) { ModelFactory.prototype.count = function(options) {
var self = this var self = this
var emitter = new Utils.CustomEventEmitter(function() { var emitter = new Utils.CustomEventEmitter(function() {
self.__query(self.QueryGenerator.maxQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) { query.call(self, self.QueryGenerator.countQuery(self.tableName, options), self, {plain: true}).on('success', function(obj) {
emitter.emit('success', obj['max']) emitter.emit('success', obj['count(*)'])
}) })
})
return emitter.run()
}
ModelFactory.prototype.min = function(field, options) {
var self = this
var emitter = new Utils.CustomEventEmitter(function() {
self.__query(self.QueryGenerator.minQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) {
emitter.emit('success', obj['min'])
}) })
}) return emitter.run()
return emitter.run() }
}
ModelFactory.prototype.findAll = function(options) { ModelFactory.prototype.max = function(field, options) {
return this.__query(this.QueryGenerator.selectQuery(this.tableName, options)) var self = this
}
var emitter = new Utils.CustomEventEmitter(function() {
ModelFactory.prototype.find = function(options) { query.call(self, self.QueryGenerator.maxQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) {
// options is not a hash but an id emitter.emit('success', obj['max'])
if(typeof options == 'number')
options = {where: options}
else if (Utils.argsArePrimaryKeys(arguments, this.primaryKeys)) {
var where = {}
, self = this
Utils._.each(arguments, function(arg, i) {
var key = Utils._.keys(self.primaryKeys)[i]
where[key] = arg
}) })
})
return emitter.run()
}
ModelFactory.prototype.min = function(field, options) {
var self = this
options = {where: where} var emitter = new Utils.CustomEventEmitter(function() {
} else if((options == null) || (options == undefined)) { query.call(self, self.QueryGenerator.minQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) {
var NullEmitter = require("./null-emitter") emitter.emit('success', obj['min'])
return new NullEmitter() })
})
return emitter.run()
}
ModelFactory.prototype.findAll = function(options) {
return query.call(this, this.QueryGenerator.selectQuery(this.tableName, options))
} }
options.limit = 1 ModelFactory.prototype.find = function(options) {
// options is not a hash but an id
if(typeof options == 'number')
options = {where: options}
else if (Utils.argsArePrimaryKeys(arguments, this.primaryKeys)) {
var where = {}
, self = this
Utils._.each(arguments, function(arg, i) {
var key = Utils._.keys(self.primaryKeys)[i]
where[key] = arg
})
options = {where: where}
} else if((options == null) || (options == undefined)) {
var NullEmitter = require("./null-emitter")
return new NullEmitter()
}
var query = this.QueryGenerator.selectQuery(this.tableName, options) options.limit = 1
return this.__query(query, this, {plain: true})
}
ModelFactory.prototype.build = function(values, options) { var sql = this.QueryGenerator.selectQuery(this.tableName, options)
var instance = new Model(values, Utils._.extend(this.options, this.attributes, { hasPrimaryKeys: this.hasPrimaryKeys })) return query.call(this, sql, this, {plain: true})
, self = this }
options = options || {} ModelFactory.prototype.build = function(values, options) {
instance.__definition = this var instance = new Model(values, Utils._.extend(this.options, this.attributes, { hasPrimaryKeys: this.hasPrimaryKeys }))
, self = this
Utils._.map(this.attributes, function(definition, name) { options = options || {}
if(typeof instance[name] == 'undefined') { instance.__definition = this
var value = null
if(self.rawAttributes.hasOwnProperty(name) && self.rawAttributes[name].hasOwnProperty('defaultValue')) Utils._.map(this.attributes, function(definition, name) {
value = self.rawAttributes[name].defaultValue if(typeof instance[name] == 'undefined') {
var value = null
instance[name] = value if(self.rawAttributes.hasOwnProperty(name) && self.rawAttributes[name].hasOwnProperty('defaultValue'))
instance.addAttribute(name, value) value = self.rawAttributes[name].defaultValue
}
})
Utils._.each(this.options.instanceMethods || {}, function(fct, name) { instance[name] = fct })
Utils._.each(this.associations, function(association, associationName) {
association.injectGetter(instance)
association.injectSetter(instance)
})
instance.isNewRecord = options.hasOwnProperty('isNewRecord') ? options.isNewRecord : true instance[name] = value
instance.addAttribute(name, value)
}
})
Utils._.each(this.options.instanceMethods || {}, function(fct, name) { instance[name] = fct })
Utils._.each(this.associations, function(association, associationName) {
association.injectGetter(instance)
association.injectSetter(instance)
})
instance.isNewRecord = options.hasOwnProperty('isNewRecord') ? options.isNewRecord : true
return instance return instance
} }
ModelFactory.prototype.create = function(values) {
return this.build(values).save()
}
ModelFactory.prototype.create = function(values) { ModelFactory.prototype.__defineGetter__('primaryKeys', function() {
return this.build(values).save() var result = {}
}
ModelFactory.prototype.__defineGetter__('primaryKeys', function() { Utils._.each(this.attributes, function(dataTypeString, attributeName) {
var result = {} if((attributeName != 'id') && (dataTypeString.indexOf('PRIMARY KEY') > -1))
result[attributeName] = dataTypeString
})
Utils._.each(this.attributes, function(dataTypeString, attributeName) { return result
if((attributeName != 'id') && (dataTypeString.indexOf('PRIMARY KEY') > -1))
result[attributeName] = dataTypeString
}) })
return result ModelFactory.prototype.__defineGetter__('primaryKeyCount', function() {
}) return Utils._.keys(this.primaryKeys).length
})
ModelFactory.prototype.__defineGetter__('primaryKeyCount', function() { ModelFactory.prototype.__defineGetter__('hasPrimaryKeys', function() {
return Utils._.keys(this.primaryKeys).length return this.primaryKeyCount > 0
}) })
ModelFactory.prototype.__defineGetter__('hasPrimaryKeys', function() { // private
return this.primaryKeyCount > 0
})
// private var query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.modelManager.sequelize
ModelFactory.prototype.__query = function() { // add this as the second argument
var args = Utils._.map(arguments, function(arg, _) { return arg }) if(arguments.length == 1) args.push(this)
, s = this.modelManager.sequelize return s.query.apply(s, args)
}
// add this as the second argument var addOptionalClassMethods = function() {
if(arguments.length == 1) args.push(this) var self = this
return s.query.apply(s, args) Utils._.each(this.options.classMethods || {}, function(fct, name) { self[name] = fct })
} }
ModelFactory.prototype.__addOptionalClassMethods = function() { var addDefaultAttributes = function() {
var self = this var defaultAttributes = {id: {type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true}}
Utils._.each(this.options.classMethods || {}, function(fct, name) { self[name] = fct }) , self = this
}
ModelFactory.prototype.__addDefaultAttributes = function() { if(this.hasPrimaryKeys) defaultAttributes = {}
var defaultAttributes = {id: {type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true}}
, self = this
if(this.hasPrimaryKeys) defaultAttributes = {} if(this.options.timestamps) {
defaultAttributes[Utils._.underscoredIf('createdAt', this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
defaultAttributes[Utils._.underscoredIf('updatedAt', this.options.underscored)] = {type: DataTypes.DATE, allowNull: false}
if(this.options.timestamps) { if(this.options.paranoid)
defaultAttributes[Utils._.underscoredIf('createdAt', this.options.underscored)] = {type: DataTypes.DATE, allowNull: false} defaultAttributes[Utils._.underscoredIf('deletedAt', this.options.underscored)] = {type: DataTypes.DATE}
defaultAttributes[Utils._.underscoredIf('updatedAt', this.options.underscored)] = {type: DataTypes.DATE, allowNull: false} }
if(this.options.paranoid) defaultAttributes = Utils.simplifyAttributes(defaultAttributes)
defaultAttributes[Utils._.underscoredIf('deletedAt', this.options.underscored)] = {type: DataTypes.DATE} Utils._.map(defaultAttributes, function(value, attr) { self.attributes[attr] = value })
} }
defaultAttributes = Utils.simplifyAttributes(defaultAttributes) var findAutoIncrementField = function() {
Utils._.map(defaultAttributes, function(value, attr) { self.attributes[attr] = value }) var self = this
}
this.autoIncrementField = null
ModelFactory.prototype.__findAutoIncrementField = function() { Utils._.map(this.attributes, function(definition, name) {
var self = this if (definition && (definition.indexOf('auto_increment') > -1)) {
if (self.autoIncrementField)
throw new Error('Invalid model definition. Only one autoincrement field allowed.')
else
self.autoIncrementField = name
}
})
}
this.autoIncrementField = null Utils._.extend(ModelFactory.prototype, require("./associations/mixin"))
Utils._.map(this.attributes, function(definition, name) {
if (definition && (definition.indexOf('auto_increment') > -1)) {
if (self.autoIncrementField)
throw new Error('Invalid model definition. Only one autoincrement field allowed.')
else
self.autoIncrementField = name
}
})
}
Utils._.extend(ModelFactory.prototype, require("./associations/mixin")) return ModelFactory
})()
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!