TRPC multiple mutation procedure

I have currently a formular which calls my mutation. The query should create multiple different entities, currently I am doing that this way, is this way good or is there anything would I can refactor here?
import { createClientSchema } from '@/lib/zod/client-schema';
import { z } from "zod";
import { t } from "../trpc";

export const clientRouter = t.router({
client: t.procedure
.input(z.object({
text: z.string().nullish(),
}).nullish())
.query(({ input }) => {
return {
client: "hello"
}
}),
getAll: t.procedure.query(({ ctx }) => {
return ctx.prisma.customer.findMany();
}),
create: t.procedure
.input(createClientSchema)
.mutation(async ({ input }) => {
const company = await prisma?.company.create({
data: {
name: input.company
}
})

const client = await prisma?.customer.create({
data: {
...input,
companyId: company?.id,
}
})

const bankAccount = await prisma?.bankAccount.create({
data: {
customerId: client?.id,
bank_name: input.bank_name,
account_owner: input.account_owner,
iban: input.iban,
bic: input.bic
}
})

return {
client
}
})
});
import { createClientSchema } from '@/lib/zod/client-schema';
import { z } from "zod";
import { t } from "../trpc";

export const clientRouter = t.router({
client: t.procedure
.input(z.object({
text: z.string().nullish(),
}).nullish())
.query(({ input }) => {
return {
client: "hello"
}
}),
getAll: t.procedure.query(({ ctx }) => {
return ctx.prisma.customer.findMany();
}),
create: t.procedure
.input(createClientSchema)
.mutation(async ({ input }) => {
const company = await prisma?.company.create({
data: {
name: input.company
}
})

const client = await prisma?.customer.create({
data: {
...input,
companyId: company?.id,
}
})

const bankAccount = await prisma?.bankAccount.create({
data: {
customerId: client?.id,
bank_name: input.bank_name,
account_owner: input.account_owner,
iban: input.iban,
bic: input.bic
}
})

return {
client
}
})
});
1 Reply
utdev
utdevOP3y ago
Not sure if I should split up each entity creation
Want results from more Discord servers?
Add your server