ciallo-id

    Pure MoonBit 4-target RFC 9562 UUIDv7, Crockford ULID, and Snowflake ID generators for unmbt/ciallo

    id
    uuid
    uuidv7
    ulid
    snowflake
    ciallo
    Download zip
    Author
    Version
    0.3.0
    License
    MIT
    Last updated
    5 hours ago
    Downloads
    15

    #unmbt/ciallo-id

    Pure 4-target MoonBit ID generators (native, js, wasm, wasm-gc):
    • UUIDv7 (RFC 9562 time-ordered monotonic, recommended default for DB primary keys)
    • ULID (26-char Crockford Base32 monotonic)
    • Snowflake (64-bit distributed ID with clock-rollback protection)

    IdError

    pub(all) suberror IdError {
    InvalidFormat(String)
    InvalidWorkerId(Int)
    ClockMovedBackwards(last_ms~ : Int64, now_ms~ : Int64)
    } derive(Eq,
    Debug
    )

    Snowflake

    pub(all) struct Snowflake(Int64) derive(Eq,
    Debug
    )

    impl Show for Snowflake
    impl ToJson for Snowflake

    Snowflake::decompose

    fn Snowflake::decompose(self : Snowflake, epoch_ms? : Int64) -> SnowflakeParts

    Snowflake::to_int64

    fn Snowflake::to_int64(self : Snowflake) -> Int64

    Snowflake::to_json

    Serializes Snowflake as a decimal Int64String to preserve full 64-bit precision in JS/TS frontends.

    Snowflake::to_string

    fn Snowflake::to_string(self : Snowflake) -> String

    SnowflakeGenerator

    pub struct SnowflakeGenerator {
    worker_id : Int
    epoch_ms : Int64
    now_ms : () -> Int64
    last_ms : Int64
    seq : Int
    }

    64-bit Snowflake ID generator (41-bit timestamp + 10-bit worker_id + 12-bit sequence).

    SnowflakeGenerator::SnowflakeGenerator

    fn SnowflakeGenerator::SnowflakeGenerator(worker_id~ : Int, now_ms~ : () -> Int64, epoch_ms? : Int64) -> SnowflakeGenerator raise IdError

    SnowflakeGenerator::next

    SnowflakeParts

    pub(all) struct SnowflakeParts {
    timestamp_ms : Int64
    worker_id : Int
    sequence : Int
    } derive(Eq,
    Debug
    )

    Ulid

    pub struct Ulid {
    bytes : Bytes
    } derive(Eq,
    Debug
    )

    impl Show for Ulid
    impl FromParam for Ulid

    Ulid::as_bytes

    fn Ulid::as_bytes(self : Ulid) -> Bytes

    Ulid::parse

    fn Ulid::parse(input : StringView) -> Ulid raise IdError

    Ulid::timestamp_ms

    fn Ulid::timestamp_ms(self : Ulid) -> Int64

    Ulid::to_json

    Ulid::to_string

    fn Ulid::to_string(self : Ulid) -> String

    UlidGenerator

    pub struct UlidGenerator {
    now_ms : () -> Int64
    last_ms : Int64
    last_rand : FixedArray[Byte]
    rng_state : UInt64
    }

    Monotonic ULID generator (lexicographically sortable 26-char Crockford Base32).

    UlidGenerator::UlidGenerator

    fn UlidGenerator::UlidGenerator(now_ms~ : () -> Int64, seed? : UInt64) -> UlidGenerator

    UlidGenerator::next

    fn UlidGenerator::next(self : UlidGenerator) -> Ulid

    Uuid

    pub struct Uuid {
    bytes : Bytes
    } derive(Eq,
    Debug
    )

    impl Show for Uuid
    impl FromParam for Uuid

    Uuid::as_bytes

    fn Uuid::as_bytes(self : Uuid) -> Bytes

    Uuid::from_bytes

    fn Uuid::from_bytes(bytes : Bytes) -> Uuid raise IdError

    Uuid::parse

    fn Uuid::parse(input : StringView) -> Uuid raise IdError

    Uuid::timestamp_ms

    fn Uuid::timestamp_ms(self : Uuid) -> Int64

    Extracts the 48-bit Unix epoch milliseconds timestamp from a UUIDv7.

    Uuid::to_json

    Uuid::to_string

    fn Uuid::to_string(self : Uuid) -> String

    Uuid::variant

    fn Uuid::variant(self : Uuid) -> Int

    Uuid::version

    fn Uuid::version(self : Uuid) -> Int

    UuidV7Generator

    pub struct UuidV7Generator {
    now_ms : () -> Int64
    last_ms : Int64
    seq : Int
    rng_state : UInt64
    }

    Monotonic RFC 9562 UUIDv7 generator with injectable millisecond clock and PRNG seed.

    UuidV7Generator::UuidV7Generator

    fn UuidV7Generator::UuidV7Generator(now_ms~ : () -> Int64, seed? : UInt64) -> UuidV7Generator

    UuidV7Generator::next

    default_snowflake_epoch_ms

    let default_snowflake_epoch_ms : Int64

    Default Snowflake custom epoch: 2025-01-01T00:00:00Z in milliseconds.