Merge pull request #1107 from strongloop/1101

Takeover: 1101
This commit is contained in:
Simon Ho 2016-09-22 21:00:01 -07:00 committed by GitHub
commit 24f000d978
3 changed files with 26 additions and 0 deletions

View File

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

View File

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

View File

@ -1703,6 +1703,27 @@ describe('manipulation', function() {
});
});
describe('shortid defaultFn', function() {
var ModelWithShortId;
before(createModelWithShortId);
it('should generate a new id when "defaultFn" is "shortid"', function(done) {
var SHORTID_REGEXP = /^[0-9a-z_\-]{7,14}$/i;
ModelWithShortId.create(function(err, modelWithShortId) {
if (err) return done(err);
modelWithShortId.shortid.should.match(SHORTID_REGEXP);
done();
});
});
function createModelWithShortId(cb) {
ModelWithShortId = db.define('ModelWithShortId', {
shortid: {type: String, defaultFn: 'shortid'},
});
db.automigrate('ModelWithShortId', cb);
}
});
// it('should work when constructor called as function', function() {
// var p = Person({name: 'John Resig'});
// p.should.be.an.instanceOf(Person);