Cursorcursorintermediate

Add Supabase Database to Next.js (Cursor Prompt)

Cursor prompt to add Supabase database client, generated types, query utilities, and RLS policies.

What you'll get

Supabase database integration with typed clients, query functions, generated types, and RLS policies.

The Prompt

Add Supabase database integration to this Next.js project.

FILES TO CREATE:
- src/lib/supabase/client.ts — Browser client (if not already created)
- src/lib/supabase/server.ts — Server client with cookies (if not already created)
- src/lib/supabase/admin.ts — Admin client using SERVICE_ROLE_KEY for server-only operations
- src/types/database.ts — Generated types from Supabase CLI (run: npx supabase gen types typescript --project-id YOUR_PROJECT_ID > src/types/database.ts)
- src/lib/queries/[resource].ts — Query functions per resource (e.g., getUsers, createPost, updatePost, deletePost)

QUERY PATTERN:
Every query function should:
1. Accept typed parameters
2. Use the typed Supabase client: supabase.from('table').select('*').returns<Row[]>()
3. Return { data, error } and handle errors with a consistent pattern
4. Be used in Server Components or Server Actions, NOT in client components directly

RLS POLICIES TO CREATE:
- Users can read their own data: auth.uid() = user_id
- Users can insert their own data: auth.uid() = user_id
- Users can update their own data: auth.uid() = user_id
- Users can delete their own data: auth.uid() = user_id
- Public read access where appropriate

DO NOT:
- Use the admin client in client-side code
- Skip RLS policies — every table must have RLS enabled
- Use raw SQL queries — use the Supabase query builder

ENVIRONMENT VARIABLES:
- NEXT_PUBLIC_SUPABASE_URL
- NEXT_PUBLIC_SUPABASE_ANON_KEY
- SUPABASE_SERVICE_ROLE_KEY (server only, never expose to client)

Replace these variables

VariableReplace with
[PROJECT_ID]Supabase project ID for type generation
[TABLES]Tables to create query functions for

Tips for best results

Regenerate types after every schema change: npx supabase gen types typescript --project-id YOUR_ID > src/types/database.ts

Use the admin client only in API routes and Server Actions — never expose the service role key.

Follow-up prompts

Add database migrations

Set up Supabase migrations with 'npx supabase migration new' for version-controlled schema changes.

Related prompts