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

Commit 4309d203 by Sascha Depold

unstable commit

1 parent ebcf8ab6
var Utils = require("./../utils") var Utils = require("./../utils")
, DataTypes = require('./../data-types') , DataTypes = require('./../data-types')
var BelongsTo = module.exports = function(associationName, srcModel, targetModel, options) { var BelongsTo = module.exports = function(srcModel, targetModel, options) {
this.associationName = associationName
this.source = srcModel this.source = srcModel
this.target = targetModel this.target = targetModel
this.options = options this.options = options
...@@ -10,12 +9,14 @@ var BelongsTo = module.exports = function(associationName, srcModel, targetModel ...@@ -10,12 +9,14 @@ var BelongsTo = module.exports = function(associationName, srcModel, targetModel
// the id is in the source table // the id is in the source table
BelongsTo.prototype.injectAttributes = function() { BelongsTo.prototype.injectAttributes = function() {
var newAttributes = {} var newAttributes = {}
this.identifier = Utils._.underscoredIf(this.associationName + "Id", this.options.underscored) this.identifier = this.target.identifierName
console.log(this.identifier)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
console.log(newAttributes)
Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes)) Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes))
console.log('ok')
console.log(this.source)
return this return this
} }
......
...@@ -11,8 +11,8 @@ var HasOne = module.exports = function(associationName, srcModel, targetModel, o ...@@ -11,8 +11,8 @@ var HasOne = module.exports = function(associationName, srcModel, targetModel, o
// the id is in the target table // the id is in the target table
HasOne.prototype.injectAttributes = function() { HasOne.prototype.injectAttributes = function() {
var newAttributes = {} var newAttributes = {}
this.identifier = Utils._.underscoredIf(this.source.tableName + "Id", this.options.underscored) this.identifier = Utils._.underscoredIf(this.source.tableName + "Id", this.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.target.attributes, Utils.simplifyAttributes(newAttributes)) Utils._.extend(this.target.attributes, Utils.simplifyAttributes(newAttributes))
...@@ -21,9 +21,9 @@ HasOne.prototype.injectAttributes = function() { ...@@ -21,9 +21,9 @@ HasOne.prototype.injectAttributes = function() {
HasOne.prototype.injectGetter = function(obj) { HasOne.prototype.injectGetter = function(obj) {
var self = this var self = this
obj[Utils._.camelize('get_' + this.associationName)] = function() { obj[Utils._.camelize('get_' + this.associationName)] = function() {
var id = obj.id var id = obj.id
, where = {} , where = {}
where[self.identifier] = id where[self.identifier] = id
......
var Utils = require("./../utils")
/* /*
Defines Mixin for all models. Defines Mixin for all models.
*/ */
var Associations = module.exports = { var Associations = module.exports = {
classMethods: { classMethods: {
hasOne: function(associationName, associatedModel) { hasOne: function(associatedModel, options) {
// the id is in the foreign table // the id is in the foreign table
var HasOne = require('./has-one') var HasOne = require('./has-one')
var association = new HasOne(associationName, this, associatedModel, this.options) var association = new HasOne(this, associatedModel, Utils._.extend((options||{}), this.options))
this.associations[associationName] = association.injectAttributes() this.associations[associatedModel.tableName] = association.injectAttributes()
}, },
belongsTo: function(associationName, associatedModel) { belongsTo: function(associatedModel, options) {
// the id is in this table // the id is in this table
var BelongsTo = require("./belongs-to") var BelongsTo = require("./belongs-to")
var association = new BelongsTo(associationName, this, associatedModel, this.options) var association = new BelongsTo(this, associatedModel, Utils._.extend((options||{}), this.options))
this.associations[associationName] = association.injectAttributes() this.associations[associatedModel.tableName] = association.injectAttributes()
}, },
hasMany: function(associationName, associatedModel, options) { hasMany: function(associatedModel, options) {
// the id is in the foreign table or in a connecting table // the id is in the foreign table or in a connecting table
} }
}, },
......
...@@ -36,7 +36,6 @@ ModelDefinition.prototype.addDefaultAttributes = function() { ...@@ -36,7 +36,6 @@ ModelDefinition.prototype.addDefaultAttributes = function() {
Utils._.map(defaultAttributes, function(value, attr) { self.attributes[attr] = value }) Utils._.map(defaultAttributes, function(value, attr) { self.attributes[attr] = value })
} }
ModelDefinition.prototype.query = function() { ModelDefinition.prototype.query = function() {
var args = Utils._.map(arguments, function(arg, _) { return arg }) var args = Utils._.map(arguments, function(arg, _) { return arg })
, s = this.modelManager.sequelize , s = this.modelManager.sequelize
...@@ -143,4 +142,8 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() { ...@@ -143,4 +142,8 @@ ModelDefinition.prototype.__defineGetter__('hasPrimaryKeys', function() {
return this.primaryKeyCount > 0 return this.primaryKeyCount > 0
}) })
ModelDefinition.prototype.__defineGetter__('identifierName', function() {
return Utils._.underscoredIf(this.tableName + "Id", this.options.underscored)
})
Utils._.map(require("./associations/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
...@@ -5,17 +5,19 @@ var assert = require("assert") ...@@ -5,17 +5,19 @@ var assert = require("assert")
module.exports = { module.exports = {
'it should correctly add the foreign id': function() { '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 }) var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
Task.belongsTo('user', User) Task.belongsTo(User)
assert.eql(Task.attributes.userId, "INT") assert.eql(Task.attributes['User'+num+'Id'], "INT")
}, },
'it should correctly add the foreign id with underscore': function() { 'it should correctly add the foreign id with underscore': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }) var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING })
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }, {underscored: true}) var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING }, {underscored: true})
Task.belongsTo('user', User) Task.belongsTo('user', User)
console.log(Task.attributes)
assert.eql(Task.attributes.user_id, "INT") assert.eql(Task.attributes.user_id, "INT")
}, },
'it should define getter and setter': function() { 'it should define getter and setter': function() {
...@@ -28,6 +30,15 @@ module.exports = { ...@@ -28,6 +30,15 @@ module.exports = {
assert.isDefined(t.setUser) assert.isDefined(t.setUser)
assert.isDefined(t.getUser) assert.isDefined(t.getUser)
}, },
'it should set the foreign id to null': 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 })
Task.belongsTo('user', User)
var t = Task.build({title: 'asd'})
assert.isNull(t.userId)
},
'it should set and get the correct object': function(exit) { 'it should set and get the correct object': function(exit) {
var User = sequelize.define('User' + parseInt(Math.random() * 99999999), { username: Sequelize.STRING }) 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 Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), { title: Sequelize.STRING })
......
...@@ -29,5 +29,26 @@ module.exports = { ...@@ -29,5 +29,26 @@ module.exports = {
var u = User.build({username: 'asd'}) var u = User.build({username: 'asd'})
assert.isDefined(u.setTask) assert.isDefined(u.setTask)
assert.isDefined(u.getTask) assert.isDefined(u.getTask)
},
'it should set and get the correct objects': 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.hasOne('task', Task)
User.sync({force: true}).on('success', function() {
Task.sync({force: true}).on('success', function() {
User.create({username: 'name'}).on('success', function(user) {
Task.create({title: 'snafu'}).on('success', function(task) {
user.setTask(task).on('success', function() {
user.getTask().on('success', function(task2) {
assert.eql(task.title, task2.title)
exit(function(){})
})
})
})
})
})
})
} }
} }
\ 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!