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•12mo 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•12mo 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
blazingbowser•12mo ago
Thanks, I am trying this, I will ask if have any issue
blazingbowser
blazingbowser•12mo ago
it is giving this type error
blazingbowser
blazingbowser•12mo ago
what should I do ?
Neto
Neto•12mo ago
you should generate the new prisma types
blazingbowser
blazingbowser•12mo 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•12mo ago
typescript does not recognize the type you should look into it tho
blazingbowser
blazingbowser•12mo ago
here's also it's showing this type error
blazingbowser
blazingbowser•12mo ago
think I should // @ts-ignore and move on 👀
Neto
Neto•12mo ago
yeah turbo and prisma are weird
blazingbowser
blazingbowser•12mo ago
this feels too much illegal 😭
blazingbowser
blazingbowser•12mo 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•12mo ago
😅
zendev
zendev•11mo 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
More Posts