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);