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

Commit 2ad55a62 by mulderr

add uuid data type with uuidv1 and uuidv4 as possible default values

1 parent d03d3c88
......@@ -188,6 +188,9 @@ module.exports = {
FLOAT: FLOAT,
NOW: 'NOW',
BLOB: BLOB,
UUID: 'CHAR(36)',
UUIDV1: 'UUIDV1',
UUIDV4: 'UUIDV4',
get ENUM() {
var result = function() {
......
......@@ -3,6 +3,7 @@ var util = require("util")
, SqlString = require("./sql-string")
, lodash = require("lodash")
, _string = require('underscore.string')
, uuid = require('node-uuid')
var Utils = module.exports = {
_: (function() {
......@@ -375,8 +376,14 @@ var Utils = module.exports = {
toDefaultValue: function(value) {
if (lodash.isFunction(value)) {
return value()
} else if (value === DataTypes.UUIDV1) {
return uuid.v1()
} else if (value === DataTypes.UUIDV4) {
return uuid.v4()
} else if (value === DataTypes.NOW) {
return Utils.now()
} else {
return (value === DataTypes.NOW) ? Utils.now() : value
return value
}
},
......@@ -488,7 +495,6 @@ var Utils = module.exports = {
return stack;
},
now: function(dialect) {
var now = new Date()
if(dialect != "postgres") now.setMilliseconds(0)
......
......@@ -46,7 +46,8 @@
"toposort-class": "~0.2.0",
"generic-pool": "2.0.4",
"promise": "~3.2.0",
"sql": "~0.28.0"
"sql": "~0.28.0",
"node-uuid": "~1.4.1"
},
"devDependencies": {
"sqlite3": "~2.1.12",
......
......@@ -7,6 +7,7 @@ var chai = require('chai')
, config = require(__dirname + "/config/config")
, sinon = require('sinon')
, datetime = require('chai-datetime')
, uuid = require('node-uuid')
, _ = require('lodash')
chai.use(datetime)
......@@ -16,6 +17,8 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
beforeEach(function(done) {
this.User = this.sequelize.define('User', {
username: { type: DataTypes.STRING },
uuidv1: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV1 },
uuidv4: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4 },
touchedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
aNumber: { type: DataTypes.INTEGER },
bNumber: { type: DataTypes.INTEGER },
......@@ -568,6 +571,26 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
describe('default values', function() {
describe('uuid', function() {
it('should store a string in uuidv1 and uuidv4', function(done) {
var user = this.User.build({ username: 'a user'})
expect(user.uuidv1).to.be.a('string')
expect(user.uuidv4).to.be.a('string')
done()
})
it('should store a string of length 36 in uuidv1 and uuidv4', function(done) {
var user = this.User.build({ username: 'a user'})
expect(user.uuidv1).to.have.length(36)
expect(user.uuidv4).to.have.length(36)
done()
})
it('should store a valid uuid in uuidv1 and uuidv4 that can be parsed to something of length 16', function(done) {
var user = this.User.build({ username: 'a user'})
expect(uuid.parse(user.uuidv1)).to.have.length(16)
expect(uuid.parse(user.uuidv4)).to.have.length(16)
done()
})
})
describe('current date', function() {
it('should store a date in touchedAt', function(done) {
var user = this.User.build({ username: 'a user'})
......
......@@ -25,6 +25,7 @@ describe(Support.getTestDialectTeaser('DataTypes'), function() {
[Sequelize.TEXT, 'TEXT', 'TEXT'],
[Sequelize.DATE, 'DATE', 'DATETIME'],
[Sequelize.NOW, 'NOW', 'NOW'],
[Sequelize.UUID, 'UUID', 'CHAR(36)'],
[Sequelize.BOOLEAN, 'BOOLEAN', 'TINYINT(1)'],
[Sequelize.BLOB, 'BLOB', 'BLOB'],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!