From fb11c78be05eb6cecc57d6588e871dd03b2c0754 Mon Sep 17 00:00:00 2001 From: Bram Borggreve Date: Wed, 2 Sep 2015 16:39:00 +0200 Subject: [PATCH] Add support for using UUID V4 as defaultFn --- lib/model.js | 4 ++++ test/manipulation.test.js | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/model.js b/lib/model.js index efd533ef..e701f81c 100644 --- a/lib/model.js +++ b/lib/model.js @@ -280,6 +280,10 @@ ModelBaseClass.prototype._initProperties = function (data, options) { // Generate a v1 (time-based) id propVal = uuid.v1(); break; + case 'uuidv4': + // Generate a RFC4122 v4 UUID + propVal = uuid.v4(); + break; case 'now': propVal = new Date(); break; diff --git a/test/manipulation.test.js b/test/manipulation.test.js index f9032ef8..b2afdfe8 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -1070,6 +1070,26 @@ describe('manipulation', function () { }); }); + + describe('uuidv4 defaultFn', function() { + var CustomModel; + + before(function(done) { + CustomModel = db.define('CustomModel5', { + guid: { type: String, defaultFn: 'uuidv4' } + }); + db.automigrate('CustomModel5', done); + }); + + it('should generate a new id when "defaultfn" is "uuidv4"', function(done) { + var inst = CustomModel.create(function(err, m) { + should.not.exists(err); + m.guid.should.match(UUID_REGEXP); + done(); + }); + }); + }); + // it('should work when constructor called as function', function() { // var p = Person({name: 'John Resig'}); // p.should.be.an.instanceOf(Person);