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

Commit c13ff3de by Sascha Depold

added wrapper for hasOne + belongsTo and hasMany + belongsTo

1 parent 1cf78a47
......@@ -11,14 +11,9 @@ var Pet = sequelize.define('pet', {
Person.hasMany('brothers')
Person.hasMany('sisters')
var fatherAssoc = Person.hasOne('father', Person)
var motherAssoc = Person.hasOne('mother', Person)
Person.belongsTo('father', Person, fatherAssoc)
Person.belongsTo('mother', Person, motherAssoc)
var petAssoc = Person.hasMany('pets', Pet)
Pet.belongsTo('owner', Person, petAssoc)
Person.hasOneAndBelongsTo('father', Person)
Person.hasOneAndBelongsTo('mother', Person)
Person.hasManyAndBelongsTo('pets', Pet, 'owner')
Sequelize.chainQueries([{drop: sequelize}, {sync: sequelize}], function() {
var person = new Person({ name: 'Luke' }),
......
......@@ -178,6 +178,11 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
}
},
hasManyAndBelongsTo: function(assocName, _table, backAssocName) {
var assoc = table.hasMany(assocName, _table)
_table.belongsTo(backAssocName || assocName, table, assoc)
},
hasOne: function(assocName, _table) {
var Factory = new require("./Factory").Factory(Sequelize, sequelize),
association = { name: assocName, table: _table, type: 'hasOne' }
......@@ -188,6 +193,11 @@ exports.SequelizeTable = function(Sequelize, sequelize, tableName, attributes, o
return association
},
hasOneAndBelongsTo: function(assocName, _table, backAssocName) {
var assoc = table.hasOne(assocName, _table)
_table.belongsTo(backAssocName || assocName, table, assoc)
},
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!")
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!