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

Commit eb7a4f1c by Mick Hansen

First steps in using a single through parameter for hasMany

1 parent 06367a93
...@@ -17,7 +17,7 @@ module.exports = (function() { ...@@ -17,7 +17,7 @@ module.exports = (function() {
var customEventEmitter = new Utils.CustomEventEmitter(function() { var customEventEmitter = new Utils.CustomEventEmitter(function() {
var where = {} var where = {}
, connectorDAO = self.__factory.connectorDAO , through = self.__factory.through
, options = _options || {} , options = _options || {}
, queryOptions = {} , queryOptions = {}
, association = self.__factory.target.associations[self.__factory.associationAccessor] , association = self.__factory.target.associations[self.__factory.associationAccessor]
...@@ -26,19 +26,19 @@ module.exports = (function() { ...@@ -26,19 +26,19 @@ module.exports = (function() {
var instancePrimaryKeys = Object.keys(self.instance.daoFactory.primaryKeys) var instancePrimaryKeys = Object.keys(self.instance.daoFactory.primaryKeys)
, instancePrimaryKey = instancePrimaryKeys.length > 0 ? instancePrimaryKeys[0] : 'id' , instancePrimaryKey = instancePrimaryKeys.length > 0 ? instancePrimaryKeys[0] : 'id'
where[connectorDAO.tableName+"."+self.__factory.identifier] = self.instance[instancePrimaryKey] where[through.tableName+"."+self.__factory.identifier] = self.instance[instancePrimaryKey]
var primaryKeys = Object.keys(connectorDAO.primaryKeys) var primaryKeys = Object.keys(through.primaryKeys)
, foreignKey = primaryKeys.filter(function(pk) { return pk != self.__factory.identifier })[0] , foreignKey = primaryKeys.filter(function(pk) { return pk != self.__factory.identifier })[0]
, foreignPrimary = Object.keys(self.__factory.target.primaryKeys) , foreignPrimary = Object.keys(self.__factory.target.primaryKeys)
foreignPrimary = foreignPrimary.length === 1 ? foreignPrimary[0] : 'id' foreignPrimary = foreignPrimary.length === 1 ? foreignPrimary[0] : 'id'
where[connectorDAO.tableName+"."+foreignKey] = {join: self.__factory.target.tableName+"."+foreignPrimary} where[through.tableName+"."+foreignKey] = {join: self.__factory.target.tableName+"."+foreignPrimary}
if (association.hasJoinTableModel) { if (association.hasJoinTableModel) {
queryOptions.hasJoinTableModel = true queryOptions.hasJoinTableModel = true
queryOptions.joinTableModel = connectorDAO queryOptions.joinTableModel = through
if (!options.attributes) { if (!options.attributes) {
options.attributes = [ options.attributes = [
...@@ -49,15 +49,15 @@ module.exports = (function() { ...@@ -49,15 +49,15 @@ module.exports = (function() {
if (options.joinTableAttributes) { if (options.joinTableAttributes) {
options.joinTableAttributes.forEach(function (elem) { options.joinTableAttributes.forEach(function (elem) {
options.attributes.push( options.attributes.push(
self.QueryInterface.quoteIdentifiers(connectorDAO.tableName + '.' + elem) + ' as ' + self.QueryInterface.quoteIdentifiers(through.tableName + '.' + elem) + ' as ' +
self.QueryInterface.quoteIdentifier(connectorDAO.name + '.' + elem, true) self.QueryInterface.quoteIdentifier(through.name + '.' + elem, true)
) )
}) })
} else { } else {
Utils._.forOwn(connectorDAO.rawAttributes, function (elem, key) { Utils._.forOwn(through.rawAttributes, function (elem, key) {
options.attributes.push( options.attributes.push(
self.QueryInterface.quoteIdentifiers(connectorDAO.tableName + '.' + key) + ' as ' + self.QueryInterface.quoteIdentifiers(through.tableName + '.' + key) + ' as ' +
self.QueryInterface.quoteIdentifier(connectorDAO.name + '.' + key, true) self.QueryInterface.quoteIdentifier(through.name + '.' + key, true)
) )
}) })
} }
...@@ -81,7 +81,7 @@ module.exports = (function() { ...@@ -81,7 +81,7 @@ module.exports = (function() {
options.where = where; options.where = where;
} }
self.__factory.target.findAllJoin(connectorDAO.tableName, options, queryOptions) self.__factory.target.findAllJoin(through.tableName, options, queryOptions)
.on('success', function(objects) { customEventEmitter.emit('success', objects) }) .on('success', function(objects) { customEventEmitter.emit('success', objects) })
.on('error', function(err){ customEventEmitter.emit('error', err) }) .on('error', function(err){ customEventEmitter.emit('error', err) })
.on('sql', function(sql) { customEventEmitter.emit('sql', sql)}) .on('sql', function(sql) { customEventEmitter.emit('sql', sql)})
...@@ -120,10 +120,10 @@ module.exports = (function() { ...@@ -120,10 +120,10 @@ module.exports = (function() {
if (!newObj) { if (!newObj) {
obsoleteAssociations.push(old) obsoleteAssociations.push(old)
} else if (association.hasJoinTableModel) { } else if (Object(association.through) === association.through) {
var changedAssociation = { var changedAssociation = {
where: {}, where: {},
attributes: Utils._.defaults({}, newObj[self.__factory.connectorDAO.name], defaultAttributes) attributes: Utils._.defaults({}, newObj[self.__factory.through.name], defaultAttributes)
} }
changedAssociation.where[self.__factory.identifier] = self.instance[self.__factory.identifier] || self.instance.id changedAssociation.where[self.__factory.identifier] = self.instance[self.__factory.identifier] || self.instance.id
...@@ -143,7 +143,7 @@ module.exports = (function() { ...@@ -143,7 +143,7 @@ module.exports = (function() {
where[self.__factory.identifier] = ((sourceKeys.length === 1) ? self.instance[sourceKeys[0]] : self.instance.id) where[self.__factory.identifier] = ((sourceKeys.length === 1) ? self.instance[sourceKeys[0]] : self.instance.id)
where[foreignIdentifier] = foreignIds where[foreignIdentifier] = foreignIds
chainer.add(self.__factory.connectorDAO.destroy(where, options)) chainer.add(self.__factory.through.destroy(where, options))
} }
if (unassociatedObjects.length > 0) { if (unassociatedObjects.length > 0) {
...@@ -154,18 +154,18 @@ module.exports = (function() { ...@@ -154,18 +154,18 @@ module.exports = (function() {
attributes[foreignIdentifier] = ((targetKeys.length === 1) ? unassociatedObject[targetKeys[0]] : unassociatedObject.id) attributes[foreignIdentifier] = ((targetKeys.length === 1) ? unassociatedObject[targetKeys[0]] : unassociatedObject.id)
if (association.hasJoinTableModel) { if (association.hasJoinTableModel) {
attributes = Utils._.defaults(attributes, unassociatedObject[association.connectorDAO.name], defaultAttributes) attributes = Utils._.defaults(attributes, unassociatedObject[association.through.name], defaultAttributes)
} }
return attributes return attributes
}) })
chainer.add(self.__factory.connectorDAO.bulkCreate(bulk, options)) chainer.add(self.__factory.through.bulkCreate(bulk, options))
} }
if (changedAssociations.length > 0) { if (changedAssociations.length > 0) {
changedAssociations.forEach(function (assoc) { changedAssociations.forEach(function (assoc) {
chainer.add(self.__factory.connectorDAO.update(assoc.attributes, assoc.where, options)) chainer.add(self.__factory.through.update(assoc.attributes, assoc.where, options))
}) })
} }
...@@ -189,15 +189,15 @@ module.exports = (function() { ...@@ -189,15 +189,15 @@ module.exports = (function() {
if (exists) { // implies hasJoinTableModel === true if (exists) { // implies hasJoinTableModel === true
var where = attributes var where = attributes
attributes = Utils._.defaults({}, newAssociation[association.connectorDAO.name], additionalAttributes) attributes = Utils._.defaults({}, newAssociation[association.through.name], additionalAttributes)
association.connectorDAO.update(attributes, where).proxy(emitterProxy) association.through.update(attributes, where).proxy(emitterProxy)
} else { } else {
if (association.hasJoinTableModel === true) { if (Object(association.through) === association.through) {
attributes = Utils._.defaults(attributes, newAssociation[association.connectorDAO.name], additionalAttributes) attributes = Utils._.defaults(attributes, newAssociation[association.through.name], additionalAttributes)
} }
this.__factory.connectorDAO.create(attributes) this.__factory.through.create(attributes)
.success(function() { emitterProxy.emit('success', newAssociation) }) .success(function() { emitterProxy.emit('success', newAssociation) })
.error(function(err) { emitterProxy.emit('error', err) }) .error(function(err) { emitterProxy.emit('error', err) })
.on('sql', function(sql) { emitterProxy.emit('sql', sql) }) .on('sql', function(sql) { emitterProxy.emit('sql', sql) })
......
...@@ -11,24 +11,32 @@ module.exports = (function() { ...@@ -11,24 +11,32 @@ module.exports = (function() {
this.source = srcDAO this.source = srcDAO
this.target = targetDAO this.target = targetDAO
this.options = options this.options = options
this.useJunctionTable = this.options.useJunctionTable === undefined ? true : this.options.useJunctionTable this.through = options.through
this.isSelfAssociation = (this.source.tableName === this.target.tableName) this.isSelfAssociation = (this.source.tableName === this.target.tableName)
this.hasJoinTableModel = !!this.options.joinTableModel
var combinedTableName; // Map to through for BC
if (this.hasJoinTableModel) { if (this.through === undefined) {
combinedTableName = this.options.joinTableModel.tableName this.through = this.options.joinTableModel || this.options.joinTableName;
} else if (this.options.joinTableName) { }
combinedTableName = this.options.joinTableName if (typeof this.through === "string") {
} else { this.through = this.source.daoFactoryManager.sequelize.define(this.through, {}, this.options)
combinedTableName = Utils.combineTableNames( }
if (this.through === undefined) {
if (this.options.useJunctionTable === false) {
this.through = null;
} else {
this.through = true;
}
}
if (this.through === true) {
this.through = Utils.combineTableNames(
this.source.tableName, this.source.tableName,
this.isSelfAssociation ? (this.options.as || this.target.tableName) : this.target.tableName this.isSelfAssociation ? (this.options.as || this.target.tableName) : this.target.tableName
) )
} };
this.options.tableName = this.combinedName = (this.options.joinTableName || combinedTableName) this.options.tableName = this.combinedName = (this.through === Object(this.through) ? this.through.tableName : this.through)
this.options.useHooks = options.useHooks
this.associationAccessor = this.options.as || this.combinedName this.associationAccessor = this.options.as || this.combinedName
var as = (this.options.as || Utils.pluralize(this.target.tableName, this.target.options.language)) var as = (this.options.as || Utils.pluralize(this.target.tableName, this.target.options.language))
...@@ -79,28 +87,26 @@ module.exports = (function() { ...@@ -79,28 +87,26 @@ module.exports = (function() {
combinedTableAttributes[this.identifier] = {type: sourceKeyType, primaryKey: true} combinedTableAttributes[this.identifier] = {type: sourceKeyType, primaryKey: true}
combinedTableAttributes[this.foreignIdentifier] = {type: targetKeyType, primaryKey: true} combinedTableAttributes[this.foreignIdentifier] = {type: targetKeyType, primaryKey: true}
if (this.hasJoinTableModel === true) { if (Object(this.through) === this.through) {
this.connectorDAO = this.options.joinTableModel
// remove any previously defined PKs // remove any previously defined PKs
Utils._.each(this.connectorDAO.attributes, function(dataTypeString, attributeName) { Utils._.each(this.through.attributes, function(dataTypeString, attributeName) {
if (dataTypeString.toString().indexOf('PRIMARY KEY') !== -1) { if (dataTypeString.toString().indexOf('PRIMARY KEY') !== -1) {
delete self.connectorDAO.rawAttributes[attributeName] delete self.through.rawAttributes[attributeName]
} }
}) })
this.connectorDAO.rawAttributes = Utils._.merge(this.connectorDAO.rawAttributes, combinedTableAttributes) this.through.rawAttributes = Utils._.merge(this.through.rawAttributes, combinedTableAttributes)
this.connectorDAO.init(this.connectorDAO.daoFactoryManager) this.through.init(this.through.daoFactoryManager)
} else { } else {
this.connectorDAO = this.source.daoFactoryManager.sequelize.define(this.combinedName, combinedTableAttributes, this.options) this.through = this.source.daoFactoryManager.sequelize.define(this.combinedName, combinedTableAttributes, this.options)
} }
if (!this.isSelfAssociation) { if (!this.isSelfAssociation) {
this.target.associations[this.associationAccessor].connectorDAO = this.connectorDAO this.target.associations[this.associationAccessor].through = this.through
} }
if (this.options.syncOnAssociation) { if (this.options.syncOnAssociation) {
this.connectorDAO.sync() this.through.sync()
} }
} else { } else {
var newAttributes = {} var newAttributes = {}
...@@ -109,6 +115,8 @@ module.exports = (function() { ...@@ -109,6 +115,8 @@ module.exports = (function() {
Utils._.defaults(this.target.rawAttributes, newAttributes) Utils._.defaults(this.target.rawAttributes, newAttributes)
} }
this.connectorDAO = Object(this.through) === this.through ? this.through : null; // Lets not break EVERYTHING just yet
// Sync attributes and setters/getters to DAO prototype // Sync attributes and setters/getters to DAO prototype
this.target.refreshAttributes() this.target.refreshAttributes()
this.source.refreshAttributes() this.source.refreshAttributes()
...@@ -120,7 +128,7 @@ module.exports = (function() { ...@@ -120,7 +128,7 @@ module.exports = (function() {
var self = this var self = this
obj[this.accessors.get] = function(options) { obj[this.accessors.get] = function(options) {
var Class = self.connectorDAO ? HasManyMultiLinked : HasManySingleLinked var Class = Object(self.through) === self.through ? HasManyMultiLinked : HasManySingleLinked
return new Class(self, this).injectGetter(options) return new Class(self, this).injectGetter(options)
} }
...@@ -178,7 +186,7 @@ module.exports = (function() { ...@@ -178,7 +186,7 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
instance[self.accessors.get]() instance[self.accessors.get]()
.success(function(oldAssociatedObjects) { .success(function(oldAssociatedObjects) {
var Class = self.connectorDAO ? HasManyMultiLinked : HasManySingleLinked var Class = Object(self.through) === self.through ? HasManyMultiLinked : HasManySingleLinked
new Class(self, instance).injectSetter(emitter, oldAssociatedObjects, newAssociatedObjects, defaultAttributes) new Class(self, instance).injectSetter(emitter, oldAssociatedObjects, newAssociatedObjects, defaultAttributes)
}) })
.error(function(err) { .error(function(err) {
...@@ -203,7 +211,7 @@ module.exports = (function() { ...@@ -203,7 +211,7 @@ module.exports = (function() {
.error(function(err){ emitter.emit('error', err)}) .error(function(err){ emitter.emit('error', err)})
.success(function(currentAssociatedObjects) { .success(function(currentAssociatedObjects) {
if (currentAssociatedObjects.length === 0 || self.hasJoinTableModel === true) { if (currentAssociatedObjects.length === 0 || self.hasJoinTableModel === true) {
var Class = self.connectorDAO ? HasManyMultiLinked : HasManySingleLinked var Class = Object(self.through) === self.through ? HasManyMultiLinked : HasManySingleLinked
new Class(self, instance).injectAdder(emitter, newAssociatedObject, additionalAttributes, !!currentAssociatedObjects.length) new Class(self, instance).injectAdder(emitter, newAssociatedObject, additionalAttributes, !!currentAssociatedObjects.length)
} else { } else {
emitter.emit('success', newAssociatedObject); emitter.emit('success', newAssociatedObject);
......
...@@ -388,7 +388,7 @@ module.exports = (function() { ...@@ -388,7 +388,7 @@ module.exports = (function() {
var table = include.daoFactory.tableName var table = include.daoFactory.tableName
, as = include.as , as = include.as
if (!include.association.connectorDAO) { if (!(Object(include.association.through) === include.association.through)) {
var primaryKeysLeft = ((include.association.associationType === 'BelongsTo') ? Object.keys(include.association.target.primaryKeys) : Object.keys(include.association.source.primaryKeys)) var primaryKeysLeft = ((include.association.associationType === 'BelongsTo') ? Object.keys(include.association.target.primaryKeys) : Object.keys(include.association.source.primaryKeys))
, tableLeft = ((include.association.associationType === 'BelongsTo') ? include.as : tableName) , tableLeft = ((include.association.associationType === 'BelongsTo') ? include.as : tableName)
, attrLeft = ((primaryKeysLeft.length !== 1) ? 'id' : primaryKeysLeft[0]) , attrLeft = ((primaryKeysLeft.length !== 1) ? 'id' : primaryKeysLeft[0])
...@@ -407,7 +407,7 @@ module.exports = (function() { ...@@ -407,7 +407,7 @@ module.exports = (function() {
, identTarget = include.association.foreignIdentifier , identTarget = include.association.foreignIdentifier
, attrTarget = ((!include.association.target.hasPrimaryKeys || primaryKeysTarget.length !== 1) ? 'id' : primaryKeysTarget[0]) , attrTarget = ((!include.association.target.hasPrimaryKeys || primaryKeysTarget.length !== 1) ? 'id' : primaryKeysTarget[0])
var tableJunction = include.association.connectorDAO.tableName var tableJunction = include.association.through.tableName
joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(tableJunction) + " ON " + this.quoteIdentifier(tableSource) + "." + this.quoteIdentifier(attrSource) + " = " + this.quoteIdentifier(tableJunction) + "." + this.quoteIdentifier(identSource) joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(tableJunction) + " ON " + this.quoteIdentifier(tableSource) + "." + this.quoteIdentifier(attrSource) + " = " + this.quoteIdentifier(tableJunction) + "." + this.quoteIdentifier(identSource)
joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(table) + " AS " + this.quoteIdentifier(as) + " ON " + this.quoteIdentifier(tableTarget) + "." + this.quoteIdentifier(attrTarget) + " = " + this.quoteIdentifier(tableJunction) + "." + this.quoteIdentifier(identTarget) joinQuery += " LEFT OUTER JOIN " + this.quoteIdentifier(table) + " AS " + this.quoteIdentifier(as) + " ON " + this.quoteIdentifier(tableTarget) + "." + this.quoteIdentifier(attrTarget) + " = " + this.quoteIdentifier(tableJunction) + "." + this.quoteIdentifier(identTarget)
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!