Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
3 replies
Viszy A

Need help with creating a mutation that involves relations

I'm a complete beginner just started and I'm really confused on how I will create this router with the mutation with all the relations

The schema is pretty big so I'll show one relation:
model Profile {
  id                String           @id @default(cuid())
  experiences       Experience[]

}

model Experience {
  id              String   @id @default(cuid())
  companyName     String
  Profile         Profile? @relation(fields: [profileId], references: [id])
  profileId       String?
}


So the inputed array could be with multiple "experiences"

data : {
experiences: [
{.... experience 1},
{... experience 2},
... etc
],
}

And I want to create a mutation that can take that entire data and input it into the profile model,
and this is my attempt on it, but i have no idea on how to make it actually work

import { z } from "zod";
import {
    createTRPCRouter,
    protectedProcedure,
    publicProcedure,
} from "@/server/api/trpc";

export const profileRouter = createTRPCRouter({
    createProfile: protectedProcedure
        .input(z.object({
           
            experiences: z.array(z.object({
                companyName: z.string(),
            
            })),
         
        }))
        .mutation(async ({ ctx, input }) => {
             return ctx.db.profile.create({
                data: {
                    experiences: {
                        ctx.db.experiences.create(input.experiences),
                    },
                },
            });
        }),
});
Was this page helpful?