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

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() {
var customEventEmitter = new Utils.CustomEventEmitter(function() {
var where = {}
, connectorDAO = self.__factory.connectorDAO
, through = self.__factory.through
, options = _options || {}
, queryOptions = {}
, association = self.__factory.target.associations[self.__factory.associationAccessor]
......@@ -26,19 +26,19 @@ module.exports = (function() {
var instancePrimaryKeys = Object.keys(self.instance.daoFactory.primaryKeys)
, 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]
, foreignPrimary = Object.keys(self.__factory.target.primaryKeys)
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) {
queryOptions.hasJoinTableModel = true
queryOptions.joinTableModel = connectorDAO
queryOptions.joinTableModel = through
if (!options.attributes) {
options.attributes = [
......@@ -49,15 +49,15 @@ module.exports = (function() {
if (options.joinTableAttributes) {
options.joinTableAttributes.forEach(function (elem) {
options.attributes.push(
self.QueryInterface.quoteIdentifiers(connectorDAO.tableName + '.' + elem) + ' as ' +
self.QueryInterface.quoteIdentifier(connectorDAO.name + '.' + elem, true)
self.QueryInterface.quoteIdentifiers(through.tableName + '.' + elem) + ' as ' +
self.QueryInterface.quoteIdentifier(through.name + '.' + elem, true)
)
})
} else {
Utils._.forOwn(connectorDAO.rawAttributes, function (elem, key) {
Utils._.forOwn(through.rawAttributes, function (elem, key) {
options.attributes.push(
self.QueryInterface.quoteIdentifiers(connectorDAO.tableName + '.' + key) + ' as ' +
self.QueryInterface.quoteIdentifier(connectorDAO.name + '.' + key, true)
self.QueryInterface.quoteIdentifiers(through.tableName + '.' + key) + ' as ' +
self.QueryInterface.quoteIdentifier(through.name + '.' + key, true)
)
})
}
......@@ -81,7 +81,7 @@ module.exports = (function() {
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('error', function(err){ customEventEmitter.emit('error', err) })
.on('sql', function(sql) { customEventEmitter.emit('sql', sql)})
......@@ -120,10 +120,10 @@ module.exports = (function() {
if (!newObj) {
obsoleteAssociations.push(old)
} else if (association.hasJoinTableModel) {
} else if (Object(association.through) === association.through) {
var changedAssociation = {
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
......@@ -143,7 +143,7 @@ module.exports = (function() {
where[self.__factory.identifier] = ((sourceKeys.length === 1) ? self.instance[sourceKeys[0]] : self.instance.id)
where[foreignIdentifier] = foreignIds
chainer.add(self.__factory.connectorDAO.destroy(where, options))
chainer.add(self.__factory.through.destroy(where, options))
}
if (unassociatedObjects.length > 0) {
......@@ -154,18 +154,18 @@ module.exports = (function() {
attributes[foreignIdentifier] = ((targetKeys.length === 1) ? unassociatedObject[targetKeys[0]] : unassociatedObject.id)
if (association.hasJoinTableModel) {
attributes = Utils._.defaults(attributes, unassociatedObject[association.connectorDAO.name], defaultAttributes)
attributes = Utils._.defaults(attributes, unassociatedObject[association.through.name], defaultAttributes)
}
return attributes
})
chainer.add(self.__factory.connectorDAO.bulkCreate(bulk, options))
chainer.add(self.__factory.through.bulkCreate(bulk, options))
}
if (changedAssociations.length > 0) {
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() {
if (exists) { // implies hasJoinTableModel === true
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 {
if (association.hasJoinTableModel === true) {
attributes = Utils._.defaults(attributes, newAssociation[association.connectorDAO.name], additionalAttributes)
if (Object(association.through) === association.through) {
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) })
.error(function(err) { emitterProxy.emit('error', err) })
.on('sql', function(sql) { emitterProxy.emit('sql', sql) })
......
......@@ -11,24 +11,32 @@ module.exports = (function() {
this.source = srcDAO
this.target = targetDAO
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.hasJoinTableModel = !!this.options.joinTableModel
var combinedTableName;
if (this.hasJoinTableModel) {
combinedTableName = this.options.joinTableModel.tableName
} else if (this.options.joinTableName) {
combinedTableName = this.options.joinTableName
} else {
combinedTableName = Utils.combineTableNames(
// Map to through for BC
if (this.through === undefined) {
this.through = this.options.joinTableModel || this.options.joinTableName;
}
if (typeof this.through === "string") {
this.through = this.source.daoFactoryManager.sequelize.define(this.through, {}, this.options)
}
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.isSelfAssociation ? (this.options.as || this.target.tableName) : this.target.tableName
)
}
};
this.options.tableName = this.combinedName = (this.options.joinTableName || combinedTableName)
this.options.useHooks = options.useHooks
this.options.tableName = this.combinedName = (this.through === Object(this.through) ? this.through.tableName : this.through)
this.associationAccessor = this.options.as || this.combinedName
var as = (this.options.as || Utils.pluralize(this.target.tableName, this.target.options.language))
......@@ -79,28 +87,26 @@ module.exports = (function() {
combinedTableAttributes[this.identifier] = {type: sourceKeyType, primaryKey: true}
combinedTableAttributes[this.foreignIdentifier] = {type: targetKeyType, primaryKey: true}
if (this.hasJoinTableModel === true) {
this.connectorDAO = this.options.joinTableModel
if (Object(this.through) === this.through) {
// 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) {
delete self.connectorDAO.rawAttributes[attributeName]
delete self.through.rawAttributes[attributeName]
}
})
this.connectorDAO.rawAttributes = Utils._.merge(this.connectorDAO.rawAttributes, combinedTableAttributes)
this.connectorDAO.init(this.connectorDAO.daoFactoryManager)
this.through.rawAttributes = Utils._.merge(this.through.rawAttributes, combinedTableAttributes)
this.through.init(this.through.daoFactoryManager)
} 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) {
this.target.associations[this.associationAccessor].connectorDAO = this.connectorDAO
this.target.associations[this.associationAccessor].through = this.through
}
if (this.options.syncOnAssociation) {
this.connectorDAO.sync()
this.through.sync()
}
} else {
var newAttributes = {}
......@@ -109,6 +115,8 @@ module.exports = (function() {
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
this.target.refreshAttributes()
this.source.refreshAttributes()
......@@ -120,7 +128,7 @@ module.exports = (function() {
var self = this
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)
}
......@@ -178,7 +186,7 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function(emitter) {
instance[self.accessors.get]()
.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)
})
.error(function(err) {
......@@ -203,7 +211,7 @@ module.exports = (function() {
.error(function(err){ emitter.emit('error', err)})
.success(function(currentAssociatedObjects) {
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)
} else {
emitter.emit('success', newAssociatedObject);
......
......@@ -388,7 +388,7 @@ module.exports = (function() {
var table = include.daoFactory.tableName
, 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))
, tableLeft = ((include.association.associationType === 'BelongsTo') ? include.as : tableName)
, attrLeft = ((primaryKeysLeft.length !== 1) ? 'id' : primaryKeysLeft[0])
......@@ -407,7 +407,7 @@ module.exports = (function() {
, identTarget = include.association.foreignIdentifier
, 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(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!