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

Commit 9474edb4 by Sascha Depold

added Sequelize.BOOLEAN

1 parent 3fb4bb81
...@@ -34,6 +34,14 @@ To define mappings between a class (_Stop telling me that JavaScript don't know ...@@ -34,6 +34,14 @@ To define mappings between a class (_Stop telling me that JavaScript don't know
description: Sequelize.TEXT, description: Sequelize.TEXT,
deadline: Sequelize.DATE deadline: Sequelize.DATE
}) })
Sequelize currently supports the following datatypes:
Sequelize.STRING ===> VARCHAR(255)
Sequelize.TEXT ===> TEXT
Sequelize.INTEGER ===> INT
Sequelize.DATE ===> DATETIME
Sequelize.BOOLEAN ===> TINYINT(1)
## Creation and deletion of class tables ## ## Creation and deletion of class tables ##
......
...@@ -16,6 +16,7 @@ var classMethods = { ...@@ -16,6 +16,7 @@ var classMethods = {
TEXT: 'TEXT', TEXT: 'TEXT',
INTEGER: 'INT', INTEGER: 'INT',
DATE: 'DATETIME', DATE: 'DATETIME',
BOOLEAN: 'TINYINT(1) NOT NULL',
sqlQueryFor: function(command, values) { sqlQueryFor: function(command, values) {
var query = null var query = null
......
...@@ -43,7 +43,7 @@ SequelizeHelper = { ...@@ -43,7 +43,7 @@ SequelizeHelper = {
valuesForInsertQuery: function(object) { valuesForInsertQuery: function(object) {
var actualValues = object.values, var actualValues = object.values,
result = [] result = []
SequelizeHelper.Hash.forEach(actualValues, function(value, key) { SequelizeHelper.Hash.forEach(actualValues, function(value, key) {
var dataType = object.table.attributes[key] var dataType = object.table.attributes[key]
result.push(SequelizeHelper.SQL.transformValueByDataType(value, dataType)) result.push(SequelizeHelper.SQL.transformValueByDataType(value, dataType))
...@@ -60,6 +60,9 @@ SequelizeHelper = { ...@@ -60,6 +60,9 @@ SequelizeHelper = {
if((value == null)||(typeof value == 'undefined')) if((value == null)||(typeof value == 'undefined'))
return "NULL" return "NULL"
if(dataType.indexOf(Sequelize.BOOLEAN) > -1)
return (value === true ? 1 : 0)
if(dataType.indexOf(Sequelize.INTEGER) > -1) if(dataType.indexOf(Sequelize.INTEGER) > -1)
return value return value
......
...@@ -9,8 +9,12 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -9,8 +9,12 @@ SequelizeTable = function(sequelize, tableName, attributes) {
var table = function(values) { var table = function(values) {
var self = this var self = this
SequelizeHelper.Hash.forEach(values, function(value, key) { SequelizeHelper.Hash.forEach(values, function(value, key) {
if(attributes[key]) if(attributes[key]) {
self[key] = value if(attributes[key] == Sequelize.BOOLEAN) {
self[key] = ((value == 1) || (value == true)) ? true : false
} else
self[key] = value
}
}) })
this.id = null // specify id as null to declare this object as unsaved and as not present in the database this.id = null // specify id as null to declare this object as unsaved and as not present in the database
this.table = table this.table = table
...@@ -301,7 +305,7 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -301,7 +305,7 @@ SequelizeTable = function(sequelize, tableName, attributes) {
var self = this var self = this
SequelizeHelper.Hash.keys(table.attributes).forEach(function(attribute) { SequelizeHelper.Hash.keys(table.attributes).forEach(function(attribute) {
result[attribute] = self[attribute] || null result[attribute] = (typeof self[attribute] == "undefined") ? null : self[attribute]
}) })
return result return result
...@@ -331,7 +335,7 @@ SequelizeTable = function(sequelize, tableName, attributes) { ...@@ -331,7 +335,7 @@ SequelizeTable = function(sequelize, tableName, attributes) {
updateAttributes: function(newValues, callback) { updateAttributes: function(newValues, callback) {
var self = this var self = this
SequelizeHelper.Hash.keys(table.attributes).forEach(function(attribute) { SequelizeHelper.Hash.keys(table.attributes).forEach(function(attribute) {
if(newValues[attribute]) if(typeof newValues[attribute] != 'undefined')
self[attribute] = newValues[attribute] self[attribute] = newValues[attribute]
}) })
this.save(callback) this.save(callback)
......
...@@ -301,5 +301,19 @@ module.exports = { ...@@ -301,5 +301,19 @@ module.exports = {
assert.equal(true, IsAssociatedWithTestOne.isAssociatedWith(IsAssociatedWithTestTwo, 'belongsTo')) assert.equal(true, IsAssociatedWithTestOne.isAssociatedWith(IsAssociatedWithTestTwo, 'belongsTo'))
assert.equal(false, IsAssociatedWithTestOne.isAssociatedWith(IsAssociatedWithTestTwo, 'hasMany')) assert.equal(false, IsAssociatedWithTestOne.isAssociatedWith(IsAssociatedWithTestTwo, 'hasMany'))
assert.equal(false, IsAssociatedWithTestOne.isAssociatedWith(IsAssociatedWithTestTwo, 'hasOne')) assert.equal(false, IsAssociatedWithTestOne.isAssociatedWith(IsAssociatedWithTestTwo, 'hasOne'))
},
'boolean ==> save': function(assert, beforeExit) {
var BooleanTest = s.define("BooleanTest", {flag: Sequelize.BOOLEAN})
var testIsFinished = false
BooleanTest.sync(function() {
new BooleanTest({flag: true}).save(function(obj) {
assert.equal(obj.flag, true)
obj.updateAttributes({flag: false}, function(obj2) {
assert.equal(obj2.flag, false)
testIsFinished = true
})
})
})
beforeExit(function() { assert.equal(true, testIsFinished) })
} }
} }
\ 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!