How can we save chat Messages in DB #32

Open
opened 2026-02-16 00:15:09 -05:00 by yindo · 1 comment
Owner

Originally created by @Good-first-Developer on GitHub (Sep 1, 2024).

Originally created by @Good-first-Developer on GitHub (Sep 1, 2024).
Author
Owner

@nitsaw09 commented on GitHub (Jun 23, 2025):

Yes you can save the chat message in db has supabase itself uses postgresql for storing the data in database sample code for implementation is like creating a route api in next.js

`import { NextResponse } from 'next/server';
import { supabase } from '@/lib/supabase';
import { generateAIResponse } from '@/lib/langchain';

export async function POST(req: Request) {
const { chatId, userMessage } = await req.json();

// Store user message
await supabase.from('messages').insert([
{ chat_id: chatId, sender: 'user', message: userMessage }
]);

const aiResponse = await generateAIResponse(userMessage);

// Store AI response
await supabase.from('messages').insert([
{ chat_id: chatId, sender: 'ai', message: aiResponse }
]);

return NextResponse.json({ aiResponse });
}
`

@nitsaw09 commented on GitHub (Jun 23, 2025): Yes you can save the chat message in db has supabase itself uses postgresql for storing the data in database sample code for implementation is like creating a route api in next.js `import { NextResponse } from 'next/server'; import { supabase } from '@/lib/supabase'; import { generateAIResponse } from '@/lib/langchain'; export async function POST(req: Request) { const { chatId, userMessage } = await req.json(); // Store user message await supabase.from('messages').insert([ { chat_id: chatId, sender: 'user', message: userMessage } ]); const aiResponse = await generateAIResponse(userMessage); // Store AI response await supabase.from('messages').insert([ { chat_id: chatId, sender: 'ai', message: aiResponse } ]); return NextResponse.json({ aiResponse }); } `
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langchain-nextjs-template#32