why do we add prisma in ctx instead of using import ?

As stated in the doc
We include the Prisma Client in Context by default and recommend using this instead of importing it separately in each file.
We include the Prisma Client in Context by default and recommend using this instead of importing it separately in each file.
but there is no explanation Why is it better ? What is the difference ?
5 Replies
techblooded
techblooded2y ago
It’s mainly so u dont have to setup and initialize prisma or import it in every router. Instead the ctx option allows keeping all these sort of things shared.
Glorrin
GlorrinOP2y ago
Ok but if my routers become more complexe and I want to externalise it in services files, can I import it there or should I "drill prop" prisma in ctx ?
Ramazan
Ramazan2y ago
Could you give an example of what you mean?
techblooded
techblooded2y ago
i think the ctx is exposed to make it easier group up such shared services in one place, but there isn't any requirement to use it. i would say that the better way would be ctx because that way u dont have dup imports
cje
cje2y ago
its done this way so that you know you have the same instance in every procedure, better dx of having it on ctx, and lets you monkeypatch it for testing but in terms of just the procedure itself it doesn't make a difference
Want results from more Discord servers?
Add your server