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

Commit c1ac2e71 by Sascha Depold

a lot of crazy hacking to get belongsTo work

1 parent 87f85fdb
Showing with 20 additions and 23 deletions
...@@ -69,7 +69,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -69,7 +69,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
case 'belongsTo': case 'belongsTo':
// adds the foreign key to me // adds the foreign key to me
// e.g. table.dayId = Sequelize.INTEGER // e.g. table.dayId = Sequelize.INTEGER
table.attributes[assocNameAsTableIdentifier] = {type :Sequelize.INTEGER} table.attributes[assocNameAsTableIdentifier] = {type: Sequelize.INTEGER}
break break
} }
}) })
...@@ -135,31 +135,24 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -135,31 +135,24 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
}, },
hasMany: function(assocName, _table, backAssocName) { hasMany: function(assocName, _table, backAssocName) {
var Factory = new require("./Factory").Factory(Sequelize, sequelize) var Factory = new require("./Factory").Factory(Sequelize, sequelize),
var association = { name: assocName, backAssociationName: backAssocName, table: _table, type: 'hasMany' } association = { name: assocName, backAssociationName: backAssocName, table: _table, type: 'hasMany' }
table.associations.push(association) table.associations.push(association)
// don't check inside of method to increase performance
if(backAssocName) { if(backAssocName) {
table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = Factory.createManyToManyGetter(table, _table, assocName, backAssocName) Factory.addManyToManyMethods(table, _table, assocName, backAssocName)
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createManyToManySetter(table, _table, assocName, backAssocName) Factory.addManyToManyMethods(_table, table, backAssocName, assocName)
_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)
} else { } else {
table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = Factory.createOneToManyGetter(table, _table, assocName) Factory.addOneToManyMethods(table, _table, assocName)
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = Factory.createOneToManySetter(table, assocName)
} }
return association return association
}, },
hasOne: function(assocName, _table) { hasOne: function(assocName, _table) {
table.associations.push({ var association = { name: assocName, table: _table, type: 'hasOne' }
name: assocName, table.associations.push(association)
table: _table,
type: 'hasOne'
})
table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = function(callback) { table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = function(callback) {
var whereConditions = {} var whereConditions = {}
...@@ -188,15 +181,19 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -188,15 +181,19 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
}) })
} }
return table return association
}, },
belongsTo: function(assocName, _table, backAssociation) { belongsTo: function(assocName, _table, backAssociation) {
if(typeof backAssociation == 'undefined') 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!") 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 // start - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys
backAssociation.name = assocName 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' }) table.associations.push({ name: assocName, table: _table, type: 'belongsTo' })
...@@ -210,9 +207,9 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -210,9 +207,9 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
// setter // setter
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName, true)] = function(object, callback) { table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName, true)] = function(object, callback) {
var attr = {}, var self = this,
self = this attr = {}
attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = object.id attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = object.id
this.updateAttributes(attr, function() { this.updateAttributes(attr, function() {
...@@ -220,7 +217,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -220,7 +217,7 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
}) })
} }
return table return _table
} }
} }
// don't put this into the hash! // 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!