5.3 KiB
Backend Generation Process
Backend generation follows a pipeline aligned with runtime, auth, and validation docs:
DSL ↓ CLI scaffolding ↓ backend code generation ↓ auth generation ↓ runtime infrastructure ↓ database runtime ↓ migration ↓ seed ↓ validation
Follow:
backend/architecture.mdbackend/runtime-rules.mdbackend/prisma-rules.mdbackend/prisma-service.mdbackend/database-runtime.mdbackend/seed-rules.mdbackend/service-rules.mdauth/keycloak-architecture.mdauth/backend-auth-rules.mdauth/keycloak-realm-template-rules.md
Step 1 — Parse DSL
Read DSL inputs and extract:
- entities
- attributes, including the actual primary key attribute per entity
- enums
- foreign keys
The generator must treat auth as default runtime infrastructure, not as a DSL feature toggle.
Step 2 — CLI scaffolding
Use official CLIs before generating backend code:
- NestJS project scaffold in
server/(seegeneration/scaffolding-rules.md) - install backend dependencies:
@prisma/clientprisma@nestjs/configjose- seed runner when needed by the chosen package tooling
The generator must not use deprecated Keycloak-specific Node adapters such as keycloak-connect.
Step 3 — Core backend code generation
Generate backend source artifacts:
- Prisma schema (
server/prisma/schema.prisma) from the domain DSL:- attributes
- primary keys
- relations
- enums
- NestJS modules per entity:
- module
- controller
- service
- DTO files:
create-entity.dto.tsupdate-entity.dto.tsentity.response.dto.ts(or equivalent)
- PrismaService:
- implement
OnModuleInit - call
await this.$connect()inonModuleInit() - do not use a
beforeExithook
- implement
- Service update methods:
- sanitize update payload before Prisma
- remove
id, the entity primary key, and readonly attributes fromdata - do not pass the raw request body directly to
prisma.*.update()
Use mapping rules from backend/prisma-rules.md:
- DSL
decimal-> DTOstring - DSL
date-> DTOstring(ISO)
Step 4 — Backend auth generation
Generate auth infrastructure as part of the normal backend output. Auth must not be deferred to a manual post-step.
Required generated auth artifacts:
server/src/auth/auth.module.ts- JWT auth guard
- roles guard
@Public()decorator@Roles()decorator- typed authenticated principal interface
- typed auth-aware config validation in
server/src/config/
JWT verification rules:
- verify JWTs with issuer, audience, and JWKS
- use a standards-based library such as
jose - resolve JWKS in this exact order:
- explicit
KEYCLOAK_JWKS_URL - OIDC discovery
${issuer}/protocol/openid-connect/certs
- explicit
RBAC rules:
- extract authorization roles only from
realm_access.roles - do not infer roles from other claims
- apply CRUD defaults by HTTP method:
GET->viewer,editor,adminPOST,PATCH,PUT->editor,adminDELETE->admin
- mark
/healthas public with@Public()
Controller generation rules:
- generated CRUD controllers must receive auth decorators by default
- path params must still use the actual entity primary key name (
:id,:code, etc.) - public routes must be explicit rather than implicit
Step 5 — Runtime infrastructure
Generate backend runtime config files:
server/.envserver/.env.exampleserver/src/config/*typed validation helpersserver/package.jsonlifecycle:"postinstall": "prisma generate"
server/package.jsonPrisma seed:"prisma": { "seed": "ts-node prisma/seed.ts" }(ortsxequivalent if that is the chosen project standard)
The generated backend env contract must include:
PORTDATABASE_URLCORS_ALLOWED_ORIGINSKEYCLOAK_ISSUER_URLKEYCLOAK_AUDIENCEKEYCLOAK_JWKS_URL(optional)
Fail-fast config rule:
- backend startup must fail fast when required auth or database env vars are missing
- the generator must not silently fall back to production auth values in code
Commands that must be supported and documented:
npx prisma generatenpx prisma migrate devnpx prisma db seed
Step 6 — Database runtime
Create docker-compose.yml at the project root with PostgreSQL only.
Minimum required compose characteristics:
services.postgresimage: postgres:16ports: ["5432:5432"]
Credentials and database name in compose must match DATABASE_URL.
Keycloak must remain an external runtime dependency and must not be added to docker-compose.yml in this repository.
Step 7 — Migration
Apply schema to the development database:
cd server
npx prisma migrate dev
Step 8 — Seed
Run development seed:
cd server
npx prisma db seed
Seed file location: server/prisma/seed.ts.
Step 9 — Validation
Run runtime, auth, and contract checks from generation/post-generation-validation.md, including:
- docker-compose exists and DB container starts
- Prisma lifecycle commands succeed
- seed runs
/healthresponds without auth- protected routes reject unauthenticated requests with
401 - authenticated users with insufficient role receive
403 - React Admin-compatible API responses include
idfor every record