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

Commit c1ac2e71 by Sascha Depold

a lot of crazy hacking to get belongsTo work

1 parent 87f85fdb
Showing with 18 additions and 21 deletions
......@@ -69,7 +69,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
case 'belongsTo':
// adds the foreign key to me
// e.g. table.dayId = Sequelize.INTEGER
table.attributes[assocNameAsTableIdentifier] = {type :Sequelize.INTEGER}
table.attributes[assocNameAsTableIdentifier] = {type: Sequelize.INTEGER}
break
}
})
......@@ -135,31 +135,24 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
},
hasMany: function(assocName, _table, backAssocName) {
var Factory = new require("./Factory").Factory(Sequelize, sequelize)
var association = { name: assocName, backAssociationName: backAssocName, table: _table, type: 'hasMany' }
var Factory = new require("./Factory").Factory(Sequelize, sequelize),
association = { name: assocName, backAssociationName: backAssocName, table: _table, type: 'hasMany' }
table.associations.push(association)
// don't check inside of method to increase performance
if(backAssocName) {
table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = Factory.createManyToManyGetter(table, _table, assocName, backAssocName)
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createManyToManySetter(table, _table, assocName, backAssocName)
_table.prototype[Sequelize.Helper.SQL.addPrefix('get', backAssocName)] = Factory.createManyToManyGetter(_table, table, backAssocName, assocName)
_table.prototype[Sequelize.Helper.SQL.addPrefix('set', backAssocName)] = Factory.createManyToManySetter(_table, table, backAssocName, assocName)
Factory.addManyToManyMethods(table, _table, assocName, backAssocName)
Factory.addManyToManyMethods(_table, table, backAssocName, assocName)
} else {
table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = Factory.createOneToManyGetter(table, _table, assocName)
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createOneToManySetter(table, assocName)
Factory.addOneToManyMethods(table, _table, assocName)
}
return association
},
hasOne: function(assocName, _table) {
table.associations.push({
name: assocName,
table: _table,
type: 'hasOne'
})
var association = { name: assocName, table: _table, type: 'hasOne' }
table.associations.push(association)
table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = function(callback) {
var whereConditions = {}
......@@ -188,15 +181,19 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
})
}
return table
return association
},
belongsTo: function(assocName, _table, backAssociation) {
if(typeof backAssociation == 'undefined')
throw new Error("Calling belongsTo with only two parameters is deprecated! Please take a look at the example in the repository!")
// this will overwrite the association name of the before defined hasOne or hasMany relation
backAssociation.name = assocName
// start - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys
var Factory = new require("./Factory").Factory(Sequelize, sequelize)
delete _table.prototype[Sequelize.Helper.SQL.addPrefix('get', backAssociation.name)]
delete _table.prototype[Sequelize.Helper.SQL.addPrefix('set', backAssociation.name)]
Factory.addOneToManyMethods(_table, table, assocName, backAssociation.name)
// end - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys
table.associations.push({ name: assocName, table: _table, type: 'belongsTo' })
......@@ -210,8 +207,8 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
// setter
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName, true)] = function(object, callback) {
var attr = {},
self = this
var self = this,
attr = {}
attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = object.id
......@@ -220,7 +217,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
})
}
return table
return _table
}
}
// don't put this into the hash!
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!