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

Commit af7b75bb by Sascha Depold

belongs_to association

1 parent db2f882d
var Utils = require("./../utils")
, DataTypes = require('./../data-types')
var BelongsTo = module.exports = function(associationName, srcModel, targetModel, options) {
this.associationName = associationName
this.source = srcModel
this.target = targetModel
this.options = options
}
// the id is in the source table
BelongsTo.prototype.injectAttributes = function() {
var newAttributes = {}
this.identifier = this.associationName + "Id"
if(this.options.underscored) this.identifier = Utils._.camelize(this.identifier)
newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes))
// this.source.associations[this.associationName] = association
return this
}
BelongsTo.prototype.injectGetter = function(obj) {
var self = this
obj['get' + this.associationName] = function() {
return self.target.find(self.identifier)
}
return this
}
BelongsTo.prototype.injectSetter = function(obj) {
var self = this
obj['set' + this.associationName] = function(associatedObject) {
self.source[self.identifier] = associatedObject.id
return self.source.save()
}
return this
}
\ No newline at end of file
/*
Defines Mixin for all models.
*/
var Associations = module.exports = {
classMethods: {
hasOne: function(associationName, associatedModel, options) {
// the id is in the foreign table
},
hasMany: function(associationName, associatedModel, options) {
// the id is in the foreign table or in a connecting table
},
belongsTo: function(associationName, associatedModel, options) {
// the id is in this table
var BelongsTo = require("./belongs-to")
var association = new BelongsTo(associationName, this, associatedModel, this.options)
this.associations[associationName] = association.injectAttributes()
}
},
instanceMethods: {
}
}
...@@ -12,6 +12,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) { ...@@ -12,6 +12,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
this.tableName = name this.tableName = name
this.attributes = Utils.simplifyAttributes(attributes) this.attributes = Utils.simplifyAttributes(attributes)
this.modelManager = null // defined by model-manager during addModel this.modelManager = null // defined by model-manager during addModel
this.associations = {}
this.addDefaultAttributes() this.addDefaultAttributes()
} }
...@@ -108,6 +109,11 @@ ModelDefinition.prototype.build = function(values) { ...@@ -108,6 +109,11 @@ ModelDefinition.prototype.build = function(values) {
instance.addAttribute(name, null) instance.addAttribute(name, null)
} }
}) })
Utils._.each(this.associations, function(association, associationName) {
association.injectGetter(instance)
association.injectSetter(instance)
})
return instance return instance
} }
...@@ -135,4 +141,4 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() { ...@@ -135,4 +141,4 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() {
return this.primaryKeyCount > 0 return this.primaryKeyCount > 0
}) })
Utils._.map(require("./association-mixin").classMethods, function(fct, name) { ModelDefinition.prototype[name] = fct }) Utils._.map(require("./associations/mixin").classMethods, function(fct, name) { ModelDefinition.prototype[name] = fct })
\ No newline at end of file \ No newline at end of file
var Utils = require("./utils") var Utils = require("./utils")
, Mixin = require("./association-mixin") , Mixin = require("./associations/mixin")
, QueryGenerator = require("./query-generator") , QueryGenerator = require("./query-generator")
var Model = module.exports = function(values, options) { var Model = module.exports = function(values, options) {
......
var client = new (require("mysql").Client)() var client = new (require("mysql").Client)()
var Utils = module.exports = { var Utils = module.exports = {
_: require("underscore"), _: (function() {
var _ = require("underscore");
_.mixin(require('underscore.string'));
_.mixin({
camelizeIf: function(string, condition) {
var result = string
if(condition) result = _.camelize(string)
return result
},
underscoredIf: function(string, condition) {
var result = string
if(condition) result = _.underscored(string)
return result
}
})
return _
})(),
addEventEmitter: function(_class) { addEventEmitter: function(_class) {
require("sys").inherits(_class, require('events').EventEmitter) require("sys").inherits(_class, require('events').EventEmitter)
}, },
......
var assert = require("assert")
, config = require("./../config")
, Sequelize = require("./../../index")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
module.exports = {
'it should correctly add the foreign id': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.belongsTo('task', Task)
assert.eql(User.attributes.taskId, "INT")
},
'it should correctly add the foreign id with underscored': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }, {underscored: true})
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.belongsTo('task', Task)
assert.eql(User.attributes.task_id, "INT")
},
'it should define getter and setter': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.belongsTo('task', Task)
var u = User.build({username: 'asd'})
console.log(u)
assert.isDefined(u.setTask)
assert.isDefined(u.getTask)
},
'it should set and get the correct object': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.belongsTo('task', Task)
User.sync({force: true}).on('success', function() {
User.create({username: 'asd'}).on('success', function(u) {
Task.create({title: 'a task'}).on('success', function(t) {
u.setTask(t).on('success', function() {
u.getTask().on('success', function(task) {
assert.eql(task.title, 'a task')
exit()
})
})
})
})
})
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!