mirror of
https://github.com/langchain-ai/langchain-nextjs-template.git
synced 2026-07-20 04:43:31 -04:00
How can we save chat Messages in DB #32
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Good-first-Developer on GitHub (Sep 1, 2024).
@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 });
}
`