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

Commit f1c73c6e by Sascha Depold

scoped attributes

1 parent 17128deb
var Utils = require('./../utils')
var HasManyDoubleLinked = module.exports = function(definition, instance) {
this.definition = definition
this.__definition = definition
this.instance = instance
}
HasManyDoubleLinked.prototype.injectGetter = function() {
var self = this
var customEventEmitter = new Utils.CustomEventEmitter(function() {
var where = {}
where[self.definition.identifier] = self.instance.id
var primaryKeys = Utils._.keys(self.definition.connectorModel.rawAttributes)
, foreignKey = primaryKeys.filter(function(pk) { return pk != self.definition.identifier })[0]
self.definition.connectorModel.findAll({where: where}).on('success', function(associatedObjects) {
where[self.__definition.identifier] = self.instance.id
var primaryKeys = Utils._.keys(self.__definition.connectorModel.rawAttributes)
, foreignKey = primaryKeys.filter(function(pk) { return pk != self.__definition.identifier })[0]
self.__definition.connectorModel.findAll({where: where}).on('success', function(associatedObjects) {
var ids = associatedObjects.map(function(obj) { return obj[foreignKey] })
if (ids.length == 0) {
customEventEmitter.emit('success', [])
} else {
self.definition.target.findAll({where: 'id in (' + ids.join(", ") + ')'})
self.__definition.target.findAll({where: 'id in (' + ids.join(", ") + ')'})
.on('success', function(objects) { customEventEmitter.emit('success', objects) })
.on('failure', function(err){ customEventEmitter.emit('failure', err) })
}
})
})
return customEventEmitter.run()
}
HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociations, newAssociations) {
var self = this
var emitter = new Utils.CustomEventEmitter(function() {
var chainer = new Utils.QueryChainer
var foreignIdentifier = self.definition.target.associations[self.definition.associationAccessor].identifier
var foreignIdentifier = self.__definition.target.associations[self.__definition.associationAccessor].identifier
var obsoleteAssociations = oldAssociations.filter(function(obj) { return !obj.equalsOneOf(newAssociations) })
if(obsoleteAssociations.length == 0)
......@@ -44,13 +44,13 @@ HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociat
obsoleteAssociations.forEach(function(associatedObject) {
var where = {}
, primaryKeys = Utils._.keys(self.definition.connectorModel.rawAttributes)
, foreignKey = primaryKeys.filter(function(pk) { return pk != self.definition.identifier })[0]
, primaryKeys = Utils._.keys(self.__definition.connectorModel.rawAttributes)
, foreignKey = primaryKeys.filter(function(pk) { return pk != self.__definition.identifier })[0]
where[self.definition.identifier] = self.instance.id
where[self.__definition.identifier] = self.instance.id
where[foreignKey] = associatedObject.id
self.definition.connectorModel.find({where: where}).on('success', function(connector) {
self.__definition.connectorModel.find({where: where}).on('success', function(connector) {
chainer.add(connector.destroy())
if(chainer.emitters.length == obsoleteAssociations.length) {
......@@ -68,21 +68,21 @@ HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociat
HasManyDoubleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations) {
var self = this
this.destroyObsoleteAssociations(oldAssociations, newAssociations)
.on('failure', function(err) { emitter.emit('failure', err) })
.on('success', function() {
var chainer = new Utils.QueryChainer
, association = self.definition.target.associations[self.definition.associationAccessor]
, association = self.__definition.target.associations[self.__definition.associationAccessor]
, foreignIdentifier = association.isSelfAssociation ? association.foreignIdentifier : association.identifier
, unassociatedObjects = newAssociations.filter(function(obj) { return !obj.equalsOneOf(oldAssociations) })
unassociatedObjects.forEach(function(unassociatedObject) {
var attributes = {}
attributes[self.definition.identifier] = self.instance.id
attributes[self.__definition.identifier] = self.instance.id
attributes[foreignIdentifier] = unassociatedObject.id
chainer.add(self.definition.connectorModel.create(attributes))
chainer.add(self.__definition.connectorModel.create(attributes))
})
chainer
......@@ -90,4 +90,4 @@ HasManyDoubleLinked.prototype.injectSetter = function(emitter, oldAssociations,
.on('success', function() { emitter.emit('success', newAssociations) })
.on('failure', function(err) { emitter.emit('failure', err) })
})
}
\ No newline at end of file
}
var Utils = require('./../utils')
var HasManySingleLinked = module.exports = function(definition, instance) {
this.definition = definition
this.__definition = definition
this.instance = instance
}
HasManySingleLinked.prototype.injectGetter = function() {
var where = {}
where[this.definition.identifier] = this.instance.id
return this.definition.target.findAll({where: where})
where[this.__definition.identifier] = this.instance.id
return this.__definition.target.findAll({where: where})
}
HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations) {
var self = this
// clear the old associations
oldAssociations.forEach(function(associatedObject) {
associatedObject[self.definition.identifier] = null
associatedObject[self.__definition.identifier] = null
associatedObject.save()
})
// set the new one
var chainer = new Utils.QueryChainer
newAssociations.forEach(function(associatedObject) {
associatedObject[self.definition.identifier] = self.instance.id
associatedObject[self.__definition.identifier] = self.instance.id
chainer.add(associatedObject.save())
})
chainer
.run()
.on('success', function() { emitter.emit('success', newAssociations) })
.on('failure', function(err) { emitter.emit('failure', err) })
}
\ No newline at end of file
}
......@@ -31,7 +31,7 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
, 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}
......@@ -69,14 +69,14 @@ ModelDefinition.prototype.query = function() {
ModelDefinition.prototype.sync = function(options) {
options = options || {}
var self = this
var doQuery = function() {
self.query(QueryGenerator.createTableQuery(self.tableName, self.attributes))
.on('success', function() { self.emit('success', self) })
.on('failure', function(err) { self.emit('failure', err) })
}
if(options.force) {
this.drop()
.on('success', function() { doQuery() })
......@@ -84,7 +84,7 @@ ModelDefinition.prototype.sync = function(options) {
} else {
doQuery()
}
return this
}
......@@ -98,7 +98,7 @@ ModelDefinition.prototype.__defineGetter__('all', function() {
ModelDefinition.prototype.count = function(options) {
var self = this
var emitter = new Utils.CustomEventEmitter(function() {
self.query(QueryGenerator.countQuery(self.tableName, options), self, {plain: true}).on('success', function(obj) {
emitter.emit('success', obj['count(*)'])
......@@ -118,7 +118,7 @@ ModelDefinition.prototype.find = function(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
......@@ -129,9 +129,9 @@ ModelDefinition.prototype.find = function(options) {
var NullEmitter = require("./null-emitter")
return new NullEmitter()
}
options.limit = 1
var query = QueryGenerator.selectQuery(this.tableName, options)
return this.query(query, this, {plain: true})
}
......@@ -139,9 +139,9 @@ ModelDefinition.prototype.find = function(options) {
ModelDefinition.prototype.build = function(values, options) {
var instance = new Model(values, Utils._.extend(this.options, {hasPrimaryKeys: this.hasPrimaryKeys}))
, self = this
options = options || {}
instance.definition = this
instance.__definition = this
Utils._.map(this.attributes, function(definition, name) {
if(typeof instance[name] == 'undefined') {
......@@ -159,9 +159,9 @@ ModelDefinition.prototype.build = function(values, options) {
association.injectGetter(instance)
association.injectSetter(instance)
})
instance.isNewRecord = options.hasOwnProperty('isNewRecord') ? options.isNewRecord : true
return instance
}
......@@ -188,4 +188,4 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() {
return this.primaryKeyCount > 0
})
Utils._.map(require("./associations/mixin").classMethods, function(fct, name) { ModelDefinition.prototype[name] = fct })
\ No newline at end of file
Utils._.map(require("./associations/mixin").classMethods, function(fct, name) { ModelDefinition.prototype[name] = fct })
......@@ -5,7 +5,7 @@ var Utils = require("./utils")
var Model = module.exports = function(values, options) {
var self = this
this.definition = null // will be set in Model.build
this.__definition = null // will be set in Model.build
this.attributes = []
this.__options = options || {}
......@@ -45,7 +45,7 @@ Model.prototype.addAttribute = function(attribute, value) {
Model.prototype.query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.definition.modelManager.sequelize
, s = this.__definition.modelManager.sequelize
args.push(this)
return s.query.apply(s, args)
......@@ -60,7 +60,7 @@ Model.prototype.save = function() {
if(this.isNewRecord) {
var self = this
var eventEmitter = new Utils.CustomEventEmitter(function() {
self.query(QueryGenerator.insertQuery(self.definition.tableName, self.values))
self.query(QueryGenerator.insertQuery(self.__definition.tableName, self.values))
.on('success', function(obj) {
obj.isNewRecord = false
eventEmitter.emit('success', obj)
......@@ -70,14 +70,14 @@ Model.prototype.save = function() {
return eventEmitter.run()
} else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return this.query(QueryGenerator.updateQuery(this.definition.tableName, this.values, identifier))
return this.query(QueryGenerator.updateQuery(this.__definition.tableName, this.values, identifier))
}
}
Model.prototype.updateAttributes = function(updates) {
var self = this
var readOnlyAttributes = Utils._.keys(this.definition.primaryKeys)
var readOnlyAttributes = Utils._.keys(this.__definition.primaryKeys)
readOnlyAttributes.push('id')
readOnlyAttributes.push('createdAt')
readOnlyAttributes.push('updatedAt')
......@@ -100,16 +100,16 @@ Model.prototype.destroy = function() {
return this.save()
} else {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : this.id
return this.query(QueryGenerator.deleteQuery(this.definition.tableName, identifier))
return this.query(QueryGenerator.deleteQuery(this.__definition.tableName, identifier))
}
}
Model.prototype.__defineGetter__("identifiers", function() {
var primaryKeys = Utils._.keys(this.definition.primaryKeys)
var primaryKeys = Utils._.keys(this.__definition.primaryKeys)
, result = {}
, self = this
if(!this.definition.hasPrimaryKeys)
if(!this.__definition.hasPrimaryKeys)
primaryKeys = ['id']
primaryKeys.forEach(function(identifier) {
......@@ -141,7 +141,7 @@ Model.prototype.__defineGetter__('primaryKeyValues', function() {
var result = {}
, self = this
Utils._.each(this.definition.primaryKeys, function(_, attr) {
Utils._.each(this.__definition.primaryKeys, function(_, attr) {
result[attr] = self[attr]
})
......
var Utils = require("./utils")
var Query = module.exports = function(client, callee, options) {
var self = this
this.client = client
this.callee = callee
this.options = options || {}
......@@ -21,7 +21,7 @@ Query.prototype.run = function(query) {
this.client.query(this.sql, function(err, results, fields) {
err ? self.onFailure(err) : self.onSuccess(self.sql, results, fields)
}).setMaxListeners(100)
return this
}
......@@ -39,7 +39,7 @@ Query.prototype.onSuccess = function(query, results, fields) {
// add the inserted row id to the instance
if (this.callee && (query.indexOf('INSERT INTO') == 0) && (results.hasOwnProperty('insertId')))
this.callee[this.callee.definition.autoIncrementField] = results.insertId
this.callee[this.callee.__definition.autoIncrementField] = results.insertId
// transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find)
......@@ -57,4 +57,4 @@ Query.prototype.onSuccess = function(query, results, fields) {
Query.prototype.onFailure = function(err) {
this.unbindClient()
this.emit('failure', err, this.callee)
}
\ No newline at end of file
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!