Add support for using UUID V4 as defaultFn

This commit is contained in:
Bram Borggreve 2015-09-02 16:39:00 +02:00
parent febfe2362a
commit fb11c78be0
2 changed files with 24 additions and 0 deletions

View File

@ -280,6 +280,10 @@ ModelBaseClass.prototype._initProperties = function (data, options) {
// Generate a v1 (time-based) id // Generate a v1 (time-based) id
propVal = uuid.v1(); propVal = uuid.v1();
break; break;
case 'uuidv4':
// Generate a RFC4122 v4 UUID
propVal = uuid.v4();
break;
case 'now': case 'now':
propVal = new Date(); propVal = new Date();
break; break;

View File

@ -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() { // it('should work when constructor called as function', function() {
// var p = Person({name: 'John Resig'}); // var p = Person({name: 'John Resig'});
// p.should.be.an.instanceOf(Person); // p.should.be.an.instanceOf(Person);