Skip to content

code4fukui/ulid

 
 

Repository files navigation

日本語のREADMEはこちらです: README.ja.md



ulid


Build Status codecov npm

Universally Unique Lexicographically Sortable Identifier

This project is an implementation of the ULID specification.

UUID can be suboptimal for many use-cases because it isn't efficiently sortable. ULID offers a compelling alternative:

  • 128-bit compatibility with UUID
  • 1.21e+24 unique ULIDs per millisecond
  • Lexicographically sortable for efficient database indexing
  • Canonically encoded as a 26-character string, as opposed to the 36-character UUID
  • URL-safe with no special characters
  • Case-insensitive and uses Crockford's Base32 for readability and efficiency
  • Monotonic sort order to ensure ordering even for ULIDs generated in the same millisecond

Install

Node.js

npm install --save ulid

Deno / ES Modules via CDN

import { ulid } from "https://unpkg.com/ulid@2.3.0/dist/index.esm.js";

console.log(ulid());

Browser via <script> tag

<!-- Get the latest version from https://unpkg.com/ulid/dist/ -->
<script src="https://unpkg.com/ulid@2.3.0/dist/index.umd.js"></script>
<script>
  // The global variable is `ULID`.
  console.log(ULID.ulid());
</script>

Usage

Basic Generation

Import and call ulid() to generate a new identifier.

ES Modules

import { ulid } from 'ulid'
ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAV

CommonJS

const { ulid } = require('ulid')
ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAV

Seed Time

You can generate a ULID for a specific millisecond timestamp by passing it as an argument. This is useful for migrating data or for generating ULIDs that correspond to a past event.

import { ulid } from 'ulid'
ulid(1469918176385) // 01ARYZ6S41TSV4RRFFQ69G5FAV

Monotonic ULIDs

To guarantee that ULIDs generated within the same millisecond are strictly ordered, use the monotonicFactory. This is useful in high-frequency scenarios where multiple ULIDs could be created in the same millisecond.

The factory returns a ulid function that will increment the random part of the ULID if it's called in the same millisecond as the previous call.

import { monotonicFactory } from 'ulid'

const ulid = monotonicFactory()

// The following ULIDs are generated in the same millisecond
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVR8
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVR9
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVRA

// Even if a lower timestamp is passed, it will preserve sort order by
// incrementing the previous ULID.
ulid(100000) // 000XAL6S41ACTAV9WEVGEMMVRB

Advanced Usage

Using a Custom Pseudo-Random Number Generator (PRNG)

By default, ulid uses a cryptographically-secure PRNG (crypto.getRandomValues in browsers, crypto.randomBytes in Node.js). You can provide your own PRNG by using the factory function. Your PRNG function must return a random number between 0 and 1.

import { factory } from 'ulid'
import prng from 'your-random-number-generator'

const ulid = factory(prng)

ulid() // 01BXAVRG61YJ5YSBRM51702F6M

The monotonicFactory also accepts a PRNG as an argument.

import { monotonicFactory } from 'ulid'
import prng from 'your-random-number-generator'

const ulid = monotonicFactory(prng)

ulid() // 01BXAVRG61YJ5YSBRM51702F6M

Allowing Insecure Math.random

In environments without a crypto module, ulid will throw an error. If security is not a concern, you can explicitly allow Math.random as a fallback.

import { factory, detectPrng } from 'ulid'

// Pass `true` to allow insecure fallback
const prng = detectPrng(true)
const ulid = factory(prng)

ulid()

Specification & Other Implementations

This library conforms to the official ulid/spec. You can find implementations in other languages there.

Development

Running Tests

npm test

Performance Benchmarks

npm run perf
ulid
336,331,131 op/s » encodeTime
102,041,736 op/s » encodeRandom
17,408 op/s » generate


Suites:  1
Benches: 3
Elapsed: 7,285.75 ms

License

MIT

About

Universally Unique Lexicographically Sortable Identifier

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • JavaScript 61.5%
  • TypeScript 38.5%