API reference › @evolu/common › Type › idToUlid

function idToUlid(value: string & Brand<"Id">): string & Brand<"Ulid">;

Defined in: packages/common/src/Type.ts:10858

Converts an Id to the Ulid with the same 16 bytes.

The first 6 bytes of a ULID are its creation time in milliseconds. Ids created by createId are random, so their ULIDs have a random time. Ids created by createIdAsUuidv7 store their creation time in the same bytes, so their ULIDs keep it.

Example

import {
  assertEqual,
  assertTrue,
  createIdFromString,
  idToUlid,
  Ulid,
  ulidToId,
} from "@evolu/common";

const id = createIdFromString("todo");
const ulid = idToUlid(id);

assertTrue(Ulid.is(ulid));
assertEqual(ulidToId(ulid), id);