|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Development Commands |
| 6 | + |
| 7 | +### Build System |
| 8 | + |
| 9 | +- `pnpm build` - Build all packages using Turbo |
| 10 | +- `pnpm watch` - Watch mode for all packages |
| 11 | +- `pnpm lint` - Run ESLint across all packages |
| 12 | +- `pnpm test` - Run tests for all packages |
| 13 | + |
| 14 | +### Package Management |
| 15 | + |
| 16 | +- Uses `pnpm` with workspaces |
| 17 | +- Package manager is pinned to `[email protected]` |
| 18 | +- Packages are located in `packages/`, `samples/`, and `tests/` |
| 19 | + |
| 20 | +### Testing |
| 21 | + |
| 22 | +- Runtime package tests: `pnpm test` (includes vitest, typing generation, and typecheck) |
| 23 | +- CLI tests: `pnpm test` |
| 24 | +- E2E tests are in `tests/e2e/` directory |
| 25 | + |
| 26 | +### ZenStack CLI Commands |
| 27 | + |
| 28 | +- `npx zenstack init` - Initialize ZenStack in a project |
| 29 | +- `npx zenstack generate` - Compile ZModel schema to TypeScript |
| 30 | +- `npx zenstack db push` - Sync schema to database (uses Prisma) |
| 31 | +- `npx zenstack migrate dev` - Create and apply migrations |
| 32 | +- `npx zenstack migrate deploy` - Deploy migrations to production |
| 33 | + |
| 34 | +## Architecture Overview |
| 35 | + |
| 36 | +### Core Components |
| 37 | + |
| 38 | +- **@zenstackhq/runtime** - Main database client and ORM engine built on Kysely |
| 39 | +- **@zenstackhq/cli** - Command line interface and project management |
| 40 | +- **@zenstackhq/language** - ZModel language specification and parser (uses Langium) |
| 41 | +- **@zenstackhq/sdk** - Code generation utilities and schema processing |
| 42 | + |
| 43 | +### Key Architecture Patterns |
| 44 | + |
| 45 | +- **Monorepo Structure**: Uses pnpm workspaces with Turbo for build orchestration |
| 46 | +- **Language-First Design**: ZModel DSL compiles to TypeScript, not runtime code generation |
| 47 | +- **Kysely-Based ORM**: V3 uses Kysely as query builder instead of Prisma runtime dependency |
| 48 | +- **Plugin Architecture**: Runtime plugins for query interception and entity mutation hooks |
| 49 | + |
| 50 | +### ZModel to TypeScript Flow |
| 51 | + |
| 52 | +1. ZModel schema (`schema.zmodel`) defines database structure and policies |
| 53 | +2. `zenstack generate` compiles ZModel to TypeScript schema (`schema.ts`) |
| 54 | +3. Schema is used to instantiate `ZenStackClient` with type-safe CRUD operations |
| 55 | +4. Client provides both high-level ORM API and low-level Kysely query builder |
| 56 | + |
| 57 | +### Package Dependencies |
| 58 | + |
| 59 | +- **Runtime**: Depends on Kysely, Zod, and various utility libraries |
| 60 | +- **CLI**: Depends on language package, Commander.js, and Prisma (for migrations) |
| 61 | +- **Language**: Uses Langium for grammar parsing and AST generation |
| 62 | +- **Database Support**: SQLite (better-sqlite3) and PostgreSQL (pg) only |
| 63 | + |
| 64 | +### Testing Strategy |
| 65 | + |
| 66 | +- Runtime package has comprehensive client API tests and policy tests |
| 67 | +- CLI has action-specific tests for commands |
| 68 | +- E2E tests validate real-world schema compatibility (cal.com, formbricks, trigger.dev) |
| 69 | +- Type coverage tests ensure TypeScript inference works correctly |
| 70 | + |
| 71 | +## Key Differences from Prisma |
| 72 | + |
| 73 | +- No runtime dependency on @prisma/client |
| 74 | +- Pure TypeScript implementation without Rust/WASM |
| 75 | +- Built-in access control and validation (coming soon) |
| 76 | +- Kysely query builder as escape hatch instead of raw SQL |
| 77 | +- Schema-first approach with ZModel DSL extension of Prisma schema language |
| 78 | + |
| 79 | +## Development Notes |
| 80 | + |
| 81 | +- Always run `zenstack generate` after modifying ZModel schemas |
| 82 | +- Database migrations still use Prisma CLI under the hood |
| 83 | +- Plugin system allows interception at ORM, Kysely, and entity mutation levels |
| 84 | +- Computed fields are evaluated at database level for performance |
0 commit comments