Merge pull request #713 from beeman/add-defaultfn-uuidv4

Add support for using UUID V4 as defaultFn
This commit is contained in:
Raymond Feng 2015-09-02 10:20:29 -07:00
commit c4de830bfe
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);