Skip to content

Instantly share code, notes, and snippets.

@mickhansen
Created December 19, 2013 20:02
Show Gist options
  • Save mickhansen/8045288 to your computer and use it in GitHub Desktop.
Save mickhansen/8045288 to your computer and use it in GitHub Desktop.
var HasMany = function(source, target, options) {
var self = this
this.associationType = 'HasMany'
this.source = source
this.target = target
this.options = options
this.sequelize = source.daoFactoryManager.sequelize
this.through = options.through
this.isSelfAssociation = (this.source.tableName === this.target.tableName)
this.doubleLinked = false
this.combinedTableName = Utils.combineTableNames(
this.source.tableName,
this.isSelfAssociation ? (this.options.as || this.target.tableName) : this.target.tableName
)
/*
* Map joinTableModel/Name to through for BC
*/
if (this.through === undefined) {
this.through = this.options.joinTableModel || this.options.joinTableName;
/*
* If both are undefined, see if useJunctionTable was false (for self associations) - else assume through to be true
*/
if (this.through === undefined) {
if (this.options.useJunctionTable === false) {
this.through = null;
} else {
this.through = true;
}
}
}
this.associationAccessor = this.options.as
if (!this.associationAccessor && (typeof this.through === "string" || Object(this.through) === this.through)) {
this.associationAccessor = this.through.tableName || this.through
}
else if (!this.associationAccessor) {
this.associationAccessor = this.combinedTableName
}
/*
* Find partner DAOFactory if present, to identify double linked association
*/
if (this.through) {
_.each(this.target.associations, function (association, accessor) {
if (self.source === association.target) {
// If through is default, we determine pairing by the accesor value (i.e. DAOFactory's using as won't match, but regular ones will)
if (self.through === true) {
if (accessor === self.associationAccessor) {
self.doubleLinked = true
association.doubleLinked = true
self.through = association.through
}
return
}
if (self.options.through === assocation.options.through) {
self.doubleLinked = true
association.doubleLinked = true
self.through = association.through
return
}
}
});
}
if (this.through === true) {
this.through = this.combinedTableName
}
if (typeof this.through === "string") {
this.through = this.sequelize.define(this.through, {}, _.extend(this.options, {
tableName: this.through
}))
}
this.options.tableName = this.combinedName = (this.through === Object(this.through) ? this.through.tableName : this.through)
var as = (this.options.as || Utils.pluralize(this.target.tableName, this.target.options.language))
this.accessors = {
get: Utils._.camelize('get_' + as),
set: Utils._.camelize('set_' + as),
add: Utils._.camelize(Utils.singularize('add_' + as, this.target.options.language)),
remove: Utils._.camelize(Utils.singularize('remove_' + as, this.target.options.language)),
hasSingle: Utils._.camelize(Utils.singularize('has_' + as, this.target.options.language)),
hasAll: Utils._.camelize('has_' + as)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment