A question on procedure design.

I would like to know if it is best to try combine procedures or keep them separated. I don't want to repeat myself but am not sure regarding best practices for this types of thing. Would appreciate if someone could push me in the right direction. Something like this for bookmarks on 2 different post types bookmarkPost: protectedProcedure .input( z.object({ postId: z.string(), }) ) .mutation(async ({ ctx: { prisma, session }, input: { postId } }) => { await prisma.bookMark.create({ data: { userId: session.user.id, postId, }, }); }), bookmarkTech: protectedProcedure .input( z.object({ techId: z.string(), }) ) vs. combined bookmarkItem: protectedProcedure .input( z .object({ itemId: z.string(), itemType: z.string().refine(value => ['post', 'tech', 'course'].includes(value), { message: "Item type must be either 'post', 'tech', or 'course'", }), }) ) .mutation(async ({ ctx: { prisma, session }, input: { itemId, itemType } }) => { switch(itemType) { case 'post': await prisma.bookMark.create({ data: { userId: session.user.id, postId: itemId, }, }); break; case 'tech': await prisma.techBookMark.create({ data: { userId: session.user.id, techId: itemId, }, }); break; case 'course': await prisma.courseBookMark.create({ data: { userId: session.user.id, courseId: itemId, }, }); break; default: throw new Error("Invalid item type"); } }),
Keep them separated. This is a question about the architecture of your code, which you want to keep "clean" because its easier to read and comprehend on the first pass (and thus, easier to modify in the future). Once code is clean and working correctly you can do performance testing to find bottlenecks and degradations. Trying to predict the performance of your code is a vain effort. Avoid premature optimization. Focus on expressing how your code should behave in the simplest way possible. Make your job easier, not harder.
Thanks for that. I'll be careful going forward. I like to keep things separated but sometimes too much repetition like in this case i think would be best handled by combining them. i'm at quite a late stage of development on this current app so it's a good time to optimize. Made my components structure more reusable as well I've already built out everything individually and now it's time to make these optimizations
You should combine the repeated code into functions so changes are localized to one location. If you have to make a change or fix a bug, it's easier to do correctly if you've separated the concerns.

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Migrating to supabase from create-t3-turboHey guys! I'm migrating a create-t3-turbo app to the supabase create-t3-turbo template, and I'm a biSystem dark mode not detected when using darkMode: "media" in tailwind.config.tsHello! I am building a simple starter project using Tailwind and `shadcn-ui` components. I want the What is wrapping a next server action in startTransition doing?This was brought up in another question. I came up with the following explination, however this is jCan I call tRPC procedure inside another procedure?Something like this: ```js export const mainRouter = createTRPCRouter({ create: privateProcedure Anyone know how to go about using Tailwind Animation to animate a linethrough on hover?Or framer motion.Task scheduler infra?I'm looking for something that can do the following (or do essentially the same thing). It feels likNeed suggestions for better infra CICDalright, so for this project i use docker containers for everything. my compose file has a redis cacConditional render, why can't it be done?I understand the reason this happens it that *something* doesn't understand that the Loader already do something once server action is completedIs there a way to achieve this (see title)? I’m using useTransition to start the action and need to How to use discord.js in Nextjs app dirHello, I am trying to use discord.js in my app but getting alot of errors, is this even possible?: `best way to fetch data in nextjsI was wondering what's the best way at the moment to query for external apis in NextJS? I know that @next/font turbo t3You might be using incompatible version of `@next/font` (13.4.4) and `next` (13.1.6). what version s