[Convex Ents] patch not working + patch has not correct typescript definitions

I wrote this code: messages.ts
export const deleteMessage = mutation({
args: { messageId: v.string() },
handler: async (ctx, args) => {
const parsedMessageId = ctx.table("messages").normalizeId(args.messageId);

if (!parsedMessageId) {
throw new ConvexError("chatId was invalid");
}

console.log(args.messageId);

(await ctx.table("messages").getX(parsedMessageId)).patch({
content: undefined,
deleted: true,
});
},
});
export const deleteMessage = mutation({
args: { messageId: v.string() },
handler: async (ctx, args) => {
const parsedMessageId = ctx.table("messages").normalizeId(args.messageId);

if (!parsedMessageId) {
throw new ConvexError("chatId was invalid");
}

console.log(args.messageId);

(await ctx.table("messages").getX(parsedMessageId)).patch({
content: undefined,
deleted: true,
});
},
});
schema.ts
messages: defineEnt({})
.field("content", v.string())
.field("deleted", v.boolean(), { default: false })
.edge("privateChat")
.edge("user"),
messages: defineEnt({})
.field("content", v.string())
.field("deleted", v.boolean(), { default: false })
.edge("privateChat")
.edge("user"),
And as you can see in the image the mutation is called correctly. But message does not get patched.
No description
No description
12 Replies
FleetAdmiralJakob 🗕 🗗 🗙
if you need to you can go into my db. it's this project
No description
lee
lee•16mo ago
i don't see why this is happening, but could it be because content should be v.optional(v.string()) if you're trying to set it to undefined? (if that's the problem, i would expect patch to throw a schema error)
FleetAdmiralJakob 🗕 🗗 🗙
Yeah, weird. Should I create another support thread covering that it's not throwing an error?
lee
lee•16mo ago
nah we can discuss that here. i'll look into it does it throw the error if you await the patch?
FleetAdmiralJakob 🗕 🗗 🗙
I will check that. But usually typescript should tell me if I can't pass an undefined
FleetAdmiralJakob 🗕 🗗 🗙
With this code it throws the error
export const deleteMessage = mutation({
args: { messageId: v.string() },
handler: async (ctx, args) => {
const parsedMessageId = ctx.table("messages").normalizeId(args.messageId);

if (!parsedMessageId) {
throw new ConvexError("chatId was invalid");
}

console.log(args.messageId);

await (
await ctx.table("messages").getX(parsedMessageId)
).patch({
content: undefined,
deleted: true,
});
},
});
export const deleteMessage = mutation({
args: { messageId: v.string() },
handler: async (ctx, args) => {
const parsedMessageId = ctx.table("messages").normalizeId(args.messageId);

if (!parsedMessageId) {
throw new ConvexError("chatId was invalid");
}

console.log(args.messageId);

await (
await ctx.table("messages").getX(parsedMessageId)
).patch({
content: undefined,
deleted: true,
});
},
});
No description
FleetAdmiralJakob 🗕 🗗 🗙
But would be nice if TS warned me 😂. I will check if it works with content: "" This code worked!!!
export const deleteMessage = mutation({
args: { messageId: v.string() },
handler: async (ctx, args) => {
const parsedMessageId = ctx.table("messages").normalizeId(args.messageId);

if (!parsedMessageId) {
throw new ConvexError("chatId was invalid");
}

console.log(args.messageId);

await (
await ctx.table("messages").getX(parsedMessageId)
).patch({
content: "",
deleted: true,
});
},
});
export const deleteMessage = mutation({
args: { messageId: v.string() },
handler: async (ctx, args) => {
const parsedMessageId = ctx.table("messages").normalizeId(args.messageId);

if (!parsedMessageId) {
throw new ConvexError("chatId was invalid");
}

console.log(args.messageId);

await (
await ctx.table("messages").getX(parsedMessageId)
).patch({
content: "",
deleted: true,
});
},
});
I think I was missing the second (outer) await Should I create an issue for that (the typing) on the package's github or is it fine here?
Michal Srb
Michal Srb•16mo ago
What's the typing bug exactly? You can explain here. That content was allowed to be undefined in patch?
Michal Srb
Michal Srb•16mo ago
This is not a bug in Ents, but a limitation of the Convex TS types. patch accepts a subset of fields of the document. But TypeScript does not distinguish between not providing a field and providing a field with the value undefined. You can see that the same issue exists in Convex without Ents.
lee
lee•16mo ago
the reason why the error wasn't thrown at runtime either: https://github.com/xixixao/convex-ents/issues/26
GitHub
unawaited patch never executes · Issue #26 · xixixao/convex-ents
promises in convex mutations usually run to completion, even if not awaited. for example, handler: async (ctx, { id }) => { ctx.db.patch(id, { foo: bar }); } will execute the patch. But this isn...

Did you find this page helpful?