@eristack/entity-id
Version:
UUID v7 entity identifiers — sortable, parseable, Drizzle column helper
58 lines (39 loc) • 1.34 kB
Markdown
```bash
pnpm add @eristack/entity-id
```
Optional peers: `drizzle-orm`, `zod`.
```ts
import {
generateEntityId,
parseEntityId,
entityIdToDate,
compareEntityIds,
} from "@eristack/entity-id";
const id = generateEntityId();
const canonical = parseEntityId(id); // lowercase + hyphens, version 7 only
entityIdToDate(canonical); // Date from embedded unix ms
compareEntityIds(idA, idB); // localeCompare — time order for generated ids
```
Accept API input with or without hyphens; always **persist** the canonical form from `parseEntityId`.
```ts
import { pgTable, varchar } from "drizzle-orm/pg-core";
import { entityIdColumn } from "@eristack/entity-id/drizzle";
export const partners = pgTable("partners", {
id: entityIdColumn("pgsql", "id").primaryKey(),
name: varchar("name", { length: 255 }).notNull(),
});
```
SQLite tests: `entityIdColumn("sqlite", "id")`.
```ts
import { entityIdSchema } from "@eristack/entity-id/zod";
const body = entityIdSchema.parse(req.body.parentId);
```
1. Default PK with `entityIdColumn` + `.primaryKey()`.
2. `parseEntityId` on every foreign key string from HTTP/JSON.
3. Do not use serial integers for public document ids — use `@eristack/doc-number` for display numbers.