⚡ Supabase1 / 1
Setup & CRUD Operations
📌 WHAT
Supabase is an open-source Firebase alternative built on PostgreSQL. It provides a database, authentication, file storage, and real-time subscriptions with a clean JavaScript client.
💡 WHY
Get a full backend (auth, database, storage) without building a server. Perfect for Next.js apps. The client API is clean and TypeScript-native.
⏰ WHEN / WHERE
Building full-stack apps without a dedicated backend. Prototyping, startups, and production apps that don't need custom backend logic.
🔧 HOW
Create project at supabase.com → get URL and anon key → install client → use typed client to query your database.
Code Examples
#1Supabase setup and CRUD
Supabase setup and CRUD
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 1. Install: npm install @supabase/supabase-js// 2. Add to .env.local:// NEXT_PUBLIC_SUPABASE_URL=your-project-url// NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key// lib/supabase.tsimport { createClient } from "@supabase/supabase-js";// Type-safe: generate types from your schema// npx supabase gen types typescript --project-id your-id > types/database.tsexport const supabase = createClient(process.env.NEXT_PUBLIC_SUPABASE_URL!,process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!);// ============================================// CRUD OPERATIONS// ============================================// READ — select with filtersconst { data: users, error } = await supabase.from("users").select("id, name, email") // specific columns (or "*" for all).eq("role", "admin") // WHERE role = 'admin'.order("created_at", { ascending: false }).limit(10);// SELECT with JOIN (Supabase handles relationships)const { data: posts } = await supabase.from("posts").select(`id, title, content,users (id, name, email)`); // auto-joins via foreign key!// CREATEconst { data: newUser, error: createError } = await supabase.from("users").insert({ name: "Ali", email: "ali@example.com", role: "user" }).select() // return the created row.single(); // return object instead of array// UPDATEconst { data: updated } = await supabase.from("users").update({ role: "admin" }).eq("id", 1).select().single();// UPSERT — insert if not exists, update if existsawait supabase.from("profiles").upsert({user_id: "uuid-here",bio: "Updated bio",});// DELETEconst { error: deleteError } = await supabase.from("users").delete().eq("id", 1);// ALWAYS check errors!if (error) console.error("Supabase error:", error.message);
#2Supabase Auth
Supabase Auth
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { supabase } from "@/lib/supabase";// ============================================// AUTHENTICATION// ============================================// Sign up with email/passwordconst { data, error } = await supabase.auth.signUp({email: "ali@example.com",password: "supersecret123",options: {data: { // extra profile dataname: "Ali Ahmed",role: "user",},},});// Sign inconst { data: session, error: loginError } = await supabase.auth.signInWithPassword({email: "ali@example.com",password: "supersecret123",});// Sign in with Google (OAuth)await supabase.auth.signInWithOAuth({provider: "google",options: {redirectTo: `${window.location.origin}/auth/callback`,},});// Get current user (client-side)const { data: { user } } = await supabase.auth.getUser();console.log(user?.id, user?.email);// Listen for auth state changesconst { data: { subscription } } = supabase.auth.onAuthStateChange((event, session) => {if (event === "SIGNED_IN") console.log("Logged in:", session?.user);if (event === "SIGNED_OUT") console.log("Logged out");});// Sign outawait supabase.auth.signOut();// Reset passwordawait supabase.auth.resetPasswordForEmail("ali@example.com", {redirectTo: "https://yourapp.com/reset-password",});
Practice This Concept
Open the playground, paste any example, and experiment. The best way to learn is by breaking things.
Open in Playground