Prisma¶
dichit-backend uses Prisma 7 with the new prisma-client generator and the PostgreSQL adapter.
Generator & client¶
From prisma/schema.prisma:
generator client {
provider = "prisma-client"
output = "../src/generated/prisma"
moduleFormat = "esm"
engineType = "client"
runtime = "nodejs"
generatedFileExtension = "ts"
importFileExtension = "ts"
}
datasource db {
provider = "postgresql"
}
- Generated client lives at
src/generated/prisma— do not edit. @prisma/adapter-pg/pgprovide the connection (the URL comes fromDATABASE_URL).- Import the client via the
@prisma/clientalias mapped intsconfig.json.
Data-access rules (from AGENTS.md)¶
- All DB operations go through the application/service layer. Controllers never call Prisma directly.
- Transactions go through the
UnitOfWorkpattern. - Repositories under
src/infrastructure/db/postgres/repositoriesare the default boundary for reads and writes. - Avoid N+1. Prefer one efficient query over many.
- Raw SQL is acceptable inside repository/infrastructure code when it reduces round trips (always parameterized + typed).
Selectonly the columns the use case needs — avoid over-fetching.- Paginate list endpoints with deterministic ordering.
Typical flow¶
flowchart LR
UC["Use case"] --> R{"UserRepository"}
R -->|"prisma.user.findMany"| A["PrismaClient"]
R -->|"raw SQL (pg)"| RAW["pg raw query"]
A --> DB[(PostgreSQL)]
RAW --> DB Daily commands¶
| Command | Purpose |
|---|---|
pnpm prisma:generate | generate client after schema changes |
pnpm migrate:local:up | apply migrations (prisma migrate dev) |
pnpm migrate:local:deploy | deploy-only (prisma migrate deploy) |
pnpm migrate:local:status | drift check |
pnpm studio:local | GUI browse/edit |
pnpm db:local:pull | introspect local DB into schema |
See migrations.md for the full matrix.
Repository examples¶
- Reads/writes stay behind
src/application/interfaces/**contracts. - Prefer efficiency: a single repository-level query beats many round trips.
- Raw SQL maps into explicit DTOs/entities; never interpolate untrusted input.