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

Commit 22635d60 by Sascha Depold

default values for freshly builded objects

1 parent ebd36605
......@@ -11,6 +11,7 @@ var ModelDefinition = module.exports = function(name, attributes, options) {
this.name = name
this.tableName = this.options.freezeTableName ? name : Utils.pluralize(name)
this.attributes = Utils.simplifyAttributes(attributes)
this.rawAttributes = attributes
this.modelManager = null // defined by model-manager during addModel
this.associations = {}
......@@ -106,8 +107,13 @@ ModelDefinition.prototype.build = function(values) {
Utils._.map(this.attributes, function(definition, name) {
if(typeof instance[name] == 'undefined') {
instance[name] = null
instance.addAttribute(name, null)
var value = null
if(self.rawAttributes.hasOwnProperty(name))
value = self.rawAttributes[name].defaultValue || null
instance[name] = value
instance.addAttribute(name, value)
}
})
......
......@@ -22,5 +22,17 @@ module.exports = {
exit(function(){})
})
})
},
'build should fill the object with default values': function() {
var Task = sequelize.define('Task' + parseInt(Math.random() * 99999999), {
title: {dataType: Sequelize.STRING, defaultValue: 'a task!'},
foo: {dataType: Sequelize.INTEGER, defaultValue: 2},
bar: {dataType: Sequelize.DATE},
foobar: {dataType: Sequelize.TEXT, defaultValue: 'asd'}
})
assert.eql(Task.build().title, 'a task!')
assert.eql(Task.build().foo, 2)
assert.eql(Task.build().bar, null)
assert.eql(Task.build().foobar, 'asd')
}
}
\ 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!