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

Commit fea413f1 by Sascha Depold

has one is working :)

1 parent 66de46ae
var Utils = require("./../utils")
, DataTypes = require('./../data-types')
var HasOne = module.exports = function(associationName, srcModel, targetModel, options) {
this.associationName = associationName
var HasOne = module.exports = function(srcModel, targetModel, options) {
this.source = srcModel
this.target = targetModel
this.options = options
......@@ -12,7 +11,7 @@ var HasOne = module.exports = function(associationName, srcModel, targetModel, o
HasOne.prototype.injectAttributes = function() {
var newAttributes = {}
this.identifier = Utils._.underscoredIf(this.source.tableName + "Id", this.options.underscored)
this.identifier = this.options.foreignKey || Utils._.underscoredIf(this.source.tableName + "Id", this.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.target.attributes, Utils.simplifyAttributes(newAttributes))
......@@ -20,9 +19,10 @@ HasOne.prototype.injectAttributes = function() {
}
HasOne.prototype.injectGetter = function(obj) {
var self = this
var self = this
, accessor = Utils._.camelize('get_' + (this.options.as || this.target.tableName))
obj[Utils._.camelize('get_' + this.associationName)] = function() {
obj[accessor] = function() {
var id = obj.id
, where = {}
......@@ -34,10 +34,11 @@ HasOne.prototype.injectGetter = function(obj) {
}
HasOne.prototype.injectSetter = function(obj) {
var self = this
var self = this
, accessor = Utils._.camelize('set_' + (this.options.as || this.target.tableName))
obj[Utils._.camelize('set_' + this.associationName)] = function(associatedObject) {
associatedObject[self.identifier] = self.id
obj[accessor] = function(associatedObject) {
associatedObject[self.identifier] = this.id
return associatedObject.save()
}
......
......@@ -5,11 +5,11 @@ var assert = require("assert")
module.exports = {
'it should correctly add the foreign id': function() {
/* var num = parseInt(Math.random() * 99999999)
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 })
User.hasOne('task', Task)
User.hasOne(Task)
assert.eql(Task.attributes['User'+num+'Id'], "INT")
},
'it should correctly add the foreign id with underscore': function() {
......@@ -17,14 +17,34 @@ module.exports = {
var User = sequelize.define('User' + num, { username: Sequelize.STRING }, {underscored: true})
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
User.hasOne('task', Task)
User.hasOne(Task)
assert.eql(Task.attributes['user'+num+'_id'], "INT")
},
'it should correctly add the foreign id when defining the foreignkey as option': function() {
var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING }, {underscored: true})
var Task = sequelize.define('Task' + num, { title: Sequelize.STRING })
User.hasOne(Task, {foreignKey: 'person_id'})
assert.eql(Task.attributes.person_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 })
var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var Task = sequelize.define('Task' + num, { title: Sequelize.STRING })
User.hasOne(Task)
var u = User.build({username: 'asd'})
assert.isDefined(u['setTask'+num])
assert.isDefined(u['getTask'+num])
},
'it should define getter and setter according to as option': function() {
var num = parseInt(Math.random() * 99999999)
var User = sequelize.define('User' + num, { username: Sequelize.STRING })
var Task = sequelize.define('Task' + num, { title: Sequelize.STRING })
User.hasOne('task', Task)
User.hasOne(Task, {as: 'Task'})
var u = User.build({username: 'asd'})
assert.isDefined(u.setTask)
......@@ -34,7 +54,7 @@ module.exports = {
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.hasOne('task', Task)
User.hasOne(Task, {as: 'Task'})
User.sync({force: true}).on('success', function() {
Task.sync({force: true}).on('success', function() {
......@@ -49,6 +69,6 @@ module.exports = {
})
})
})
})*/
})
}
}
\ 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!