express-prisma-pattern
Skill2 starsChanged 24 days agoMITView on GitHub
House pattern for Express + Prisma backends structured as manual Clean Architecture (controller → usecase → repository, wired by hand in a factory) — the real shape of two production Express + Prisma backends. Use whenever creating a new route, usecase or domain in an Express backend on this stack, even if the request is just "create the X endpoint" or "add the Y usecase".
In this repository28 other files it gives its agents.
- AGENTS.mdAGENTS.md
- ai-newsSkill
- break-uiSkill
- ci-cd-and-automationSkill
- clickup-task-breakdownSkill
- client-facing-docsSkill
- codebase-designSkill
- design-auditSkill
- diagnosing-bugsSkill
- domain-modelingSkill
- frontend-designSkill
- game-assetsSkill
- game-design-docSkill
- grillingSkill
- improve-codebase-architectureSkill
- motion-dictionarySkill
- playwright-cliSkill
- prisma-schema-conventionsSkill
- rag-architectSkill
- rbac-designSkill
- redesign-existing-projectsSkill
- screenshot-to-codeSkill
- secrets-gitleaksSkill
- security-auditSkill
- sentry-setup-ai-monitoringSkill
- tddSkill
- token-efficiencySkill
- web-design-guidelinesSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 59
Controller never imports Prisma: Not even the db client — if you're tempted, the logic belongs in the usecase or repository.
- Very specific: line 61
Wiring happens in
main/factories/, by hand: No DI container, no decorators — a plain make<X>Controller() function that news the chain.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Express + Prisma — house pattern
Manual Clean Architecture, not a framework's DI container: every layer is a plain TS interface, wired by hand in a factory function. Confirmed by reading real code in two production backends (independent codebases, different Prisma majors, same controller/usecase/repository/factory shape) — the shell below is non-negotiable, do not invent a NestJS-style module system on top of it. The two repos diverge on where zod validation runs (see rule 4) — that's the one confirmed exception to "same shape", not an oversight.
Per-feature structure
src/
├── controllers/<domain>/<action>-controller.ts # implements Controller. Parses HttpRequest, calls the usecase, returns HttpResponse via helpers. Never imports Prisma.
├── usecases/<domain>/<action>-usecase.ts # Business logic. execute(...). Depends on the repository INTERFACE, injected via constructor.
├── interfaces/
│ ├── controllers/controller.ts # shared Controller contract
│ ├── http/http-request.ts, http-response.ts # shared request/response shape
│ └── repositories/<domain>-repository.ts # repository contract + domain types
├── repository/<domain>/<domain>-repository.ts # Prisma<Domain>Repository implements <Domain>Repository
└── main/factories/<domain>-factory.ts # make<Action>Controller(): wires repository → usecase → controller by handThe shell (non-negotiable)
// interfaces/controllers/controller.ts
export interface Controller<T = any> {
handle(request: HttpRequest): Promise<HttpResponse<T>>;
}// controllers/area/area-controllers.ts
export class ListAreasController implements Controller {
constructor(private readonly usecase: ListAreasUsecase) {}
async handle(_request: HttpRequest): Promise<HttpResponse<AreaComContagem[]>> {
return ok(await this.usecase.execute());
}
}// main/factories/area-factory.ts — manual composition root, no DI container
const repository = new PrismaAreaRepository();
export const makeListAreasController = (): ListAreasController =>
new ListAreasController(new ListAreasUsecase(repository));Response shaping always goes through the shared helpers (ok, badRequest, unauthorized, serverError), never a raw res.json() inside the controller. Every handle() wraps its body in try/catch → serverError(e) — an uncaught throw inside a controller is a bug.
The business-logic layer — one correct shape, one tolerated legacy shape
- UseCase class (
usecases/<domain>/<action>-usecase.ts,execute()method, repository INTERFACE injected via constructor) — the only shape for new code. Matches theList<Entity>Usecaseshape. - Legacy service function (
services/<domain>/<domain>-service.ts) — exists in older code in some repos (e.g.<domain>-service.ts), and it is not a lighter version of the same pattern: it imports the Prisma client directly, with no repository interface at all. This is debt, not a second accepted form — never model new code after it, and touching a neighboring line doesn't obligate rewriting it.
Non-negotiable rules
- Controller never imports Prisma. Not even the db client — if you're tempted, the logic belongs in the usecase or repository.
- Repository is always interface + implementation, in separate files (
interfaces/repositories/<x>.tscontract,repository/<x>/<x>-repository.tswithPrisma<X>Repository implements <X>Repository). No controller or usecase importsPrismaClient/the db client directly. - Wiring happens in
main/factories/, by hand. No DI container, no decorators — a plainmake<X>Controller()function thatnews the chain. - Validate with
zod(already a dependency in every repo on this stack) as the first thing that runs onrequest.body, before any business logic — confirmed in two different spots depending on the repo: in the controller before calling the usecase (e.g.<domain>-controllers.ts), or as the first step insideusecase.execute()(e.g.Create<Entity>Usecase). Match whichever placement the project already uses — check an existing feature before picking one — never split validation across both layers for the same feature. Manualif (!field) return badRequest(...)chains are legacy, not the model to copy into new code. - Tenant/workspace isolation at the repository query — same bar as
security-baseline.md. Being Express instead of NestJS is not an exception.
Source of truth for the model
Exact fields come from the project's data model document. If it's not there, stop and ask before inventing fields.
Definition of done (per feature)
- Model in Prisma + migration applied
- Controller/usecase/repository/factory files created, wired through the factory
- Response goes through the shared http helpers, try/catch → serverError
- Validation via zod (not manual ad-hoc checks) as the first thing that runs, in whichever layer this project already validates at (controller or usecase — see rule 4)
- Lint and build clean
- Route registered in
main/routes/
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

