Pablo
Pablo2w ago

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
erquhart
erquhart2w ago
Null is valid in the db, you can use it in patch.
Pablo
PabloOP2w ago
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:
await ctx.db.patch(id, {optionalNumber: null});
await ctx.db.patch(id, {optionalNumber: null});
(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:
convex/admin/sets.ts:126:29 - error TS2322: Type 'null' is not assignable to type 'number | undefined'.

126 await ctx.db.patch(id, {optionalNumber: null});
~~~~~~~~~~~~~~

node_modules/convex/dist/esm-types/server/database.d.ts:210:22
210 type PatchValue<T> = {
~
211 [P in keyof T]?: undefined extends T[P] ? T[P] | undefined : T[P];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
212 };
~
The expected type comes from property 'optionalNumber' which is declared here on type 'PatchValue<{ _id: Id<"sets">; _creationTime: number; optionalNumber?: number | undefined; optionalPositiveNumber?: number | undefined; name: string; mandatoryNumber: number; uniqueNumber: number; }>'


Found 1 error in convex/admin/sets.ts:126
convex/admin/sets.ts:126:29 - error TS2322: Type 'null' is not assignable to type 'number | undefined'.

126 await ctx.db.patch(id, {optionalNumber: null});
~~~~~~~~~~~~~~

node_modules/convex/dist/esm-types/server/database.d.ts:210:22
210 type PatchValue<T> = {
~
211 [P in keyof T]?: undefined extends T[P] ? T[P] | undefined : T[P];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
212 };
~
The expected type comes from property 'optionalNumber' which is declared here on type 'PatchValue<{ _id: Id<"sets">; _creationTime: number; optionalNumber?: number | undefined; optionalPositiveNumber?: number | undefined; name: string; mandatoryNumber: number; uniqueNumber: number; }>'


Found 1 error in convex/admin/sets.ts:126
Isak
Isak2w ago
You have to use a union with null. Or use the nullabale helper.
Pablo
PabloOP2w ago
@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?
Isak
Isak2w ago
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.
Pablo
PabloOP2w ago
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.
Isak
Isak2w ago
If you want to unset an optional field patch it with undefined.
Pablo
PabloOP2w ago
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.
Isak
Isak2w ago
I guess you have to do the conversions.
Pablo
PabloOP2w ago
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.
Isak
Isak2w ago
I'm no convex expert either just wanted to share my knowledge.
Pablo
PabloOP2w ago
Thank you. I appreciate the help. You mentioned something about convex-helpers. Was there something in particular you were thinking of from that package?

Did you find this page helpful?