# Generated Code

## Overview

Run `erp-kit module generate code -p <modules-dir>/<module-name>` to generate deterministic `.generated.ts` files from command and query documentation.

## What gets generated

- `lib/errors.generated.ts` — Domain error classes from Error Scenarios sections
- `lib/permissions.generated.ts` — Permission definitions nested by scope (`permissions.scope.command`)
- `command/<name>.generated.ts` — Command shells wiring permissions + `run` function
- `query/<name>.generated.ts` — Query shells wrapping `run` function
- `db/<model>.lifecycle.generated.ts` — Lifecycle definition from model doc State Transitions table (stateful models only)

## File convention

- `.generated.ts` files are always overwritten — never edit them
- Custom logic goes in `.ts` files (same base name) that import from `.generated.ts`

## Workflow

1. Write command/query docs (skills 1-3)
2. Write db models (skill 4, step 2)
3. Run `pnpm generate` for kysely types
4. Run `erp-kit module generate code -p <root>/<module>`
5. Implement business logic in `.ts` files importing from `.generated.ts`

## What you still write by hand

- Input interface field definitions (in `.generated.ts` they're stubs)
- Command handler business logic (validation, mutations)
- State transition commands — `executeTransition` for simple transitions, `lifecycle.tryTransition` for transitions with side effects
- Test assertions
- Complex queries beyond simple lookups
