From be1d71f626e1b1f0df3e79ea58a6ae077b0151e3 Mon Sep 17 00:00:00 2001 From: Tim De Pauw Date: Tue, 20 Sep 2016 11:16:00 +0200 Subject: [PATCH 1/4] Support {defaultFn: 'shortid'} --- lib/model.js | 4 ++++ package.json | 1 + test/manipulation.test.js | 24 ++++++++++++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/model.js b/lib/model.js index 5e33c16d..44ccec16 100644 --- a/lib/model.js +++ b/lib/model.js @@ -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); diff --git a/package.json b/package.json index 9bb51eb0..bbc92ba4 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/manipulation.test.js b/test/manipulation.test.js index afd10d5a..9c1ccf02 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -12,6 +12,7 @@ var db, Person; 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 SHORTID_REGEXP = /^[0-9a-z_\-]{7,14}$/i; describe('manipulation', function() { @@ -1675,7 +1676,7 @@ describe('manipulation', function() { 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) { should.not.exists(err); m.guid.should.match(UUID_REGEXP); @@ -1694,7 +1695,7 @@ describe('manipulation', function() { 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) { should.not.exists(err); 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() { // var p = Person({name: 'John Resig'}); // p.should.be.an.instanceOf(Person); From 419a464d06efd9f00467d8c3f273c04647851d5f Mon Sep 17 00:00:00 2001 From: Tim De Pauw Date: Wed, 21 Sep 2016 12:48:02 +0200 Subject: [PATCH 2/4] Polish PR --- test/manipulation.test.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 9c1ccf02..5d788c20 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -12,7 +12,6 @@ var db, Person; 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 SHORTID_REGEXP = /^[0-9a-z_\-]{7,14}$/i; describe('manipulation', function() { @@ -1715,9 +1714,11 @@ describe('manipulation', function() { }); 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); + var SHORTID_REGEXP = /^[0-9a-z_\-]{7,14}$/i; + + CustomModel.create(function(err, customModel) { + if (err) return done(err); + customModel.shortid.should.match(SHORTID_REGEXP); done(); }); }); From 5636c15903a4dadd47541e52a735dbd88640ef94 Mon Sep 17 00:00:00 2001 From: Tim De Pauw Date: Thu, 22 Sep 2016 13:21:54 +0200 Subject: [PATCH 3/4] More descriptive name for model with shortid --- test/manipulation.test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 5d788c20..645f4c64 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -1704,21 +1704,21 @@ describe('manipulation', function() { }); describe('shortid defaultFn', function() { - var CustomModel; + var ModelWithShortid; before(function(done) { - CustomModel = db.define('CustomModel6', { + ModelWithShortid = db.define('ModelWithShortid', { shortid: {type: String, defaultFn: 'shortid'}, }); - db.automigrate('CustomModel6', done); + db.automigrate('ModelWithShortid', done); }); it('should generate a new id when "defaultFn" is "shortid"', function(done) { var SHORTID_REGEXP = /^[0-9a-z_\-]{7,14}$/i; - CustomModel.create(function(err, customModel) { + ModelWithShortid.create(function(err, modelWithShortid) { if (err) return done(err); - customModel.shortid.should.match(SHORTID_REGEXP); + modelWithShortid.shortid.should.match(SHORTID_REGEXP); done(); }); }); From 06d4b9007152c519a5afc18d6661db5b0999e754 Mon Sep 17 00:00:00 2001 From: Simon Ho Date: Thu, 22 Sep 2016 16:13:11 -0700 Subject: [PATCH 4/4] Add code review fixups --- test/manipulation.test.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 645f4c64..d0e37277 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -1675,7 +1675,7 @@ describe('manipulation', function() { 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) { should.not.exists(err); m.guid.should.match(UUID_REGEXP); @@ -1694,7 +1694,7 @@ describe('manipulation', function() { 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) { should.not.exists(err); m.guid.should.match(UUID_REGEXP); @@ -1704,24 +1704,24 @@ describe('manipulation', function() { }); describe('shortid defaultFn', function() { - var ModelWithShortid; - - before(function(done) { - ModelWithShortid = db.define('ModelWithShortid', { - shortid: {type: String, defaultFn: 'shortid'}, - }); - db.automigrate('ModelWithShortid', done); - }); + 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) { + ModelWithShortId.create(function(err, modelWithShortId) { if (err) return done(err); - modelWithShortid.shortid.should.match(SHORTID_REGEXP); + 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() {