How to add Many to Many relation fields in `z.object` and how to write procedure for it ?

I have created these two models
model Category {
id String @id @default(cuid())
name String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
products Product[]
}

model Product {
id String @id @default(cuid())
name String
description String?
selling_price Float
discount_price Float?
tags String[]
created_at DateTime @default(now())
updated_at DateTime @updatedAt
images Image[]
skus Sku[]
categories Category[]
}
model Category {
id String @id @default(cuid())
name String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
products Product[]
}

model Product {
id String @id @default(cuid())
name String
description String?
selling_price Float
discount_price Float?
tags String[]
created_at DateTime @default(now())
updated_at DateTime @updatedAt
images Image[]
skus Sku[]
categories Category[]
}
I have already created categories. I want to write a procedure to create a product but I don't know how to add categories. How to define categories in z.object
create: adminProcedure
.input(
z.object({
name: z.string().min(1),
description: z.string().min(1),
selling_price: z.number().min(1),
discount_price: z.number().min(1),
categories : ???
}),
)
.mutation(({ ctx, input }) => {
return ctx.prisma.product.create({ data: input });
})
create: adminProcedure
.input(
z.object({
name: z.string().min(1),
description: z.string().min(1),
selling_price: z.number().min(1),
discount_price: z.number().min(1),
categories : ???
}),
)
.mutation(({ ctx, input }) => {
return ctx.prisma.product.create({ data: input });
})
do I have to add array of category_id or something else? Will the product will be automatically added to the category table? having same question for other fields too i.e. skus, images...
15 Replies
Neto
Neto•2y ago
create: adminProcedure
.input(
z.object({
name: z.string().min(1),
description: z.string().min(1),
selling_price: z.number().min(1),
discount_price: z.number().min(1),
categories : z.array(z.string())
}),
)
.mutation(({ ctx, input }) => {
const { categories, ...rest } =input
return ctx.prisma.product.create({
data: rest,
categories: {
connect: categories.map((c) => ({id: c}))
}
});
})
create: adminProcedure
.input(
z.object({
name: z.string().min(1),
description: z.string().min(1),
selling_price: z.number().min(1),
discount_price: z.number().min(1),
categories : z.array(z.string())
}),
)
.mutation(({ ctx, input }) => {
const { categories, ...rest } =input
return ctx.prisma.product.create({
data: rest,
categories: {
connect: categories.map((c) => ({id: c}))
}
});
})
something like this
Neto
Neto•2y ago
Prisma
Relation queries (Concepts)
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
blazingbowser
blazingbowserOP•2y ago
Thanks, I am trying this, I will ask if have any issue
blazingbowser
blazingbowserOP•2y ago
it is giving this type error
blazingbowser
blazingbowserOP•2y ago
what should I do ?
Neto
Neto•2y ago
you should generate the new prisma types
blazingbowser
blazingbowserOP•2y ago
I ran the command to genrate prisma types turbo db:generate Do I have to define any types here ?
model Category {
id String @id @default(cuid())
name String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
products Product[]
}

model Product {
id String @id @default(cuid())
name String
description String?
selling_price Float
discount_price Float?
tags String[]
created_at DateTime @default(now())
updated_at DateTime @updatedAt
images Image[]
skus Sku[]
categories Category[]
}
model Category {
id String @id @default(cuid())
name String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
products Product[]
}

model Product {
id String @id @default(cuid())
name String
description String?
selling_price Float
discount_price Float?
tags String[]
created_at DateTime @default(now())
updated_at DateTime @updatedAt
images Image[]
skus Sku[]
categories Category[]
}
Neto
Neto•2y ago
typescript does not recognize the type you should look into it tho
blazingbowser
blazingbowserOP•2y ago
here's also it's showing this type error
blazingbowser
blazingbowserOP•2y ago
think I should // @ts-ignore and move on 👀
Neto
Neto•2y ago
yeah turbo and prisma are weird
blazingbowser
blazingbowserOP•2y ago
this feels too much illegal 😭
blazingbowser
blazingbowserOP•2y ago
there was a syntax error the fields should be inside data
data: {
...rest,
categories: {
connect: categories.map((c) => ({id: c}))
},
images: {
connect: images.map((i) => ({id: i}))
},
skus: {
connect: skus.map((s) => ({id: s}))
}
}
data: {
...rest,
categories: {
connect: categories.map((c) => ({id: c}))
},
images: {
connect: images.map((i) => ({id: i}))
},
skus: {
connect: skus.map((s) => ({id: s}))
}
}
Neto
Neto•2y ago
😅
zendev
zendev•17mo ago
Hi guys I have an almost identical situation and I'm also confused - when you say you have already created categories, do you mean that you created a categories table manually in Prisma Studio so that you can connect newly-created products to these existing categories? Can you explain what you mean by "I already created the categories"?
Want results from more Discord servers?
Add your server