Support {defaultFn: 'shortid'}

This commit is contained in:
Tim De Pauw 2016-09-20 11:16:00 +02:00 committed by Simon Ho
parent 8b835b1b5c
commit be1d71f626
3 changed files with 27 additions and 2 deletions

View File

@ -26,6 +26,7 @@ var _extend = util._extend;
var utils = require('./utils'); var utils = require('./utils');
var fieldsToArray = utils.fieldsToArray; var fieldsToArray = utils.fieldsToArray;
var uuid = require('node-uuid'); var uuid = require('node-uuid');
var shortid = require('shortid');
// Set up an object for quick lookup // Set up an object for quick lookup
var BASE_TYPES = { var BASE_TYPES = {
@ -308,6 +309,9 @@ ModelBaseClass.prototype._initProperties = function(data, options) {
case 'now': case 'now':
propVal = new Date(); propVal = new Date();
break; break;
case 'shortid':
propVal = shortid.generate();
break;
default: default:
// TODO Support user-provided functions via a registry of functions // TODO Support user-provided functions via a registry of functions
g.warn('Unknown default value provider %s', defn); g.warn('Unknown default value provider %s', defn);

View File

@ -48,6 +48,7 @@
"minimatch": "^3.0.3", "minimatch": "^3.0.3",
"node-uuid": "^1.4.2", "node-uuid": "^1.4.2",
"qs": "^3.1.0", "qs": "^3.1.0",
"shortid": "^2.2.6",
"strong-globalize": "^2.6.2", "strong-globalize": "^2.6.2",
"traverse": "^0.6.6" "traverse": "^0.6.6"
}, },

View File

@ -12,6 +12,7 @@ var db, Person;
var ValidationError = require('..').ValidationError; var ValidationError = require('..').ValidationError;
var UUID_REGEXP = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; var UUID_REGEXP = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
var SHORTID_REGEXP = /^[0-9a-z_\-]{7,14}$/i;
describe('manipulation', function() { describe('manipulation', function() {
@ -1675,7 +1676,7 @@ describe('manipulation', function() {
db.automigrate('CustomModel5', done); db.automigrate('CustomModel5', done);
}); });
it('should generate a new id when "defaultfn" is "uuid"', function(done) { it('should generate a new id when "defaultFn" is "uuid"', function(done) {
var inst = CustomModel.create(function(err, m) { var inst = CustomModel.create(function(err, m) {
should.not.exists(err); should.not.exists(err);
m.guid.should.match(UUID_REGEXP); m.guid.should.match(UUID_REGEXP);
@ -1694,7 +1695,7 @@ describe('manipulation', function() {
db.automigrate('CustomModel5', done); db.automigrate('CustomModel5', done);
}); });
it('should generate a new id when "defaultfn" is "uuidv4"', function(done) { it('should generate a new id when "defaultFn" is "uuidv4"', function(done) {
var inst = CustomModel.create(function(err, m) { var inst = CustomModel.create(function(err, m) {
should.not.exists(err); should.not.exists(err);
m.guid.should.match(UUID_REGEXP); m.guid.should.match(UUID_REGEXP);
@ -1703,6 +1704,25 @@ describe('manipulation', function() {
}); });
}); });
describe('shortid defaultFn', function() {
var CustomModel;
before(function(done) {
CustomModel = db.define('CustomModel6', {
shortid: {type: String, defaultFn: 'shortid'},
});
db.automigrate('CustomModel6', done);
});
it('should generate a new id when "defaultFn" is "shortid"', function(done) {
var inst = CustomModel.create(function(err, m) {
should.not.exists(err);
m.shortid.should.match(SHORTID_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);