Skip to content

Commit 24f000d

Browse files
authored
Merge pull request #1107 from strongloop/1101
Takeover: 1101
2 parents 8b835b1 + 06d4b90 commit 24f000d

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

lib/model.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var _extend = util._extend;
2626
var utils = require('./utils');
2727
var fieldsToArray = utils.fieldsToArray;
2828
var uuid = require('node-uuid');
29+
var shortid = require('shortid');
2930

3031
// Set up an object for quick lookup
3132
var BASE_TYPES = {
@@ -308,6 +309,9 @@ ModelBaseClass.prototype._initProperties = function(data, options) {
308309
case 'now':
309310
propVal = new Date();
310311
break;
312+
case 'shortid':
313+
propVal = shortid.generate();
314+
break;
311315
default:
312316
// TODO Support user-provided functions via a registry of functions
313317
g.warn('Unknown default value provider %s', defn);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"minimatch": "^3.0.3",
4949
"node-uuid": "^1.4.2",
5050
"qs": "^3.1.0",
51+
"shortid": "^2.2.6",
5152
"strong-globalize": "^2.6.2",
5253
"traverse": "^0.6.6"
5354
},

test/manipulation.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,27 @@ describe('manipulation', function() {
17031703
});
17041704
});
17051705

1706+
describe('shortid defaultFn', function() {
1707+
var ModelWithShortId;
1708+
before(createModelWithShortId);
1709+
1710+
it('should generate a new id when "defaultFn" is "shortid"', function(done) {
1711+
var SHORTID_REGEXP = /^[0-9a-z_\-]{7,14}$/i;
1712+
ModelWithShortId.create(function(err, modelWithShortId) {
1713+
if (err) return done(err);
1714+
modelWithShortId.shortid.should.match(SHORTID_REGEXP);
1715+
done();
1716+
});
1717+
});
1718+
1719+
function createModelWithShortId(cb) {
1720+
ModelWithShortId = db.define('ModelWithShortId', {
1721+
shortid: {type: String, defaultFn: 'shortid'},
1722+
});
1723+
db.automigrate('ModelWithShortId', cb);
1724+
}
1725+
});
1726+
17061727
// it('should work when constructor called as function', function() {
17071728
// var p = Person({name: 'John Resig'});
17081729
// p.should.be.an.instanceOf(Person);

0 commit comments

Comments
 (0)