macstr1k3r / trpc-nestjs-adapter

An adapter, allowing you to use TRPC inside of Nest.JS
158 stars 14 forks source link

Not an issue - I just don't know where to put what files? #3

Open jeffthompson1971 opened 1 year ago

jeffthompson1971 commented 1 year ago

Hi. I want to use this and try it out but i don't know how to use it exactly.. i see an example folder, so do i need to copy that whole folder including the /lib into my project? Or how does this work? I don't see how to run the example you have to see it working. Should I be able to?

medv commented 1 year ago
  1. You should include TRPCModule.forRoot in your app module as shown in the example.
  2. Take init-trpc and put it somewhere central. You should export type Router = typeof appRouter here (this is missing in the example but it is assumed you understand this, as it is the main requirement of tRPC that is covered in their great documentation.)
  3. Write your router with procedures in init-trpc, in each procedure you can resolve any service class you wish that has the actual business logic you route to (as shown with ctx.resolveNestDependency(AService)). This particular step is why this adapter exists, to allow you to use nest's dependency injection in your routes.
  4. At some point, you can start breaking out of init-trpc via providing a list of routes in each of your modules (eg. user/user.routes.ts), then importing them all back up to the init-trpc level where you actually create the router out of all of them (you can use tRPC.mergeRouters or nest them in namespaces, see tRPC docs). The main requirement of tRPC is that you combine all your routes into a router and export its type for your front-end to use. When you import the type of the router only, your front end does not include any server side code in the bundle, as typescript types get stripped.
  5. If you are not familiar with the intricacies of modules and bundlers, I would recommend putting some very loud console.logs in your server code, to make sure they never show up anywhere in your client's runtime (in terminal, nor in the browser) so that you can be sure you're not baking in your database secrets or anything of the sort.