Skip to content

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/prismado not edit.
  • @prisma/adapter-pg / pg provide the connection (the URL comes from DATABASE_URL).
  • Import the client via the @prisma/client alias mapped in tsconfig.json.

Data-access rules (from AGENTS.md)

  1. All DB operations go through the application/service layer. Controllers never call Prisma directly.
  2. Transactions go through the UnitOfWork pattern.
  3. Repositories under src/infrastructure/db/postgres/repositories are the default boundary for reads and writes.
  4. Avoid N+1. Prefer one efficient query over many.
  5. Raw SQL is acceptable inside repository/infrastructure code when it reduces round trips (always parameterized + typed).
  6. Select only the columns the use case needs — avoid over-fetching.
  7. 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.