I understand Convex can't do much about
I understand Convex can't do much about forms using
"" to represent blank values, but the way Convex requires null to represent blank values on the API, and the undefined to represent them in db.patch is creating a lot of boilerplate code (including lot's of types) on what feels like should be a tiny app (https://github.com/pupeno/convex-nextjs-playground). Does anybody have any advice on how to make this less messy?13 Replies
Null is valid in the db, you can use it in patch.
optionalNumber is defined as v.optional(v.number()) in https://github.com/pupeno/convex-nextjs-playground/blob/a934f5370070725ad787b86c1e735b9b8ae763c5/convex/schema.ts#L9
If I call patch this way:
(replacing the value in this line: https://github.com/pupeno/convex-nextjs-playground/blob/a934f5370070725ad787b86c1e735b9b8ae763c5/convex/admin/sets.ts#L126)
I get this type error:
You have to use a union with null.
Or use the nullabale helper.
@Isak but I don't want the field to be null. I want the field to be optional, which it already is.
What do you mean by nullable helper?
It's from the convex-helpers package.
You can unset the field with undefined.
But if you want to use null you have to use the helper or create the union yourself.
I don't want to use null. I have to use null because Convex silently drops
undefined from the API, so those get lost.
All I want to achieve is blanking of a field from a form to the database.If you want to unset an optional field patch it with undefined.
That is what I'm doing.
The form generates "", which then gets converted to null, which then gets converted to undefined, so I end up with 3 definitions of a Set (4 if you count the database schema). It just feels quite convoluted, verbose, boiler platy for something so simple.
Data Types | Convex Developer Hub
Supported data types in Convex documents
I guess you have to do the conversions.
Yeah, I read that, what are you pointing me to?
Yeah, that's what I wanted to confirm with a Convex expert. I'm just a noob coming from environments where this would be a lot less code, so it felt like I was doing something wrong.
I'm no convex expert either just wanted to share my knowledge.
Thank you. I appreciate the help. You mentioned something about convex-helpers. Was there something in particular you were thinking of from that package?