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

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