Inside create t3 app with Drizzle, why do we pass the database through ctx on each procedure?
If I'm not mistaken, we ideally don't want to do this if we want to break our application into separate layers (One of them being a data acess layer that we use to connect to our database).
Would there be an issue if I remove this from the context and just import the db directly from the src/server/db/index.ts file?
Default way of doing this in T3:
What I would rather do:
Here's how it's being added to context now, I'd rather not use this? I feel like it's there for a reason though and I can't figure out why 😦
4 Replies
Id like to know this too!
I believe it is related to being able to use the correct db instance for each procedure. In prisma for example it's possible to create multiple extensions where each prisma client has a specific functionality. Maybe this is also true for drizzle.
It's a good question though. Don't know the answer
its just basic dependency injection
if you want to import directly from drizzle, be
same effect, just one more import on the trpc file
Sweet, I think I will use imports then. Specifically since this will allow our layered architecture to stay clean.
We go from Router Layer > Service Layer > Data Access.
At any time we can swap out Drizzle to any ORM in the Data Access layer which is great. Having an orm implementation directly in the ctx of TRPC forces us to commit to Drizzle / sort of hops from Data Access to Router.
i would disagree that importing the db instead of semi-DIing it creates clearer separation of layers, you're still making all the same assumptions
and have now made testing harder
but yea importing works