Convex Ents with Optimistic Updates

I'm currently trying to implement a optimistic update together with Convex Ents. The problem is that in order to set a new entry optimistically I need many related information as seen in the example. What is the best way to get this information or to work around that?
No description
3 Replies
FleetAdmiralJakob 🗕 🗗 🗙
Ok, I've come so far:
const newMessage: Ent<"messages"> = {
_id: crypto.randomUUID() as Id<"messages">,
userId: userInfo._id,
_creationTime: now,
content,
deleted: false,
privateChatId: chatId,
};
localStore.setQuery(api.messages.getMessages, { chatId }, [
...existingMessages,
newMessage,
]);
const newMessage: Ent<"messages"> = {
_id: crypto.randomUUID() as Id<"messages">,
userId: userInfo._id,
_creationTime: now,
content,
deleted: false,
privateChatId: chatId,
};
localStore.setQuery(api.messages.getMessages, { chatId }, [
...existingMessages,
newMessage,
]);
but
TS2322: Type Ent<"messages"> is not assignable to type
{ userId: undefined; from: Ent<"users", { _id: Id<"users">; _creationTime: number; clerkId: string; username: string; firstName?: string | undefined; lastName?: string | undefined; }, EntDataModelFromSchema<...>>; ... 5 more ...; privateChatId: Id<...>; }
TS2322: Type Ent<"messages"> is not assignable to type
{ userId: undefined; from: Ent<"users", { _id: Id<"users">; _creationTime: number; clerkId: string; username: string; firstName?: string | undefined; lastName?: string | undefined; }, EntDataModelFromSchema<...>>; ... 5 more ...; privateChatId: Id<...>; }
Michal Srb
Michal Srb•7mo ago
What happens if you remove the type annotation from the first line?
FleetAdmiralJakob 🗕 🗗 🗙
Then I get this error:
TS2322: Type
{ _id: Id<"messages">; userId: Id<"users">; _creationTime: number; content: string; deleted: boolean; privateChatId: Id<"privateChats">; }
is not assignable to type
{ userId: undefined; from: Ent<"users", { _id: Id<"users">; _creationTime: number; clerkId: string; username: string; firstName?: string | undefined; lastName?: string | undefined; }, EntDataModelFromSchema<...>>; ... 5 more ...; privateChatId: Id<...>; }
TS2322: Type
{ _id: Id<"messages">; userId: Id<"users">; _creationTime: number; content: string; deleted: boolean; privateChatId: Id<"privateChats">; }
is not assignable to type
{ userId: undefined; from: Ent<"users", { _id: Id<"users">; _creationTime: number; clerkId: string; username: string; firstName?: string | undefined; lastName?: string | undefined; }, EntDataModelFromSchema<...>>; ... 5 more ...; privateChatId: Id<...>; }
The problem is that I need a type that the setQuery accepts but idk how to grab this type. ah nvm i think the correct type is:
FunctionReturnType<typeof api.messages.getMessages>[number]
FunctionReturnType<typeof api.messages.getMessages>[number]
thank you