nmeng
nmeng17mo ago

One file per function call on the server side

I'd like to structure my server with one file per function call -- is this an antipattern? If not, does anyone have an example repo of how to structure the server code with one function/rpc call per file? Do I have to have one router per function call?
4 Replies
Nick
Nick17mo ago
Just export your procedure from each file and then construct your routers by importing them What’s the challenge?
nmeng
nmeng17mo ago
Hey, thanks @Nick Lucas -- the problem is that the function is created using the object from initTRPC.create(); (i.e. we do const t = initTRPC.create(); then const app = router({ endpoint: t.procedure.input(...).query(() => { .... }), }); I'd like to put the whole t.procedure.... into a separate file, not only the function passed to .query() so would I from the endpoint file export e.g. const endpointFactory = (procedure) => procedure.input().query(() => { ... }) and then (in the app router) do router({ endpoint: endpointFactory(t.procedure), }) which seems ... over the top
Nick
Nick17mo ago
You can import t everywhere, and it's the only way to create a procedure because it's what contains all your types But export const listThing = t.procedure.query() is totally fine
nmeng
nmeng16mo ago
Alright, that's what I'll do! Thanks 🙏