Skip to content

Commit 864711c

Browse files
committed
docs(ai-chat): upsert the hydrate row in the persistence-and-replay example
Same fragile update-against-a-possibly-missing-row pattern the other hydrate examples already moved away from.
1 parent d89c04d commit 864711c

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

docs/ai-chat/patterns/persistence-and-replay.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,13 @@ export const myChat = chat.agent({
142142
// See lifecycle-hooks for the full upsert pattern + rationale:
143143
// /ai-chat/lifecycle-hooks#hydratemessages
144144
if (upsertIncomingMessage(stored, { trigger, incomingMessages })) {
145-
await db.chat.update({ where: { id: chatId }, data: { messages: stored } });
145+
// Upsert, not update: head-start first turns run without a preload
146+
// to create the row.
147+
await db.chat.upsert({
148+
where: { id: chatId },
149+
create: { id: chatId, messages: stored },
150+
update: { messages: stored },
151+
});
146152
}
147153

148154
return stored;

0 commit comments

Comments
 (0)