Skip to content

Database identifier generator for Node.js

License

Notifications You must be signed in to change notification settings

Darkhogg/node-dbid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dbid: Database Identifier generator

A generator of strings to use as database identifiers with at least the following properties:

  • URL safe: Generated IDs don't contain any symbols that are special or need to be escaped in URLs.
  • Compact: Generated IDs use a wide range of symbols (64 in total) so they don't waste space.
  • Monotonic: Generated IDs always increase in lexicographical order with time, so listings of elements sorted by ID are stable in time.

Usage

Install this package using npm:

$ npm install dbid

The only exported symbol is a function that generates IDs:

const dbid = require('dbid');
const id = dbid(); // example: "0MDHSlhrk5Y~UnklHOMKDG"

You can also generate IDs on the command line:

$ npx dbid
0MGKTkrIk5Y~UoG36b9yq0

Details

The generated ID created using:

  • A 48-bit millisecond timestamp,
  • A 32-bit machine identifier,
  • A 16-bit process identifier, and
  • A 32-bit random number.

All those values are put together in order, and the resulting byte string is encoded using [base64-lex][], a Base64 dialect that ensures lexicographical order is always preserved, giving a final result of a 22 character string.

Usage of a timestamp ensures that multiple IDs generated on machines with their time coordinated are always roughly increasing in order. The usage of machine and process identifiers makes multiple IDs generated at the same time be distinct from each other, while the random number at the end ensures multiple IDs generated at the same time by the same process are also distinct.

About

Database identifier generator for Node.js

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published