Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ npm run format # Format code with Prettier
- `app.ts`: Main Fastify app with routes, CORS, Swagger docs
- `admin-app.ts`: Admin server (runs on PG_META_PORT + 1) for metrics
- `routes/*.ts`: REST endpoints mapping to library methods
- `templates/*.ts`: Type generation templates for different languages
- `routes/generators/*.ts`: Type generation endpoints backed by `@supabase/postgrest-typegen`

### Object Manager Pattern

Expand Down Expand Up @@ -116,9 +116,9 @@ Database errors are formatted to mimic `psql` output:

Type generation (`npm run gen:types:*`) works by:
1. Connecting to a database (test DB or custom via `PG_META_DB_URL`)
2. Fetching all schemas, tables, columns, relationships, functions, types
3. Passing data to language-specific templates in `src/server/templates/*.ts`
4. Templates output type definitions to stdout
2. Introspecting schemas, tables, columns, relationships, functions, and types via `@supabase/postgrest-typegen`'s `introspect()` (wrapped by `src/lib/generators.ts`)
3. Passing the metadata to the package's language generators (`generateTypescript`, `generateGo`, `generateSwift`, `generatePython`)
4. Generators output type definitions to stdout

Environment variables:
- `PG_META_GENERATE_TYPES`: Language (typescript, python, go, swift)
Expand Down Expand Up @@ -154,20 +154,27 @@ PG_META_MAX_BODY_LIMIT_MB=3 # Max request body size in MB (default:
PG_META_SHUTDOWN_GRACE_PERIOD_SECS=10 # Shutdown wait on in-flight work before force-exit (default: 10)
```

Type generation formatting (opt-in):
TypeScript generation on a worker thread (opt-in):
```bash
PG_META_FORMAT_IN_WORKER=true # Format generated types on a worker thread (default: false)
PG_META_FORMAT_POOL_SIZE=1 # Worker threads used for formatting (default: 1)
PG_META_FORMAT_MAX_QUEUE=20 # Max format calls in flight before returning 503 (default: 20)
PG_META_FORMAT_TIMEOUT_SECS=60 # Per-format timeout (default: 60)
PG_META_FORMAT_IN_WORKER=true # Generate TypeScript types on a worker thread (default: false)
PG_META_FORMAT_POOL_SIZE=1 # Worker threads used for generation (default: 1)
PG_META_FORMAT_MAX_QUEUE=20 # Max generation calls in flight before returning 503 (default: 20)
PG_META_FORMAT_TIMEOUT_SECS=60 # Per-generation timeout (default: 60)
PG_META_FORMAT_IDLE_TIMEOUT_SECS=30 # Idle time before a worker exits (default: 30)
```

Prettier is CPU-bound and synchronous, so formatting a large schema blocks the
event loop for seconds and the server cannot answer anything else, health checks
included. `PG_META_FORMAT_IN_WORKER=true` moves it to a worker thread. Always
off during type generation (`PG_META_GENERATE_TYPES`), which generates once and
exits.
`generateTypescript` is CPU-bound and synchronous: on a large schema it blocks
the event loop and the server cannot answer anything else, health checks
included. Formatting is the bulk of it (oxfmt since postgrest-typegen 0.2.0,
much faster than the prettier it replaced but still synchronous) and the string
building ahead of it costs too, so `PG_META_FORMAT_IN_WORKER=true` moves the
whole call to a worker thread (`src/server/format-pool.ts`). Metadata
crosses the thread boundary as a structured clone, which is plain JSON and does
not measurably change wall-clock time. Always off during type generation
(`PG_META_GENERATE_TYPES`), which generates once and exits.

The env vars keep their `PG_META_FORMAT_*` names from when the worker formatted
only, so existing deployments do not need reconfiguring.

## Testing Notes

Expand Down
Loading
Loading