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

Commit d4d2f7be by Jan Aagaard Meier

Merge pull request #975 from thanpolas/defaultValue-allow-functions

Allow functions to be defined as values for "defaultValue"
2 parents 85fdbebe 068e6926
...@@ -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')) {
......
...@@ -357,7 +357,7 @@ module.exports = (function() { ...@@ -357,7 +357,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)
} }
......
...@@ -499,7 +499,10 @@ module.exports = (function() { ...@@ -499,7 +499,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)
} }
......
...@@ -256,7 +256,10 @@ module.exports = (function() { ...@@ -256,7 +256,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) {
......
/* jshint camelcase: false */ /* jshint camelcase: false */
/* jshint expr: true */
var chai = require('chai') var chai = require('chai')
, Sequelize = require('../index') , Sequelize = require('../index')
, expect = chai.expect , expect = chai.expect
...@@ -158,6 +159,27 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -158,6 +159,27 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it('should allow me to set a function as default value', function(done) {
var defaultFunction = sinon.stub().returns(5)
var UserTable = this.sequelize.define('UserCol', {
aNumber: {
type: Sequelize.INTEGER,
defaultValue: defaultFunction
}
}, { timestamps: true })
UserTable.sync({ force: true }).success(function() {
UserTable.create().success(function(user) {
UserTable.create().success(function(user2) {
expect(user.aNumber).to.equal(5)
expect(user2.aNumber).to.equal(5)
expect(defaultFunction.callCount).to.equal(2)
done()
})
})
})
})
it('should allow me to override updatedAt, createdAt, and deletedAt fields', function(done) { it('should allow me to override updatedAt, createdAt, and deletedAt fields', function(done) {
var UserTable = this.sequelize.define('UserCol', { var UserTable = this.sequelize.define('UserCol', {
aNumber: Sequelize.INTEGER aNumber: Sequelize.INTEGER
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!