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

Commit 4dbbb89b by Sascha Depold

private method scoping for associations

1 parent 3e57b469
var Utils = require("./../utils") var Utils = require("./../utils")
, DataTypes = require('./../data-types') , DataTypes = require('./../data-types')
var BelongsTo = module.exports = function(srcModel, targetModel, options) { module.exports = (function() {
var BelongsTo = function(srcModel, targetModel, options) {
this.source = srcModel this.source = srcModel
this.target = targetModel this.target = targetModel
this.options = options this.options = options
...@@ -13,10 +14,10 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) { ...@@ -13,10 +14,10 @@ var BelongsTo = module.exports = function(srcModel, targetModel, options) {
this.associationAccessor = this.isSelfAssociation this.associationAccessor = this.isSelfAssociation
? Utils.combineTableNames(this.target.tableName, this.options.as || this.target.tableName) ? Utils.combineTableNames(this.target.tableName, this.options.as || this.target.tableName)
: this.target.tableName : this.target.tableName
} }
// the id is in the source table // the id is in the source table
BelongsTo.prototype.injectAttributes = function() { BelongsTo.prototype.injectAttributes = function() {
var newAttributes = {} var newAttributes = {}
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.tableName) + "Id", this.source.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.tableName) + "Id", this.source.options.underscored)
...@@ -24,9 +25,9 @@ BelongsTo.prototype.injectAttributes = function() { ...@@ -24,9 +25,9 @@ BelongsTo.prototype.injectAttributes = function() {
Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes)) Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes))
return this return this
} }
BelongsTo.prototype.injectGetter = function(obj) { BelongsTo.prototype.injectGetter = function(obj) {
var self = this var self = this
, accessor = Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName))) , accessor = Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName)))
...@@ -36,9 +37,9 @@ BelongsTo.prototype.injectGetter = function(obj) { ...@@ -36,9 +37,9 @@ BelongsTo.prototype.injectGetter = function(obj) {
} }
return this return this
} }
BelongsTo.prototype.injectSetter = function(obj) { BelongsTo.prototype.injectSetter = function(obj) {
var self = this var self = this
, accessor = Utils._.camelize('set_' + (this.options.as || Utils.singularize(this.target.tableName))) , accessor = Utils._.camelize('set_' + (this.options.as || Utils.singularize(this.target.tableName)))
...@@ -48,4 +49,7 @@ BelongsTo.prototype.injectSetter = function(obj) { ...@@ -48,4 +49,7 @@ BelongsTo.prototype.injectSetter = function(obj) {
} }
return this return this
} }
\ No newline at end of file
return BelongsTo
})()
var Utils = require('./../utils') var Utils = require('./../utils')
var HasManyDoubleLinked = module.exports = function(definition, instance) { module.exports = (function() {
var HasManyDoubleLinked = function(definition, instance) {
this.__definition = definition this.__definition = definition
this.instance = instance this.instance = instance
} }
HasManyDoubleLinked.prototype.injectGetter = function() { HasManyDoubleLinked.prototype.injectGetter = function() {
var self = this var self = this
var customEventEmitter = new Utils.CustomEventEmitter(function() { var customEventEmitter = new Utils.CustomEventEmitter(function() {
...@@ -29,9 +30,37 @@ HasManyDoubleLinked.prototype.injectGetter = function() { ...@@ -29,9 +30,37 @@ HasManyDoubleLinked.prototype.injectGetter = function() {
}) })
return customEventEmitter.run() return customEventEmitter.run()
} }
HasManyDoubleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations) {
var self = this
destroyObsoleteAssociations.call(this, 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]
, 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[foreignIdentifier] = unassociatedObject.id
chainer.add(self.__definition.connectorModel.create(attributes))
})
chainer
.run()
.on('success', function() { emitter.emit('success', newAssociations) })
.on('failure', function(err) { emitter.emit('failure', err) })
})
}
// private
HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociations, newAssociations) { var destroyObsoleteAssociations = function(oldAssociations, newAssociations) {
var self = this var self = this
var emitter = new Utils.CustomEventEmitter(function() { var emitter = new Utils.CustomEventEmitter(function() {
...@@ -64,30 +93,7 @@ HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociat ...@@ -64,30 +93,7 @@ HasManyDoubleLinked.prototype.destroyObsoleteAssociations = function(oldAssociat
}) })
}) })
return emitter.run() return emitter.run()
} }
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]
, 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[foreignIdentifier] = unassociatedObject.id
chainer.add(self.__definition.connectorModel.create(attributes))
})
chainer return HasManyDoubleLinked
.run() })()
.on('success', function() { emitter.emit('success', newAssociations) })
.on('failure', function(err) { emitter.emit('failure', err) })
})
}
var Utils = require('./../utils') var Utils = require('./../utils')
var HasManySingleLinked = module.exports = function(definition, instance) { module.exports = (function() {
var HasManySingleLinked = function(definition, instance) {
this.__definition = definition this.__definition = definition
this.instance = instance this.instance = instance
} }
HasManySingleLinked.prototype.injectGetter = function() { HasManySingleLinked.prototype.injectGetter = function() {
var where = {} var where = {}
where[this.__definition.identifier] = this.instance.id where[this.__definition.identifier] = this.instance.id
return this.__definition.target.findAll({where: where}) return this.__definition.target.findAll({where: where})
} }
HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations) { HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, newAssociations) {
var self = this var self = this
// clear the old associations // clear the old associations
...@@ -31,4 +32,7 @@ HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations, ...@@ -31,4 +32,7 @@ HasManySingleLinked.prototype.injectSetter = function(emitter, oldAssociations,
.run() .run()
.on('success', function() { emitter.emit('success', newAssociations) }) .on('success', function() { emitter.emit('success', newAssociations) })
.on('failure', function(err) { emitter.emit('failure', err) }) .on('failure', function(err) { emitter.emit('failure', err) })
} }
return HasManySingleLinked
})()
...@@ -4,7 +4,8 @@ var Utils = require("./../utils") ...@@ -4,7 +4,8 @@ var Utils = require("./../utils")
var HasManySingleLinked = require("./has-many-single-linked") var HasManySingleLinked = require("./has-many-single-linked")
, HasManyMultiLinked = require("./has-many-double-linked") , HasManyMultiLinked = require("./has-many-double-linked")
var HasMany = module.exports = function(srcModel, targetModel, options) { module.exports = (function() {
var HasMany = function(srcModel, targetModel, options) {
this.source = srcModel this.source = srcModel
this.target = targetModel this.target = targetModel
this.options = options this.options = options
...@@ -23,11 +24,11 @@ var HasMany = module.exports = function(srcModel, targetModel, options) { ...@@ -23,11 +24,11 @@ var HasMany = module.exports = function(srcModel, targetModel, options) {
add: Utils._.camelize(Utils.singularize('add_' + as)), add: Utils._.camelize(Utils.singularize('add_' + as)),
remove: Utils._.camelize(Utils.singularize('remove_' + as)) remove: Utils._.camelize(Utils.singularize('remove_' + as))
} }
} }
// the id is in the target table // the id is in the target table
// or in an extra table which connects two tables // or in an extra table which connects two tables
HasMany.prototype.injectAttributes = function() { HasMany.prototype.injectAttributes = function() {
var multiAssociation = this.target.associations.hasOwnProperty(this.associationAccessor) var multiAssociation = this.target.associations.hasOwnProperty(this.associationAccessor)
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored)
...@@ -58,9 +59,9 @@ HasMany.prototype.injectAttributes = function() { ...@@ -58,9 +59,9 @@ HasMany.prototype.injectAttributes = function() {
} }
return this return this
} }
HasMany.prototype.injectGetter = function(obj) { HasMany.prototype.injectGetter = function(obj) {
var self = this var self = this
obj[this.accessors.get] = function() { obj[this.accessors.get] = function() {
...@@ -69,9 +70,9 @@ HasMany.prototype.injectGetter = function(obj) { ...@@ -69,9 +70,9 @@ HasMany.prototype.injectGetter = function(obj) {
} }
return this return this
} }
HasMany.prototype.injectSetter = function(obj) { HasMany.prototype.injectSetter = function(obj) {
var self = this var self = this
obj[this.accessors.set] = function(newAssociatedObjects) { obj[this.accessors.set] = function(newAssociatedObjects) {
...@@ -124,4 +125,7 @@ HasMany.prototype.injectSetter = function(obj) { ...@@ -124,4 +125,7 @@ HasMany.prototype.injectSetter = function(obj) {
} }
return this return this
} }
\ No newline at end of file
return HasMany
})()
var Utils = require("./../utils") var Utils = require("./../utils")
, DataTypes = require('./../data-types') , DataTypes = require('./../data-types')
var HasOne = module.exports = function(srcModel, targetModel, options) { module.exports = (function() {
var HasOne = function(srcModel, targetModel, options) {
this.source = srcModel this.source = srcModel
this.target = targetModel this.target = targetModel
this.options = options this.options = options
...@@ -18,10 +19,10 @@ var HasOne = module.exports = function(srcModel, targetModel, options) { ...@@ -18,10 +19,10 @@ var HasOne = module.exports = function(srcModel, targetModel, options) {
get: Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName))), get: Utils._.camelize('get_' + (this.options.as || Utils.singularize(this.target.tableName))),
set: Utils._.camelize('set_' + (this.options.as || Utils.singularize(this.target.tableName))) set: Utils._.camelize('set_' + (this.options.as || Utils.singularize(this.target.tableName)))
} }
} }
// the id is in the target table // the id is in the target table
HasOne.prototype.injectAttributes = function() { HasOne.prototype.injectAttributes = function() {
var newAttributes = {} var newAttributes = {}
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored)
...@@ -29,9 +30,9 @@ HasOne.prototype.injectAttributes = function() { ...@@ -29,9 +30,9 @@ HasOne.prototype.injectAttributes = function() {
Utils._.extend(this.target.attributes, Utils.simplifyAttributes(newAttributes)) Utils._.extend(this.target.attributes, Utils.simplifyAttributes(newAttributes))
return this return this
} }
HasOne.prototype.injectGetter = function(obj) { HasOne.prototype.injectGetter = function(obj) {
var self = this var self = this
obj[this.accessors.get] = function() { obj[this.accessors.get] = function() {
...@@ -43,9 +44,9 @@ HasOne.prototype.injectGetter = function(obj) { ...@@ -43,9 +44,9 @@ HasOne.prototype.injectGetter = function(obj) {
} }
return this return this
} }
HasOne.prototype.injectSetter = function(obj) { HasOne.prototype.injectSetter = function(obj) {
var self = this var self = this
obj[this.accessors.set] = function(associatedObject) { obj[this.accessors.set] = function(associatedObject) {
...@@ -66,4 +67,7 @@ HasOne.prototype.injectSetter = function(obj) { ...@@ -66,4 +67,7 @@ HasOne.prototype.injectSetter = function(obj) {
} }
return this return this
} }
\ No newline at end of file
return HasOne
})()
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!