How to organise reusable functions

How do you guys organize functions that all need the same context (usually from tRPC)? For example let's say that you have 10 functions you use multiple times across multiple routers and they all share the same context. Do you use a class?
N
Nick410d ago
Can you share an example?
S
Shooe#6468406410d ago
E.g. a function that given an input user ID checks a bunch of properties on that user (e.g. for an educational app that the current user has a valid teacher's contract) Or a function that checks that some invariants are hold during certain operations (e.g. for a calendar app that multiple events don't overlap on the same calendar when: moving, swapping, rescheduling, etc.)
N
Nick410d ago
Probably makes sense to type the function just with the deps it needs, rather than a full Context These just sound like utilities so ideally should be too coupled to the APi right?
S
Shooe#6468406410d ago
It's kinda cumbersome to do that though. It's also difficult to enforce that the parameter is the first/last of the series of arguments so you end up with an inconsistent library TBH I don't see the context as part of the API The context is more of a business logic piece in my mind
N
Nick410d ago
This is definitely a case of "move stuff around until it feels right" tRPC doesn't really have any strong opinions about how to do this
S
Shooe#6468406410d ago
Yeah I know, I was just wondering how you guys get by
N
Nick410d ago
Personally, I put service classes into the Context and use it like DI, then have some utility functions without any strong pattern for their interfaces
S
Shooe#6468406410d ago
Wdym by "putting service classes into the context" exactly? Sounds like what I'm looking for
N
Nick410d ago
using createContext I instantiate any services I need and then procedures can consume them Like ORM Repositories, API Clients, etc
S
Shooe#6468406410d ago
So then you call something like ctx.someService.create(...)?
N
Nick410d ago
You can also use middlewares to extend the Context with extra services if you want to scope them If you like No right answers here, just what fits your needs/opinions
S
Shooe#6468406410d ago
I know, but I'm trying to understand what your way is and if it works for you
N
Nick410d ago
my way supports this usage ctx.db.findById(id) already created
S
Shooe#6468406410d ago
I'm assuming db there is your repository or a Prisma extended client. What about your services?
N
Nick410d ago
Everything done the same way, just a namespace on ctx
S
Shooe#6468406410d ago
I think I got it thanks
More Posts
How to get response type depending on the input params?Simple example of a normal JS function with generics: ``` const example = <T extends unknown>(paramshow to build tRPC and Prisma with express?I am using TSC as the official docs example does. But when I use paths in tsconfig.json, It does notHow to infer types from input?When I call my procedure from the client I send an array of strings as an input. How can I infer theHow to infer types of a queryCoinsider the example `const hello = trpc.hello.useQuery();` I would like to export the type we get Data Visualisation/Chartsis anyone using tRPC on data visualisation (say bar chart/line chart) in a scenario close to a bankitrpc + AWS Lambda (through cdk)Hi all, has anyone successfully integrated tRPC with AWS Lambda? My current stack is API Gateway + LIs it possible to narrow an output schema if the query optionally doesn't return all fields?I have a router procedure that has an input schema that has an optional `filter` that changes the shFetching different server url than defined in configIs it possible to access the reactQuery instance and fetch different server url? I would like to useinput using z.or not working properlyi have an input like this let input = z.object({ name: z.string().optional() }).or(z.object({ How can I disable batching with fastify adapter?I cant seem to find a way to disable batching for my server, and this link doesnt help me much httpsIssue with monorepo architecture ant tRPCHi, we had an issue with batched requests that if we batch some requests they produce a TRPCClientErUsing tRPC in CRON jobsHey everyone, this might be a very stupid question, but is it possible to use tRPC inside a CRON joasync createContext for Express AdapterBeen debugging an odd behavior for the past hour, it seems like that an async function does not workIs it possible to split the router definition with the imlementation?I want to define the server router(input\output\meta) in a separate package from the server package Cache not working for `useQuery`I have a query like this: ```js const { data: article, isFetching } = api.public.getArticle.useQueryZod File Upload Validation with Open-Api Support?Hi guys, anyone know how to validate file upload with zod and get also open-api support?Zod validation and open-api support for File on the server?Hi guys, anyone know how to validate a File upload using zod? and also have open-api support?is context cached?If I put an object on the context that represents the User record from my database... ``` export asyJSON inferred router output not matchingHello. I have a procedure query which is returning Json from a postgresql db using Prisma. The type Best way to implement input based validation on a router procedureHi guys, bit of a noob. I have already created a 'protectedProcedure', ensuring the user is logged