mirror of
https://github.com/knex/knex.git
synced 2025-12-29 16:08:45 +00:00
Replace uuid with copied nanoid logic (#4089)
This commit is contained in:
parent
6001e55382
commit
26f3e5d62d
@ -15,7 +15,7 @@ const isEmpty = require('lodash/isEmpty');
|
||||
const map = require('lodash/map');
|
||||
const omitBy = require('lodash/omitBy');
|
||||
const reduce = require('lodash/reduce');
|
||||
const uuid = require('uuid');
|
||||
const { nanoid } = require('../util/nanoid');
|
||||
const { isString, isUndefined } = require('../util/is');
|
||||
|
||||
const debugBindings = debug('knex:bindings');
|
||||
@ -66,7 +66,7 @@ class QueryCompiler {
|
||||
timeout: this.timeout,
|
||||
cancelOnTimeout: this.cancelOnTimeout,
|
||||
bindings: this.formatter.bindings || [],
|
||||
__knexQueryUid: uuid.v1(),
|
||||
__knexQueryUid: nanoid(),
|
||||
};
|
||||
|
||||
Object.defineProperties(query, {
|
||||
|
||||
@ -9,7 +9,7 @@ const assign = require('lodash/assign');
|
||||
const isPlainObject = require('lodash/isPlainObject');
|
||||
const reduce = require('lodash/reduce');
|
||||
const saveAsyncStack = require('./util/save-async-stack');
|
||||
const uuid = require('uuid');
|
||||
const { nanoid } = require('./util/nanoid');
|
||||
const { isNumber, isObject } = require('./util/is');
|
||||
|
||||
const debugBindings = debug('knex:bindings');
|
||||
@ -109,7 +109,7 @@ assign(Raw.prototype, {
|
||||
);
|
||||
}
|
||||
|
||||
obj.__knexQueryUid = uuid.v1();
|
||||
obj.__knexQueryUid = nanoid();
|
||||
|
||||
return obj;
|
||||
},
|
||||
|
||||
29
lib/util/nanoid.js
Normal file
29
lib/util/nanoid.js
Normal file
@ -0,0 +1,29 @@
|
||||
// This alphabet uses `A-Za-z0-9_-` symbols. The genetic algorithm helped
|
||||
// optimize the gzip compression for this alphabet.
|
||||
const urlAlphabet =
|
||||
'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW';
|
||||
|
||||
/**
|
||||
* Generate URL-friendly unique ID. This method uses the non-secure
|
||||
* predictable random generator with bigger collision probability.
|
||||
* Based on https://github.com/ai/nanoid
|
||||
*
|
||||
* ```js
|
||||
* model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"
|
||||
* ```
|
||||
*
|
||||
* @param size Size of the ID. The default size is 21.
|
||||
* @returns A random string.
|
||||
*/
|
||||
function nanoid(size = 21) {
|
||||
let id = '';
|
||||
// A compact alternative for `for (var i = 0; i < step; i++)`.
|
||||
let i = size;
|
||||
while (i--) {
|
||||
// `| 0` is more compact and faster than `Math.floor()`.
|
||||
id += urlAlphabet[(Math.random() * 64) | 0];
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
module.exports = { nanoid };
|
||||
@ -43,7 +43,6 @@
|
||||
"pg-connection-string": "2.3.0",
|
||||
"tarn": "^3.0.1",
|
||||
"tildify": "2.0.0",
|
||||
"uuid": "^7.0.3",
|
||||
"v8flags": "^3.2.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
|
||||
const { expect } = require('chai');
|
||||
|
||||
const uuid = require('uuid');
|
||||
const _ = require('lodash');
|
||||
const sinon = require('sinon');
|
||||
|
||||
@ -754,7 +753,7 @@ module.exports = function (knex) {
|
||||
return knex('datatype_test')
|
||||
.insert({
|
||||
enum_value: 'c',
|
||||
uuid: uuid.v4(),
|
||||
uuid: 'c39d8fcf-68a0-4902-b192-1ebb6310d9ad',
|
||||
})
|
||||
.then(function () {
|
||||
return knex('datatype_test').insert({
|
||||
@ -780,8 +779,14 @@ module.exports = function (knex) {
|
||||
});
|
||||
|
||||
it('should not mutate the array passed in', function () {
|
||||
const a = { enum_value: 'a', uuid: uuid.v4() };
|
||||
const b = { enum_value: 'c', uuid: uuid.v4() };
|
||||
const a = {
|
||||
enum_value: 'a',
|
||||
uuid: '00419fc1-7eed-442c-9c01-cf757e74b8f0',
|
||||
};
|
||||
const b = {
|
||||
enum_value: 'c',
|
||||
uuid: '13ac5acd-c5d7-41a0-8db0-dacf64d0e4e2',
|
||||
};
|
||||
const x = [a, b];
|
||||
|
||||
return knex('datatype_test')
|
||||
|
||||
13
test/unit/util/nanoid.js
Normal file
13
test/unit/util/nanoid.js
Normal file
@ -0,0 +1,13 @@
|
||||
const { nanoid } = require('../../../lib/util/nanoid');
|
||||
const { expect } = require('chai');
|
||||
|
||||
describe('nanoid', () => {
|
||||
it('generates id', () => {
|
||||
const uuid = nanoid();
|
||||
const uuid2 = nanoid();
|
||||
|
||||
expect(uuid.length).to.equal(21);
|
||||
expect(uuid2.length).to.equal(21);
|
||||
expect(uuid).not.to.equal(uuid2);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user