Programmatically determine which field to update in a Prisma mutation

I want to update a specific field in my database. I'm passing the id of the record to update, the name of the field and the new value for this field, into my router to the chgField procedure. Can this be done with some syntax like that shown in * input.field: input.nu * below , if so, help on the syntax here would be great (this doesn't work).
export const peopleRouter = router({
chgField: publicProcedure
.input(
z.object({
idx: z.string(),
field: z.string(),
nu: z.string(),
}),
)
.mutation(({ ctx , input }) => {
return ctx.prisma.user.update({
data: {
*input.field: input.nu*
},
where: {
id: input.idx
}
});
}),
});
export const peopleRouter = router({
chgField: publicProcedure
.input(
z.object({
idx: z.string(),
field: z.string(),
nu: z.string(),
}),
)
.mutation(({ ctx , input }) => {
return ctx.prisma.user.update({
data: {
*input.field: input.nu*
},
where: {
id: input.idx
}
});
}),
});
I sense it might not be possible this way!
4 Replies
Amos
Amos•3y ago
Just send the data and use that? 😕
return ctx.prisma.user.update({
data: input.data,
where: {
id: input.idx
}
});
return ctx.prisma.user.update({
data: input.data,
where: {
id: input.idx
}
});
ibamsey
ibamseyOP•3y ago
I only have the new data for one field within the record. So I need to tell the update which field to apply the new data to. (I hope that makes sense) i.e. I don't have the entire tuple. e.g. I want the change FirstName within a User record to "bill".
Amos
Amos•3y ago
So input.data = {"FirstName": "bill"}? It's literally the same as what you are doing But if you really want it that way then do
data: {
[input.field]: input.nu
}
data: {
[input.field]: input.nu
}
ibamsey
ibamseyOP•3y ago
@Amos, thanks, now have that working. (I'm new to JS and that was the root problem.)
Want results from more Discord servers?
Add your server