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

Commit f0eb03f2 by Sascha Depold

added allowNull, defaultValue, primaryKey for extended datatypes

1 parent ff7794d3
Showing with 27 additions and 13 deletions
...@@ -64,11 +64,13 @@ var Utils = module.exports = { ...@@ -64,11 +64,13 @@ var Utils = module.exports = {
var template = "<%= type %>" var template = "<%= type %>"
, replacements = { type: dataType.type } , replacements = { type: dataType.type }
if(dataType.default) { if(dataType.hasOwnProperty('allowNull') && (!dataType.allowNull)) template += " NOT NULL"
if(dataType.defaultValue) {
template += " DEFAULT <%= defaultValue %>" template += " DEFAULT <%= defaultValue %>"
replacements.defaultValue = Utils.escape(dataType.default) replacements.defaultValue = Utils.escape(dataType.defaultValue)
} }
if(dataType.unique) template += " UNIQUE" if(dataType.unique) template += " UNIQUE"
if(dataType.primaryKey) template += " PRIMARY KEY"
result[name] = Utils._.template(template)(replacements) result[name] = Utils._.template(template)(replacements)
} else { } else {
......
var assert = require("assert") var assert = require("assert")
, Sequelize = require("./../../index") , Sequelize = require("./../../index")
, sequelize = new Sequelize('database', 'username', 'password')
module.exports = { module.exports = {
'it should add a new model to the model-manager': function() { 'it should add a new model to the model-manager': function() {
...@@ -8,18 +9,28 @@ module.exports = { ...@@ -8,18 +9,28 @@ module.exports = {
s.define('foo', { title: Sequelize.STRING }) s.define('foo', { title: Sequelize.STRING })
assert.eql(s.modelManager.all.length, 1) assert.eql(s.modelManager.all.length, 1)
}, },
'it should handle extended attributes': function() { 'it should handle extended attributes correctly - unique': function() {
var s = new Sequelize('database', 'username', 'password') var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {
var User = s.define('User', { username: {type: Sequelize.STRING, unique: true}
name: Sequelize.STRING, })
username: {type: Sequelize.STRING, unique: true}, assert.eql(User.attributes, {username:"VARCHAR(255) UNIQUE",id:"INT NOT NULL auto_increment PRIMARY KEY"})
password: {type: Sequelize.STRING, default: 'password'} },
'it should handle extended attributes correctly - default': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {
username: {type: Sequelize.STRING, defaultValue: 'foo'}
}) })
assert.eql(User.attributes, { assert.eql(User.attributes, {username:"VARCHAR(255) DEFAULT 'foo'",id:"INT NOT NULL auto_increment PRIMARY KEY"})
name:"VARCHAR(255)", },
username:"VARCHAR(255) UNIQUE", 'it should handle extended attributes correctly - null': function() {
password:"VARCHAR(255) DEFAULT 'password'", var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {
id:"INT NOT NULL auto_increment PRIMARY KEY" username: {type: Sequelize.STRING, allowNull: false}
})
assert.eql(User.attributes, {username:"VARCHAR(255) NOT NULL",id:"INT NOT NULL auto_increment PRIMARY KEY"})
},
'it should handle extended attributes correctly - primary key': function() {
var User = sequelize.define('User' + parseInt(Math.random() * 999999999), {
username: {type: Sequelize.STRING, primaryKey: true}
}) })
assert.eql(User.attributes, {username:"VARCHAR(255) PRIMARY KEY",id:"INT NOT NULL auto_increment PRIMARY KEY"})
} }
} }
\ 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!