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

allow functions as value for 'defaultValue' property

1 parent f8b516da
...@@ -172,9 +172,8 @@ module.exports = (function() { ...@@ -172,9 +172,8 @@ module.exports = (function() {
self.DAO.prototype.booleanValues.push(name); self.DAO.prototype.booleanValues.push(name);
} }
if (definition.hasOwnProperty('defaultValue')) { if (definition.hasOwnProperty('defaultValue')) {
self.DAO.prototype.defaultValues[name] = function() { self.DAO.prototype.defaultValues[name] = Utils._.partial(
return Utils.toDefaultValue(definition.defaultValue) Utils.toDefaultValue, definition.defaultValue)
}
} }
if (definition.hasOwnProperty('validate')) { if (definition.hasOwnProperty('validate')) {
......
...@@ -529,7 +529,7 @@ module.exports = (function() { ...@@ -529,7 +529,7 @@ module.exports = (function() {
} }
// Blobs/texts cannot have a defaultValue // Blobs/texts cannot have a defaultValue
if (dataType.type !== "TEXT" && dataType.type._binary !== true && (dataType.defaultValue !== undefined) && (dataType.defaultValue != DataTypes.NOW)) { if (dataType.type !== "TEXT" && dataType.type._binary !== true && Utils.defaultValueSchemable(dataType.defaultValue)) {
template += " DEFAULT " + this.escape(dataType.defaultValue) template += " DEFAULT " + this.escape(dataType.defaultValue)
} }
......
...@@ -650,7 +650,10 @@ module.exports = (function() { ...@@ -650,7 +650,10 @@ module.exports = (function() {
template += " SERIAL" template += " SERIAL"
} }
if (dataType.defaultValue !== undefined) { if (Utils.defaultValueSchemable(dataType.defaultValue)) {
// TODO thoroughly check that DataTypes.NOW will properly
// get populated on all databases as DEFAULT value
// i.e. mysql requires: DEFAULT CURRENT_TIMESTAMP
template += " DEFAULT <%= defaultValue %>" template += " DEFAULT <%= defaultValue %>"
replacements.defaultValue = this.escape(dataType.defaultValue) replacements.defaultValue = this.escape(dataType.defaultValue)
} }
......
...@@ -313,7 +313,10 @@ module.exports = (function() { ...@@ -313,7 +313,10 @@ module.exports = (function() {
template += " NOT NULL" template += " NOT NULL"
} }
if (dataType.defaultValue !== undefined) { if (Utils.defaultValueSchemable(dataType.defaultValue)) {
// TODO thoroughly check that DataTypes.NOW will properly
// get populated on all databases as DEFAULT value
// i.e. mysql requires: DEFAULT CURRENT_TIMESTAMP
template += " DEFAULT <%= defaultValue %>" template += " DEFAULT <%= defaultValue %>"
replacements.defaultValue = this.escape(dataType.defaultValue) replacements.defaultValue = this.escape(dataType.defaultValue)
} }
......
...@@ -373,7 +373,32 @@ var Utils = module.exports = { ...@@ -373,7 +373,32 @@ var Utils = module.exports = {
}, },
toDefaultValue: function(value) { toDefaultValue: function(value) {
return (value === DataTypes.NOW) ? Utils.now() : value if (lodash.isFunction(value)) {
return value()
} else {
return (value === DataTypes.NOW) ? Utils.now() : value
}
},
/**
* Determine if the default value provided exists and can be described
* in a db schema using the DEFAULT directive.
*
* @param {*} value Any default value.
* @return {boolean} yes / no.
*/
defaultValueSchemable: function(value) {
if (typeof value === 'undefined') {return false}
// TODO this will be schemable when all supported db
// have been normalized for this case
if (value === DataTypes.NOW) {return false}
if (lodash.isFunction(value)) {
return false
}
return true
}, },
setAttributes: function(hash, identifier, instance, prefix) { setAttributes: function(hash, identifier, instance, prefix) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!