How nicolasmontone/chat-with-your-base works
Project type and purpose
• This is a full-stack, server-rendered Next.js application (using the new app directory) that provides an AI-driven chat interface to inspect and query a Postgres database via Supabase.
• Key use cases:
• Authenticate users via Supabase Auth
• Generate database statistics and SQL via OpenAI
• Execute SQL statements against a Postgres DB
File structure
• app/… contains both page routes (React Server Components and Client Components) and API route handlers (e.g., app/api/chat/route.ts).
• actions/… are Next.js Server Actions invoked directly from client components (chart.ts, run-sql.ts, get-chats.ts, etc.).
• utils/supabase/… wraps Supabase client and middleware for server-side queries and auth enforcement.
• components/… houses UI building blocks (shadcn-powered, Tailwind CSS) and domain components (chat interface, SQL result, charts).
• lib/… holds helper functions (chart formatting, Recharts adapters, general utilities).
• middleware.ts enforces auth and redirects.
• state/index.ts and hooks/… implement client-side state (Zustand or React context) and custom hooks.
• Top-level config files (next.config.js, tsconfig.json, tailwind.config.ts) define build and styling pipelines.
README for additional insights
• The stack: Next.js, Supabase (DB + Auth), OpenAI, Vercel, Tailwind, shadcn UI.
Main components and their relationships
a. Frontend (Browser)
• Next.js React pages & components
• Client Components invoke Server Actions or fetch API routes
b. Server layer (hosted on Vercel)
• Next.js Server Components & API route handlers (app/api/…)
• Server Actions (actions/*.ts) for chart data, SQL execution, chat history
• Supabase middleware for auth/session injection
c. Database & Auth
• Supabase Postgres instance (NEXT_PUBLIC_SUPABASE_URL + ANON_KEY)
• Supabase Auth callbacks (app/api/auth/callback/route.ts)
d. AI Service
• OpenAI API (OPENAI_API_KEY) used by chat and SQL-generation endpoints
e. Client-side state & utilities
• Zustand or context via state/index.ts
• Custom hooks (use-toast, use-app-local-storage, etc.)
Architectural patterns
• Jamstack/SSR hybrid: UI delivered by Next.js, with server-side logic via API routes and Server Actions.
• “Backend for Frontend”: all AI and DB calls are funneled through Next.js server logic, never from the client directly.
• Separation of concerns: UI components, data-fetch actions, DB client wrappers, and utility libs are modularized.