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

Commit ffeddc46 by Sascha Depold

use private method scoping

1 parent 59605e88
Showing with 56 additions and 52 deletions
...@@ -2,7 +2,8 @@ var Utils = require("./utils") ...@@ -2,7 +2,8 @@ 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 ModelFactory = function(name, attributes, options) {
var self = this var self = this
this.options = options || {} this.options = options || {}
...@@ -14,22 +15,22 @@ var ModelFactory = module.exports = function(name, attributes, options) { ...@@ -14,22 +15,22 @@ var ModelFactory = module.exports = function(name, attributes, options) {
this.modelManager = null // defined by model-manager during addModel this.modelManager = null // defined by model-manager during addModel
this.associations = {} this.associations = {}
this.__addDefaultAttributes() addDefaultAttributes.call(this)
this.__addOptionalClassMethods() addOptionalClassMethods.call(this)
this.__findAutoIncrementField() findAutoIncrementField.call(this)
} }
ModelFactory.prototype.__defineGetter__('QueryGenerator', function() { ModelFactory.prototype.__defineGetter__('QueryGenerator', function() {
return this.modelManager.sequelize.connectorManager.getQueryGenerator() return this.modelManager.sequelize.connectorManager.getQueryGenerator()
}) })
ModelFactory.prototype.sync = function(options) { ModelFactory.prototype.sync = function(options) {
options = Utils.merge(options || {}, this.options) options = Utils.merge(options || {}, this.options)
var self = this var self = this
var eventEmitter = new Utils.CustomEventEmitter(function() { var eventEmitter = new Utils.CustomEventEmitter(function() {
var doQuery = function() { var doQuery = function() {
self.__query(self.QueryGenerator.createTableQuery(self.tableName, self.attributes, options)) query.call(self, self.QueryGenerator.createTableQuery(self.tableName, self.attributes, options))
.on('success', function() { eventEmitter.emit('success', self) }) .on('success', function() { eventEmitter.emit('success', self) })
.on('failure', function(err) { eventEmitter.emit('failure', err) }) .on('failure', function(err) { eventEmitter.emit('failure', err) })
} }
...@@ -44,52 +45,52 @@ ModelFactory.prototype.sync = function(options) { ...@@ -44,52 +45,52 @@ ModelFactory.prototype.sync = function(options) {
}) })
return eventEmitter.run() return eventEmitter.run()
} }
ModelFactory.prototype.drop = function() { ModelFactory.prototype.drop = function() {
return this.__query(this.QueryGenerator.dropTableQuery(this.tableName, this.id)) return query.call(this, this.QueryGenerator.dropTableQuery(this.tableName, this.id))
} }
ModelFactory.prototype.all = function() { ModelFactory.prototype.all = function() {
return this.__query(this.QueryGenerator.selectQuery(this.tableName)) return query.call(this, this.QueryGenerator.selectQuery(this.tableName))
} }
ModelFactory.prototype.count = function(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.countQuery(self.tableName, 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['count(*)']) emitter.emit('success', obj['count(*)'])
}) })
}) })
return emitter.run() return emitter.run()
} }
ModelFactory.prototype.max = function(field, options) { ModelFactory.prototype.max = function(field, 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.maxQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) {
emitter.emit('success', obj['max']) emitter.emit('success', obj['max'])
}) })
}) })
return emitter.run() return emitter.run()
} }
ModelFactory.prototype.min = function(field, options) { ModelFactory.prototype.min = function(field, options) {
var self = this var self = this
var emitter = new Utils.CustomEventEmitter(function() { var emitter = new Utils.CustomEventEmitter(function() {
self.__query(self.QueryGenerator.minQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) { query.call(self, self.QueryGenerator.minQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) {
emitter.emit('success', obj['min']) emitter.emit('success', obj['min'])
}) })
}) })
return emitter.run() return emitter.run()
} }
ModelFactory.prototype.findAll = function(options) { ModelFactory.prototype.findAll = function(options) {
return this.__query(this.QueryGenerator.selectQuery(this.tableName, options)) return query.call(this, this.QueryGenerator.selectQuery(this.tableName, options))
} }
ModelFactory.prototype.find = function(options) { ModelFactory.prototype.find = function(options) {
// options is not a hash but an id // options is not a hash but an id
if(typeof options == 'number') if(typeof options == 'number')
options = {where: options} options = {where: options}
...@@ -110,11 +111,11 @@ ModelFactory.prototype.find = function(options) { ...@@ -110,11 +111,11 @@ ModelFactory.prototype.find = function(options) {
options.limit = 1 options.limit = 1
var query = this.QueryGenerator.selectQuery(this.tableName, options) var sql = this.QueryGenerator.selectQuery(this.tableName, options)
return this.__query(query, this, {plain: true}) return query.call(this, sql, this, {plain: true})
} }
ModelFactory.prototype.build = function(values, options) { ModelFactory.prototype.build = function(values, options) {
var instance = new Model(values, Utils._.extend(this.options, this.attributes, { hasPrimaryKeys: this.hasPrimaryKeys })) var instance = new Model(values, Utils._.extend(this.options, this.attributes, { hasPrimaryKeys: this.hasPrimaryKeys }))
, self = this , self = this
...@@ -141,13 +142,13 @@ ModelFactory.prototype.build = function(values, options) { ...@@ -141,13 +142,13 @@ ModelFactory.prototype.build = function(values, options) {
instance.isNewRecord = options.hasOwnProperty('isNewRecord') ? options.isNewRecord : true instance.isNewRecord = options.hasOwnProperty('isNewRecord') ? options.isNewRecord : true
return instance return instance
} }
ModelFactory.prototype.create = function(values) { ModelFactory.prototype.create = function(values) {
return this.build(values).save() return this.build(values).save()
} }
ModelFactory.prototype.__defineGetter__('primaryKeys', function() { ModelFactory.prototype.__defineGetter__('primaryKeys', function() {
var result = {} var result = {}
Utils._.each(this.attributes, function(dataTypeString, attributeName) { Utils._.each(this.attributes, function(dataTypeString, attributeName) {
...@@ -156,33 +157,33 @@ ModelFactory.prototype.__defineGetter__('primaryKeys', function() { ...@@ -156,33 +157,33 @@ ModelFactory.prototype.__defineGetter__('primaryKeys', function() {
}) })
return result return result
}) })
ModelFactory.prototype.__defineGetter__('primaryKeyCount', function() { ModelFactory.prototype.__defineGetter__('primaryKeyCount', function() {
return Utils._.keys(this.primaryKeys).length return Utils._.keys(this.primaryKeys).length
}) })
ModelFactory.prototype.__defineGetter__('hasPrimaryKeys', function() { ModelFactory.prototype.__defineGetter__('hasPrimaryKeys', function() {
return this.primaryKeyCount > 0 return this.primaryKeyCount > 0
}) })
// private // private
ModelFactory.prototype.__query = function() { var query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg }) var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.modelManager.sequelize , s = this.modelManager.sequelize
// add this as the second argument // add this as the second argument
if(arguments.length == 1) args.push(this) if(arguments.length == 1) args.push(this)
return s.query.apply(s, args) return s.query.apply(s, args)
} }
ModelFactory.prototype.__addOptionalClassMethods = function() { var addOptionalClassMethods = function() {
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 })
} }
ModelFactory.prototype.__addDefaultAttributes = function() { var addDefaultAttributes = function() {
var defaultAttributes = {id: {type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true}} var defaultAttributes = {id: {type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true}}
, self = this , self = this
...@@ -198,9 +199,9 @@ ModelFactory.prototype.__addDefaultAttributes = function() { ...@@ -198,9 +199,9 @@ ModelFactory.prototype.__addDefaultAttributes = function() {
defaultAttributes = Utils.simplifyAttributes(defaultAttributes) defaultAttributes = Utils.simplifyAttributes(defaultAttributes)
Utils._.map(defaultAttributes, function(value, attr) { self.attributes[attr] = value }) Utils._.map(defaultAttributes, function(value, attr) { self.attributes[attr] = value })
} }
ModelFactory.prototype.__findAutoIncrementField = function() { var findAutoIncrementField = function() {
var self = this var self = this
this.autoIncrementField = null this.autoIncrementField = null
...@@ -212,6 +213,9 @@ ModelFactory.prototype.__findAutoIncrementField = function() { ...@@ -212,6 +213,9 @@ ModelFactory.prototype.__findAutoIncrementField = function() {
self.autoIncrementField = name self.autoIncrementField = name
} }
}) })
} }
Utils._.extend(ModelFactory.prototype, require("./associations/mixin"))
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!