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
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 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)