Supabase + Next.js Onboarding Guide
Mục tiêu của bộ tài liệu:
- Giúp dev mới hiểu Supabase nhanh, có khả năng tự build MVP trong vài ngày.
- Làm chuẩn nội bộ để scale đội dev, dễ dàng onboard người mới hoặc so sánh effort với backend truyền thống.
- Làm cơ sở cho future best-practices (batch job, auth, RLS, CI/CD,...).
🏁 Phần 0. Giới thiệu & Tổng quan
Mục tiêu của tài liệu
- Mục tiêu training (learning outcomes)
- Đối tượng (dev frontend, fullstack, backend, intern,...)
- Cách đọc tài liệu: theo tuần / theo module
Kiến trúc tổng quan Supabase + Next.js
Diagram: Next.js (App Router) ↔ Supabase (DB + Auth + Storage + Functions)
Giải thích các thành phần chính:
- Database (Postgres)
- Auth
- Storage
- Edge Functions
- Cron & Queue
- Realtime
So sánh nhanh với backend truyền thống (NestJS, Express, Postgres, S3,...)
Case study xuyên suốt (Sample App)
- Giới thiệu app mẫu (ví dụ: "Subscription Manager")
- Các tính năng sẽ triển khai: Auth, CRUD, Batch Job, Upload, Cron
- Cấu trúc thư mục, flow học tập theo từng module
🧱 Phần 1. Cài đặt & Chuẩn bị môi trường
Cài đặt công cụ
- Node.js / pnpm / npm
- Supabase CLI (
supabase login,supabase start,...) - VSCode plugins (SQLTools, Supabase, etc.)
Khởi tạo project
npx create-next-appnpx supabase init- Cấu hình
.env(Supabase URL, Anon Key) - Kết nối thử với Supabase SDK
Chạy Supabase local
supabase start, truy cập http://localhost:54323- Tạo user, test Auth
- Mô phỏng local dev environment hoàn chỉnh
Cấu trúc thư mục đề xuất
/app- Next.js App Router/lib- Supabase client, hooks/supabase- migrations, policies, seeds/scripts- batch jobs, automation
🔐 Phần 2. Authentication
Kiến trúc Auth của Supabase
- Giải thích cơ chế JWT,
auth.uid() - Anon key vs Service role key
- Sign in/out flow
- Giải thích cơ chế JWT,
Triển khai Auth với Next.js
@supabase/supabase-js&@supabase/ssrTạo Supabase client cho:
- Client-side (browser)
- Server-side (SSR)
Middleware bảo vệ routes (
middleware.ts)
Các loại đăng nhập
- Email/password
- Magic link
- OAuth (Google, GitHub)
- OTP (SMS/Email OTP nếu cần)
Quản lý session
- Lưu session vào cookie (server component)
- Refresh token
- Logout / Revalidate session
Tùy biến Auth
- Custom user metadata
- Hooks
auth.users→profiletable sync - Best practice: sync user profile bằng trigger hoặc Edge Function
🔑 Phần 3. Authorization (RLS & Policy)
Giới thiệu RLS (Row-Level Security)
- RLS là gì và vì sao Supabase "an toàn mặc định"
- Cấu hình RLS trên table
Viết Policy cơ bản
- SELECT / INSERT / UPDATE / DELETE
- Sử dụng
auth.uid() - Ví dụ: "User chỉ xem được task của chính mình"
Tổ chức Role & Permission
- Mapping role vào JWT claim
- Multi-tenant design:
organization_id - Admin / Owner / Member policy
Best Practices
- "Always RLS On"
- Test policy trước khi code (SQL playground)
- Quy ước viết file policy.sql / migration.sql
💾 Phần 4. Database & Migrations
Thiết kế schema
- Ví dụ bảng
users,organizations,memberships,tasks - Sử dụng enum, index, constraint
- Naming convention
- Ví dụ bảng
Migrations với Supabase CLI
supabase db pushsupabase migration new add_tasks_table- Quản lý migration file, version control
Seeding & data mẫu
supabase db seed- Script tạo dữ liệu cho môi trường dev/test
Query & Performance
- RPC function
- Caching (Next.js revalidate tag)
- Dùng
explain analyzeđể kiểm tra query
🌐 Phần 5. Supabase + Next.js Integration
Next.js App Router với Supabase
- Cách gọi Supabase từ server component
- Dùng Server Action để CRUD
- Protect route (redirect nếu chưa đăng nhập)
Realtime Subscription
- Realtime setup (
supabase.channel) - Cập nhật UI khi có thay đổi DB
- Ví dụ: Task board realtime
- Realtime setup (
Storage (Upload & Access Control)
- Tạo bucket, config policy
- Upload file, lấy signed URL
- Bảo mật: user chỉ xem được file của mình
Edge Functions
- Khi nào nên dùng
- Viết Edge Function (
supabase functions new send-email) - Test và deploy Edge Function
- Gọi Edge Function từ Next.js (client/server)
⏰ Phần 6. Batch Job & Background Tasks
Batch job với pg_cron
- Cài extension
pg_cron - Tạo job định kỳ (cleanup, sync, aggregate)
- Best practice cho cron trong DB
- Cài extension
Edge Function + Cron Scheduler
- Tạo Edge Function và đăng ký Cron job
- Use case: gửi email reminder
Queues (pgmq)
- Cài extension pgmq
- Push / Consume job
- Pattern: async processing trong Supabase
So sánh Cron vs Queue
- Cron → định kỳ
- Queue → theo sự kiện (event-driven)
Monitoring job
- Logs trong Supabase dashboard
- Retry / failure tracking
🧩 Phần 7. API & Integration Patterns
Direct Query vs API Proxy
- Khi nào FE gọi Supabase trực tiếp
- Khi nào nên có lớp API trung gian (Route Handler / Function)
Webhooks
- Tạo DB trigger → gọi Edge Function
- Ví dụ: khi user đăng ký → gửi email chào mừng
Integration bên thứ ba
- Stripe / GitHub / Slack webhook
- Best practice lưu audit log
⚙️ Phần 8. CI/CD & DevOps
Supabase CLI trong pipeline
- Login bằng token
- Run migration, test, deploy function
- Ví dụ YAML GitHub Actions
Environment management
- Dev / Staging / Prod setup
- Quy ước env key & secrets
- Sync schema giữa các môi trường
Deploy Next.js
- Vercel / Cloudflare / Render
- Kết nối với Supabase project
- Zero-downtime deployment
Testing
- Unit test Next.js logic
- Integration test với Supabase local
- Mock Supabase trong Jest
🔍 Phần 9. Observability & Debugging
Logs
- Database logs
- Edge Function logs
- Cron / Queue logs
Tracing request
- Gắn
request_idxuyên suốt - Tracking error
- Gắn
Monitoring
- Alert email / Slack khi job fail
- Metrics cơ bản (API latency, job duration,...)
🔒 Phần 10. Security Best Practices
API key & secret
- Quản lý Anon key / Service role key
- Không bao giờ expose service key lên client
RLS bắt buộc
- Checklist RLS khi tạo bảng mới
- Cách test policy nhanh
Rate limiting & abuse
- Dùng Edge Function middleware
- Logging và detect spam
💰 Phần 11. Cost & Performance Optimization
Tối ưu chi phí
- Cấu hình Supabase plan hợp lý
- Sử dụng Storage & Realtime tiết kiệm
Tối ưu performance
- Index / query tuning
- Cache layer trong Next.js
- Khi nào nên chuyển logic sang Edge Function
Benchmark
- So sánh performance CRUD Supabase vs API backend truyền thống
📊 Phần 12. So sánh với Techstack truyền thống
| Hạng mục | Supabase + Next.js | Backend truyền thống |
|---|---|---|
| Auth | Built-in | Phải code + DB |
| Storage | Built-in | Tự config S3 |
| Cron / Batch | pg_cron / Edge Function | Bull / Cloud Tasks |
| Queue | pgmq | Redis / SQS |
| Realtime | Có sẵn | WebSocket / Socket.IO |
| DevOps | CLI + Dashboard | Tự setup infra |
| Cost khởi điểm | Rất thấp | Trung bình - cao |
| Scaling lớn | Giới hạn vendor | Tự do control |
🧠 Phần 13. Phụ lục (Advanced)
Multi-project / Monorepo
- Tích hợp nhiều Supabase project
- Tách environment
Custom Postgres extension
- Dùng
pgvector,postgis,citext,pg_partman
- Dùng
Analytics & AI integration
- Sử dụng Supabase Vector
- Query embedding / RAG pattern
Troubleshooting
- Các lỗi thường gặp (RLS deny, token expired,...)
- Checklist debug
📎 Phần 14. Tài liệu tham khảo
Supabase Docs (database, auth, cron, queue, cli, function)
Next.js App Router Docs
Example repo (Supabase + Next.js Starter)
Nội bộ:
supabase_onboarding.mdsupabase_best_practices.sqlgithub-actions-supabase.yml