|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Commands |
| 6 | + |
| 7 | +```bash |
| 8 | +# Root commands |
| 9 | +pnpm install # Install dependencies |
| 10 | +pnpm build # Build packages in order: domain → db,cloudflare → contracts |
| 11 | +pnpm check # Type check all packages/apps (runs per-package) |
| 12 | +pnpm test # Run all tests with vitest |
| 13 | + |
| 14 | +# App development (run from app directory) |
| 15 | +cd apps/effect-worker-api |
| 16 | +pnpm dev # Start local dev server with wrangler |
| 17 | +pnpm deploy # Deploy to Cloudflare |
| 18 | + |
| 19 | +# Database operations (run from packages/db) |
| 20 | +cd packages/db |
| 21 | +DATABASE_URL=postgres://... pnpm db:push # Push schema to database |
| 22 | +DATABASE_URL=postgres://... pnpm db:studio # Open Drizzle Studio |
| 23 | +DATABASE_URL=postgres://... pnpm db:generate # Generate migrations |
| 24 | +DATABASE_URL=postgres://... pnpm db:migrate # Run migrations |
| 25 | +``` |
| 26 | + |
| 27 | +## Architecture |
| 28 | + |
| 29 | +This is a Cloudflare Workers monorepo using Effect-TS. Apps import from shared packages via TypeScript path aliases (`@backpine/*`). |
| 30 | + |
| 31 | +### Package Dependency Flow |
| 32 | + |
| 33 | +``` |
| 34 | +@backpine/domain (types, schemas, errors) |
| 35 | + ↓ |
| 36 | +@backpine/db (Drizzle schema + Effect query programs) |
| 37 | + ↓ |
| 38 | +@backpine/cloudflare (FiberRef bridge, service tags, database factory) |
| 39 | + ↓ |
| 40 | +@backpine/contracts (HTTP/RPC API definitions, middleware tags) |
| 41 | + ↓ |
| 42 | +apps/ (handler implementations, middleware implementations) |
| 43 | +``` |
| 44 | + |
| 45 | +### Key Patterns |
| 46 | + |
| 47 | +**FiberRef Bridge**: Cloudflare's `env` and `ctx` are request-scoped. The pattern stores them in FiberRefs at request entry, then middleware reads them to provide services: |
| 48 | + |
| 49 | +```typescript |
| 50 | +// Entry point |
| 51 | +const effect = handleRequest(request).pipe(withCloudflareBindings(env, ctx)) |
| 52 | +return runtime.runPromise(effect) |
| 53 | + |
| 54 | +// Middleware reads FiberRef → provides service |
| 55 | +const env = yield* FiberRef.get(currentEnv) |
| 56 | +return { env, ctx } |
| 57 | +``` |
| 58 | + |
| 59 | +**Contract/Implementation Split**: `@backpine/contracts` defines abstract middleware tags; apps provide concrete implementations via Layers. |
| 60 | + |
| 61 | +**Query Programs**: Database queries live in `@backpine/db/src/queries/` as Effect programs requiring `PgDrizzle`. Handlers call these instead of inline queries. |
| 62 | + |
| 63 | +### Import Conventions |
| 64 | + |
| 65 | +- Apps use `@/*` for internal imports (e.g., `@/services`, `@/handlers`) |
| 66 | +- Packages use relative imports (`./`, `../`) |
| 67 | +- Cross-package imports use `@backpine/*` |
| 68 | +- Never re-export from `@backpine/*` in app barrel files; import directly where needed |
| 69 | + |
| 70 | +### Local Development |
| 71 | + |
| 72 | +For Hyperdrive (database), set in `.env`: |
| 73 | +``` |
| 74 | +CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE="postgres://postgres:postgres@localhost:5432/effect_worker" |
| 75 | +``` |
| 76 | + |
| 77 | +### Where Code Goes |
| 78 | + |
| 79 | +| Type | Location | |
| 80 | +|------|----------| |
| 81 | +| Domain types, branded schemas | `@backpine/domain/src/schemas/` | |
| 82 | +| Domain errors | `@backpine/domain/src/errors/` | |
| 83 | +| Database tables (Drizzle) | `@backpine/db/src/schema.ts` | |
| 84 | +| Reusable queries | `@backpine/db/src/queries/` | |
| 85 | +| HTTP endpoint definitions | `@backpine/contracts/src/http/groups/` | |
| 86 | +| RPC procedure definitions | `@backpine/contracts/src/rpc/procedures/` | |
| 87 | +| Middleware tags | `@backpine/contracts/src/*/middleware/` | |
| 88 | +| Handler implementations | `apps/*/src/handlers/` | |
| 89 | +| Middleware implementations | `apps/*/src/services/middleware.ts` | |
0 commit comments