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

Commit b8a548a5 by Sascha Depold

factory stuff for one to one assocs

1 parent 670ebd01
...@@ -11,7 +11,43 @@ module.exports.Factory = function(Sequelize, sequelize) { ...@@ -11,7 +11,43 @@ module.exports.Factory = function(Sequelize, sequelize) {
table1.prototype[setterName] = Factory.createOneToManySetter(table1, assocName) table1.prototype[setterName] = Factory.createOneToManySetter(table1, assocName)
table1.prototype[getterName] = Factory.createOneToManyGetter(table1, table2, assocName) table1.prototype[getterName] = Factory.createOneToManyGetter(table1, table2, assocName)
return table1 },
addOneToOneMethods: function(table1, table2, assocName, backAssocName) {
var setterName = Sequelize.Helper.SQL.addPrefix('set', backAssocName || assocName),
getterName = Sequelize.Helper.SQL.addPrefix('get', backAssocName || assocName)
// getter
table1.prototype[getterName] = function(callback) {
var whereConditions = {}
whereConditions[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = this.id
table2.find(whereConditions, callback)
}
// setter
table1.prototype[setterName] = function(object, callback) {
var self = this
this[Sequelize.Helper.SQL.addPrefix('get', backAssocName)](function(currentAssociation) {
var attr = {}
if(currentAssociation == null) {
attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = self.id
object.updateAttributes(attr, callback)
} else {
if(object.id == currentAssociation.id) callback(currentAssociation)
else {
// first update the currently associated item to have no association any more
attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = null
currentAssociation.updateAttributes(attr, function() {
// now update the object itself to set the new association
attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = self.id
object.updateAttributes(attr, callback)
})
}
}
})
}
}, },
createManyToManyGetter: function(table1, table2, assocName, backAssocName) { createManyToManyGetter: function(table1, table2, assocName, backAssocName) {
......
...@@ -151,35 +151,11 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -151,35 +151,11 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
}, },
hasOne: function(assocName, _table) { hasOne: function(assocName, _table) {
var association = { name: assocName, table: _table, type: 'hasOne' } var Factory = new require("./Factory").Factory(Sequelize, sequelize),
table.associations.push(association) association = { name: assocName, table: _table, type: 'hasOne' }
table.prototype[Sequelize.Helper.SQL.addPrefix('get', assocName)] = function(callback) {
var whereConditions = {}
whereConditions[table.identifier] = this.id
_table.find(whereConditions, callback)
}
table.prototype[Sequelize.Helper.SQL.addPrefix('set', assocName)] = function(object, callback) {
var self = this
this[Sequelize.Helper.SQL.addPrefix('get', assocName)](function(currentAssociation) { table.associations.push(association)
var attr = {} Factory.addOneToOneMethods(table, _table, assocName)
if(currentAssociation == null) {
attr[table.identifier] = self.id
object.updateAttributes(attr, callback)
} else {
if(object.id == currentAssociation.id) callback()
else {
attr[table.identifier] = null
currentAssociation.updateAttributes(attr, function() {
attr[table.identifier] = self.id
object.updateAttributes(attr, callback)
})
}
}
})
}
return association return association
}, },
...@@ -190,9 +166,15 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o ...@@ -190,9 +166,15 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
// start - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys // 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) var Factory = new require("./Factory").Factory(Sequelize, sequelize)
if(backAssociation.type == 'hasMany') {
delete _table.prototype[Sequelize.Helper.SQL.addPrefix('get', backAssociation.name)] delete _table.prototype[Sequelize.Helper.SQL.addPrefix('get', backAssociation.name)]
delete _table.prototype[Sequelize.Helper.SQL.addPrefix('set', backAssociation.name)] delete _table.prototype[Sequelize.Helper.SQL.addPrefix('set', backAssociation.name)]
Factory.addOneToManyMethods(_table, table, assocName, backAssociation.name) Factory.addOneToManyMethods(_table, table, assocName, backAssociation.name)
} else {
delete _table.prototype[Sequelize.Helper.SQL.addPrefix('get', backAssociation.name)]
delete _table.prototype[Sequelize.Helper.SQL.addPrefix('set', backAssociation.name)]
Factory.addOneToOneMethods(_table, table, assocName, backAssociation.name)
}
// end - overwrite the association of the before defined hasOne or hasMany relation, to fit the belongsTo foreign keys // 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' })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!