API reference › @evolu/common › Type › ulidToId

function ulidToId<B extends string = never>(
  value: string & Brand<"Ulid">,
  ..._validation: IdBrandValidation<B>
): CreatedId<B>;

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

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

Use this to store records whose external keys are ULIDs. Unlike createIdFromString, the mapping is reversible with idToUlid, so the original ULID does not need its own column. The first 6 bytes of a ULID are its creation time, which the Id keeps.

Example

import {
  assertEqual,
  assertType,
  idToUlid,
  Ulid,
  ulidToId,
  type Brand,
  type Id,
} from "@evolu/common";

const ulid = Ulid.orThrow("01ARZ3NDEKTSV4RRFFQ69G5FAV");
const todoId = ulidToId<"Todo">(ulid);

assertEqual(todoId, "AVY-OrXT1nZMYe-5kwK9Ww");
assertType<typeof todoId, Id & Brand<"Todo">>();
assertEqual(idToUlid(todoId), ulid);