4jandals
4jandals
WWasp
Created by 4jandals on 5/2/2025 in #đŸ™‹questions
Best way to generate null form values for a Prisma update
Not sure best place to ask this question, but I figure developers building CRUD forms in wasp might have the same question. I have an entity like this:
model Foo {
id String @id @default(uuid(7))
bar String?
}
model Foo {
id String @id @default(uuid(7))
bar String?
}
I'm updating the entity like this in the back-end:
export const updateFoo: UpdateFoo<{
id: string;
bar?: string;
}, Foo> = async ({ id, ...updatedData }, context) => {

if (!context.user) {
throw new HttpError(401, 'Not authorized');
}

const updated = await context.entities.Foo.update({
where: {
id,
},
data: {
updatedData,
}
});
export const updateFoo: UpdateFoo<{
id: string;
bar?: string;
}, Foo> = async ({ id, ...updatedData }, context) => {

if (!context.user) {
throw new HttpError(401, 'Not authorized');
}

const updated = await context.entities.Foo.update({
where: {
id,
},
data: {
updatedData,
}
});
I have a Zod schema in the front-end code
formSchema = z
.object({
title: z.string().optional().nullable()
})
formSchema = z
.object({
title: z.string().optional().nullable()
})
In my form if I clear the value of bar it generates the following for updatedData:
{
bar: undefined
}
{
bar: undefined
}
As a result bar will be ignored in the Prisma update and so it doesn't clear to null. What's best practise here?: 1. Do I need to find a way to get Zod to generate null for empty fields (I've tried) 2. Or do I need to convert fields that are undefined to null in the updatedData object before using Prisma to update the object. I've read this https://www.prisma.io/docs/orm/prisma-client/special-fields-and-types/null-and-undefined which indicates that undefined should be avoided in Primsa.
10 replies
WWasp
Created by 4jandals on 4/4/2025 in #đŸ™‹questions
Is there a recommended way to add computed fields to a model?
For example, let's say I have a Person entity with a dateOfBirth, and I want to derive age in the model so I can reuse that across the UI. I think Prisma client supports compute, but not sure how to use that in Wasp? Or maybe there is a better way?
6 replies