ids/ulid

A module for generating ULIDs (Universally Unique Lexicographically Sortable Identifier).

Types

The messages handled by the actor. The actor shouldn’t be called directly so this type is opaque.

pub opaque type Message

The internal state of the actor. The state keeps track of the last ULID components to make sure to handle the monotonicity correctly.

pub opaque type State

Constants

pub const crockford_alphabet: String = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
pub const max_time: Int = 281_474_976_710_655

Functions

pub fn decode(ulid: String) -> Result(#(Int, BitArray), String)

Decodes an ULID into #(timestamp, randomness).

pub fn from_parts(
  timestamp: Int,
  randomness: BitArray,
) -> Result(String, String)

Generates an ULID with the supplied timestamp and randomness.

pub fn from_timestamp(timestamp: Int) -> Result(String, String)

Generates an ULID with the supplied unix timestamp in milliseconds.

pub fn generate() -> String

Generates an ULID.

pub fn monotonic_from_timestamp(
  channel: Subject(Message),
  timestamp: Int,
) -> Result(String, String)

Generates an ULID from a timestamp using the given channel with a monotonicity check. This guarantees sortability if the same timestamp is used repeatedly back to back.

Usage

import ids/ulid

let assert Ok(channel) = ulid.start()
let Ok(id) = ulid.monotonic_from_timestamp(channel, 1_696_346_659_217)
pub fn monotonic_generate(
  channel: Subject(Message),
) -> Result(String, String)

Generates an ULID using the given channel with a monotonicity check. This guarantees sortability if multiple ULID get created in the same millisecond.

Usage

import ids/ulid

let assert Ok(channel) = ulid.start()
let Ok(id) = ulid.monotonic_generate(channel)
pub fn start() -> Result(Subject(Message), StartError)

Starts a ULID generator.

Search Document