How rphlmr/drizzle-supabase-rls works
Project Type and Purpose
• This is a full-stack, real-time chat application built with Next.js 13 (App Router) and React on the frontend, Node.js on the backend, Supabase for authentication and realtime messaging, and Drizzle-ORM for schema management and RLS (Row Level Security) enforcement.
• Users log in via Supabase Auth, create or join chat rooms, and send ephemeral messages in authorized channels guarded by RLS policies.
File Structure Overview
• app/
• Next.js routes and layouts (login, auth callbacks, protected chat page, global CSS).
• components/
• Reusable UI (SubmitButton, SupabaseLogo).
• utils/supabase/
• Supabase client setup for browser, server, and middleware.
• database/
• Drizzle schema definitions (schema.ts), migrations, db/drizzle client factories (db.ts, drizzle.ts), JWT helper.
• supabase/
• Local Supabase config (config.toml, seed.sql) for Docker-based local dev and RLS client creation.
• middleware.ts
• Next.js middleware for enforcing auth.
• Configs
• next.config.js, tailwind.config.js, tsconfig.json, turbo.json, etc.
Key Architectural Patterns
• Separation of Concerns
• Frontend (React/Next.js client components) vs. Backend (Next.js server components, API routes, Drizzle ORM).
• Database as a Service + ORM
• Supabase provides Auth, Realtime, and Postgres hosting; Drizzle-ORM manages schema, migrations, and wraps Supabase connections to enforce RLS.
• Event-Driven Real-Time Messaging
• Supabase Realtime channels with presence and broadcast, scoped per room by RLS.
• Security Boundary: RLS
• All protected queries run through db.rls(...), which under the hood sets JWT claims and user role before executing SQL.
Main Components
a. Browser Client
– Next.js React pages/components
– Supabase JS client for Auth and Realtime (channels)
b. Next.js Server
– Server Components & Server Actions (app/ pages, actions.ts)
– createDrizzleSupabaseClient factory → returns { admin, client } Drizzle instances
– Middleware layer enforcing session/cookie auth
c. Supabase Service
– Auth (JWT generation, Hooks)
– Realtime (Pub/Sub engine for channels)
– Postgres hosting
d. PostgreSQL Database
– Tables: profiles, rooms, rooms_users, plus realtime.* tables
– Triggers and functions (e.g., insert_user() for profiles)
– RLS Policies on tables and external real-time tables
e. Local Dev Infrastructure
– Supabase CLI Docker containers
– Config files (supabase/config.toml, supabase/seed.sql)
Relationships and Data Flows
• User → Browser: logs in via Supabase Auth UI → receives JWT
• Browser → Supabase Realtime: subscribes to channel room-<topic> via supabase.channel(...)
• Browser → Next.js Server: fetches initial data/server actions; sends /invite, sendMessage via server functions
• Next.js Server → Drizzle-ORM:
• admin instance for schema setup and profiles inserts
• client.rls instance for all read/write operations subject to RLS
• Drizzle-ORM → Postgres: executes SQL with configured JWT claims and role
• Postgres → Realtime Engine: triggers broadcast/presence events to subscribed clients
• Realtime Engine → Browser: pushes updates to authorized users only