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

Commit ebcf8ab6 by Sascha Depold

some first steps for hasOne

1 parent 963e63f4
......@@ -16,7 +16,6 @@ BelongsTo.prototype.injectAttributes = function() {
newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes))
// this.source.associations[this.associationName] = association
return this
}
......
var Utils = require("./../utils")
, DataTypes = require('./../data-types')
var HasOne = module.exports = function(associationName, srcModel, targetModel, options) {
this.associationName = associationName
this.source = srcModel
this.target = targetModel
this.options = options
}
// the id is in the target table
HasOne.prototype.injectAttributes = function() {
var newAttributes = {}
this.identifier = Utils._.underscoredIf(this.source.tableName + "Id", this.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.target.attributes, Utils.simplifyAttributes(newAttributes))
return this
}
HasOne.prototype.injectGetter = function(obj) {
var self = this
obj[Utils._.camelize('get_' + this.associationName)] = function() {
var id = obj.id
, where = {}
where[self.identifier] = id
return self.target.find({where: where})
}
return this
}
HasOne.prototype.injectSetter = function(obj) {
var self = this
obj[Utils._.camelize('set_' + this.associationName)] = function(associatedObject) {
associatedObject[self.identifier] = self.id
return associatedObject.save()
}
return this
}
\ No newline at end of file
......@@ -6,7 +6,7 @@ var Associations = module.exports = {
hasOne: function(associationName, associatedModel) {
// the id is in the foreign table
var HasOne = require('./has-one')
var association = new HasOne(association, this, associatedModel, this.options)
var association = new HasOne(associationName, this, associatedModel, this.options)
this.associations[associationName] = association.injectAttributes()
},
belongsTo: function(associationName, associatedModel) {
......
......@@ -2,27 +2,29 @@ var assert = require("assert")
, config = require("./../config")
, Sequelize = require("./../../index")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
return module.exports = {'asd': function(){}}
module.exports = {
'it should correctly add the foreign id': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.hasOne('user', User)
assert.eql(User.attributes.taskId, "INT")
User.hasOne('task', Task)
assert.eql(Task.attributes['User'+num+'Id'], "INT")
},
'it should correctly add the foreign id with underscore': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }, {underscored: true})
var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { 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")
User.hasOne('task', Task)
assert.eql(Task.attributes['user'+num+'_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)
User.hasOne('task', Task)
var u = User.build({username: 'asd'})
assert.isDefined(u.setTask)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!