Reputation: 519
How to generate unique numeric short ID in nodejs?
I need to give each of my transaction one of these numeric short id
There can be millions of transactions
Upvotes: 5
Views: 24867
Reputation: 4406
You can either use https://www.npmjs.com/package/uuid or https://www.npmjs.com/package/nanoid. I personally prefer the second one.
Generating unique numbers is a much more complicated subject than most people think. Using timestamps, for example, might "work" in some cases, but create a security concern. They are too easy to guess. Please, for your own sake, take a look at the readme of the two libraries I mentioned before jumping to to simple "solution".
Upvotes: 1
Reputation: 7103
just install this packagge like
npm i nodejs-unique-numeric-id-generator
# yarn
yarn add nodejs-unique-numeric-id-generator
and add this in your node environmental
const ID = require("nodejs-unique-numeric-id-generator")
ID.generate(new Date().toJSON()); // output: "118626"
ID.generate(new Date().toJSON()); // output: "096984"
Upvotes: 1
Reputation: 99
You can try this package: https://npmjs.com/package/nodejs-unique-numeric-id-generator It's a time-based Numeric ID generator and thus will always give a new id.
var nodejsUniqueNumericIdGenerator = require("nodejs-unique-numeric-id-generator")
nodejsUniqueNumericIdGenerator.generate(new Date().toJSON());
Upvotes: 1
Reputation: 755
new Date().valueOf()
will give you simple number thats unique (unless you call it twice in the same millisecond).
Upvotes: 5
Reputation: 31
Try following function -
function generate(n) {
var add = 1,
max = 12 - add;
if (n > max) {
return generate(max) + generate(n - max);
}
max = Math.pow(10, n + add);
var min = max / 10; // Math.pow(10, n) basically
var number = Math.floor(Math.random() * (max - min + 1)) + min;
return ("" + number).substring(add);
}
console.log(`id with 6 digits - ${generate(6)}`);
console.log(`id with 7 digits - ${generate(7)}`);
console.log(`id with 8 digits - ${generate(8)}`);
Upvotes: 3
Reputation: 353
I have written an npm package named custom-id to solve that problem. You can tailor your custom-id as you like. If you want it numeric, you can do this. It can generate alphanumeric value too. You are concerned about the readability. That's why the plugin is written. It is built for both human and machine. We use familiar letters (from given name & email) in our randomly generated ID. People can easily recognize those familiar letters as they use it every time, every day, every moment. You can randomly generate IDs with uuid or nanoid, but these are not generated for human. These are for the machine. But this library is for both of them.
You can download this package from here - custom-id
npm i custom-id
You can generate an ID instantly by giving an empty object as the argument.
var customId = require("custom-id");
customId({}); // Voila... A random 8 character string will be generated automatically
The custom ID will be generated in this format -
✌✌** 2 Number + 2 Letter + 2 Number + 2 Letter = 8 characters ** ✌✌
All those number and letter will be generated randomly. We use cryptography to generate ids (if available).
You can read about the detailed documentation from here - NPM
But in your case, you just want a number, which is human readable, intuitive & unique. The code will be like -
var customId = require("custom-id")
customId({
name: "123456",
email: "78910"
});
// Random Result - "20111070" or "11435189" or "64656618"
You should look into that number. This is just a number but easy on the eye and easy to remember. This random id generator will not create a number like - 24359625! Thanks to its unique but very simple algorithm. That's why I suggest you use it.
Upvotes: 6
Reputation: 172
you can use shortid package. install package:
npm -i shortid
then you can use in code like this
var shortid = require("shortid");
id = shortid.generate();
Upvotes: 1
Reputation: 10940
Best to go with alphanumeric id. They are still readable, can be quite short and you are generally ensured of uniqueness across multiple tables. My recommendation would be the shortid node library.
https://www.npmjs.com/package/shortid
From their docs:
ShortId creates amazingly short non-sequential url-friendly unique ids. Perfect for url shorteners, MongoDB and Redis ids, and any other id users might see.
By default 7-14 url-friendly characters: A-Z, a-z, 0-9, _- Supports cluster (automatically), custom seeds, custom alphabet. Can generate any number of ids without duplicates, even millions per day. Perfect for games, especially if you are concerned about cheating so you don't want an easily guessable id. Apps can be restarted any number of times without any chance of repeating an id. Popular replacement for Mongo ID/Mongoose ID. Works in Node, io.js, and web browsers.
Upvotes: 1