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

Commit 200802ba by Sascha Depold

Merge branch 'master' into milestones/2.0.0

Conflicts:
	package.json
2 parents 518baccc eda3d59e
# Sequelize [![Build Status](https://secure.travis-ci.org/sequelize/sequelize.png)](http://travis-ci.org/sequelize/sequelize) [![Dependency Status](https://david-dm.org/sequelize/sequelize.png)](https://david-dm.org/sequelize/sequelize) [![Dependency Status](https://david-dm.org/sequelize/sequelize.png)](https://david-dm.org/sequelize/sequelize) [![Flattr this](http://api.flattr.com/button/flattr-badge-large.png)](http://flattr.com/thing/1259407/Sequelize) # # Sequelize [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/sequelize/sequelize/trend.png)](https://bitdeli.com/free "Bitdeli Badge") [![Build Status](https://secure.travis-ci.org/sequelize/sequelize.png)](http://travis-ci.org/sequelize/sequelize) [![Dependency Status](https://david-dm.org/sequelize/sequelize.png)](https://david-dm.org/sequelize/sequelize) [![Flattr this](http://api.flattr.com/button/flattr-badge-large.png)](http://flattr.com/thing/1259407/Sequelize) #
MySQL, MariaDB, PostgresSQL, and SQLite Object Relational Mapper (ORM) for [node](http://nodejs.org). MySQL, MariaDB, PostgresSQL, and SQLite Object Relational Mapper (ORM) for [node](http://nodejs.org).
......
...@@ -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')) {
......
...@@ -407,7 +407,7 @@ module.exports = (function() { ...@@ -407,7 +407,7 @@ module.exports = (function() {
} }
DAO.prototype.addAttribute = function(attribute, value) { DAO.prototype.addAttribute = function(attribute, value) {
if (this.booleanValues.length && this.booleanValues.indexOf(attribute) !== -1 && value !== undefined) { // transform integer 0,1 into boolean if (this.booleanValues.length && this.booleanValues.indexOf(attribute) !== -1 && value != null) { // transform integer 0,1 into boolean
value = !!value value = !!value
} }
......
...@@ -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)
} }
......
...@@ -49,7 +49,11 @@ module.exports = (function() { ...@@ -49,7 +49,11 @@ module.exports = (function() {
function(fct) { function(fct) {
fct = bindToProcess(fct); fct = bindToProcess(fct);
this.on('error', function(err) { fct(err, null) }) this.on('error', function(err) { fct(err, null) })
.on('success', function(arg0, arg1) { fct(null, arg0, arg1) }) .on('success', function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(null);
fct.apply(fct, args);
})
return this return this
} }
......
...@@ -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) {
......
{ {
"name": "sequelize", "name": "sequelize",
"description": "Multi dialect ORM for Node.JS", "description": "Multi dialect ORM for Node.JS",
"version": "2.0.0-beta.0", "version": "2.0.0-beta.1",
"author": "Sascha Depold <sascha@depold.com>", "author": "Sascha Depold <sascha@depold.com>",
"contributors": [ "contributors": [
{ {
......
/* 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
......
...@@ -1114,6 +1114,25 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -1114,6 +1114,25 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
}) })
}) })
}) })
it("returns null for null, undefined, and unset boolean values", function(done) {
var Setting = this.sequelize.define('SettingHelper', {
setting_key: DataTypes.STRING,
bool_value: { type: DataTypes.BOOLEAN, allowNull: true },
bool_value2: { type: DataTypes.BOOLEAN, allowNull: true },
bool_value3: { type: DataTypes.BOOLEAN, allowNull: true }
}, { timestamps: false, logging: false })
Setting.sync({ force: true }).success(function() {
Setting.create({ setting_key: 'test', bool_value: null, bool_value2: undefined }).success(function() {
Setting.find({ where: { setting_key: 'test' } }).success(function(setting) {
expect(setting.bool_value).to.equal(null)
expect(setting.bool_value2).to.equal(null)
expect(setting.bool_value3).to.equal(null)
done()
})
})
})
})
}) })
describe('equals', function() { describe('equals', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!