chore: replace shortid with nanoid

Signed-off-by: Mario Estrada <marioestradarosa@yahoo.com>
This commit is contained in:
Mario Estrada 2021-06-20 23:03:19 -06:00 committed by Rifa Achrinza
parent 9a58695740
commit e3448fad03
4 changed files with 38 additions and 22 deletions

View File

@ -28,7 +28,7 @@ const _extend = util._extend;
const utils = require('./utils');
const fieldsToArray = utils.fieldsToArray;
const uuid = require('uuid');
const shortid = require('shortid');
const {nanoid} = require('nanoid');
// Set up an object for quick lookup
const BASE_TYPES = {
@ -322,7 +322,8 @@ ModelBaseClass.prototype._initProperties = function(data, options) {
propVal = new Date();
break;
case 'shortid':
propVal = shortid.generate();
case 'nanoid':
propVal = nanoid(9);
break;
default:
// TODO Support user-provided functions via a registry of functions

32
package-lock.json generated
View File

@ -17,8 +17,8 @@
"lodash": "^4.17.11",
"loopback-connector": "^5.0.0",
"minimatch": "^3.0.3",
"nanoid": "^3.1.20",
"qs": "^6.5.0",
"shortid": "^2.2.6",
"strong-globalize": "^6.0.5",
"traverse": "^0.6.6",
"uuid": "^8.3.1"
@ -3399,8 +3399,15 @@
}
},
"node_modules/nanoid": {
"version": "2.1.11",
"integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA=="
"version": "3.1.25",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.25.tgz",
"integrity": "sha512-rdwtIXaXCLFAQbnfqDRnI6jaRHp9fTcYBjtFKE8eezcZ7LuLjhUaQGNeMXf1HmRoCH32CLz6XwX0TtxEOS/A3Q==",
"bin": {
"nanoid": "bin/nanoid.cjs"
},
"engines": {
"node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1"
}
},
"node_modules/natural-compare": {
"version": "1.4.0",
@ -4391,13 +4398,6 @@
"node": ">=8"
}
},
"node_modules/shortid": {
"version": "2.2.16",
"integrity": "sha512-Ugt+GIZqvGXCIItnsL+lvFJOiN7RYqlGy7QE41O3YC1xbNSeDGIRO7xg2JJXIAj1cAGnOeC1r7/T9pgrtQbv4g==",
"dependencies": {
"nanoid": "^2.1.0"
}
},
"node_modules/should": {
"version": "13.2.3",
"resolved": "https://registry.npmjs.org/should/-/should-13.2.3.tgz",
@ -7719,8 +7719,9 @@
}
},
"nanoid": {
"version": "2.1.11",
"integrity": "sha512-s/snB+WGm6uwi0WjsZdaVcuf3KJXlfGl2LcxgwkEwJF0D/BWzVWAZW/XY4bFaiR7s0Jk3FPvlnepg1H1b1UwlA=="
"version": "3.1.25",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.25.tgz",
"integrity": "sha512-rdwtIXaXCLFAQbnfqDRnI6jaRHp9fTcYBjtFKE8eezcZ7LuLjhUaQGNeMXf1HmRoCH32CLz6XwX0TtxEOS/A3Q=="
},
"natural-compare": {
"version": "1.4.0",
@ -8466,13 +8467,6 @@
"resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz",
"integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A=="
},
"shortid": {
"version": "2.2.16",
"integrity": "sha512-Ugt+GIZqvGXCIItnsL+lvFJOiN7RYqlGy7QE41O3YC1xbNSeDGIRO7xg2JJXIAj1cAGnOeC1r7/T9pgrtQbv4g==",
"requires": {
"nanoid": "^2.1.0"
}
},
"should": {
"version": "13.2.3",
"resolved": "https://registry.npmjs.org/should/-/should-13.2.3.tgz",

View File

@ -58,8 +58,8 @@
"lodash": "^4.17.11",
"loopback-connector": "^5.0.0",
"minimatch": "^3.0.3",
"nanoid": "^3.1.20",
"qs": "^6.5.0",
"shortid": "^2.2.6",
"strong-globalize": "^6.0.5",
"traverse": "^0.6.6",
"uuid": "^8.3.1"

View File

@ -2182,6 +2182,27 @@ describe('manipulation', function() {
});
});
describe('nanoid defaultFn', function() {
let ModelWithNanoId;
before(createModelWithNanoId);
it('should generate a new id when "defaultFn" is "nanoid"', function(done) {
const NANOID_REGEXP = /^[0-9a-z_\-]{7,14}$/i;
ModelWithNanoId.create(function(err, modelWithNanoId) {
if (err) return done(err);
modelWithNanoId.nanoid.should.match(NANOID_REGEXP);
done();
});
});
function createModelWithNanoId(cb) {
ModelWithNanoId = db.define('ModelWithNanoId', {
nanoid: {type: String, defaultFn: 'nanoid'},
});
db.automigrate('ModelWithNanoId', cb);
}
});
describe('shortid defaultFn', function() {
let ModelWithShortId;
before(createModelWithShortId);