Database Prompts
Prompts for database schema design, queries, and backend operations. Works with both Supabase (PostgreSQL) and Convex.Schema Design
Design a Table
- Supabase
- Convex
Design a database table for [feature/entity].Requirements:
- [List what data needs to be stored]
- [Any relationships to other tables]
- [Any constraints or validations]
- CREATE TABLE statement
- Row Level Security policies (users can only access their own data)
- Any necessary indexes
- TypeScript interface matching the schema
Design Related Tables
- Supabase
- Convex
Design a database schema for [feature] with these entities:
- Relationship: [describe relationship]
- All CREATE TABLE statements
- Foreign key relationships
- RLS policies for each table
- TypeScript interfaces
- Example queries for common operations
Add Column to Existing Table
- Supabase
- Convex
I need to add a new column to the [table_name] table:
- Column name: [name]
- Type: [type]
- Nullable: [yes/no]
- Default: [value or none]
- ALTER TABLE statement
- Update to TypeScript interface
- Migration to backfill existing rows (if needed)
Security
Supabase RLS Policy
Create Row Level Security policies for the [table_name] table.Rules:
- Users can only see their own rows
- Users can create rows for themselves
- Users can update their own rows
- Users can delete their own rows
user_id column that references auth.users.Convex Auth Check
Add authentication checks to the [function_name] query/mutation.Requirements:
- Only authenticated users can access
- Users can only access their own data
- Return appropriate error for unauthorized access
ctx.auth.getUserIdentity() to check auth.Role-Based Access
- Supabase
- Convex
Create RLS policies for [table_name] with role-based access:Roles:
- Admin: Can see and modify all rows
- User: Can only see/modify their own rows
- Guest: Read-only access to public rows
user_roles table with user_id and role columns.CRUD Operations
Create Service Functions
- Supabase
- Convex
Create TypeScript service functions for the [table_name] table.Operations needed:
- getAll() - Fetch all user’s records
- getById(id) - Fetch single record
- create(data) - Create new record
- update(id, data) - Update record
- delete(id) - Delete record
- Use the Supabase client from
@/services/supabase - Include proper TypeScript types
- Handle errors appropriately
- Return typed data
app/services/[name]Service.tsFetch with Filters
- Supabase
- Convex
Create a function to fetch [items] from Supabase with filters:Filters needed:
- Search by: [fields]
- Sort by: [field] ascending/descending
- Pagination: page, pageSize
Fetch with Relationships
- Supabase
- Convex
Create a function to fetch [items] with related data:Main table: [table_name]
Related data:
- [related_table] via [foreign_key]
- [another_table] via [foreign_key]
Real-time Subscriptions
Subscribe to Changes
- Supabase
- Convex
Create a real-time subscription for changes to [table_name].When a record is:
- Inserted: [what should happen]
- Updated: [what should happen]
- Deleted: [what should happen]
- Subscription setup function
- Cleanup function for unmount
- TypeScript types for payload
- Usage example in a component
Live Query Hook
- Supabase
- Convex
Create a React hook that provides real-time data from [table_name].Features:
- Initial data fetch
- Real-time updates via subscription
- Loading and error states
- Automatic cleanup on unmount
const { data, loading, error } = useLive[Items]()Migrations
Create Migration
- Supabase
- Convex
Create a Supabase migration for:
[Describe the change]Include:
- The migration SQL
- A descriptive migration name
- Any seed data if needed
- Rollback SQL (if possible)
Performance
Add Indexes
- Supabase
- Convex
Suggest indexes for the [table_name] table to optimize these queries:
- [Describe query 1]
- [Describe query 2]
- [Describe query 3]
Common Patterns
Soft Delete
- Supabase
- Convex
Implement soft delete for [table_name]:
- Add
deleted_attimestamp column - Update RLS to filter deleted rows by default
- Create service functions:
- softDelete(id)
- restore(id)
- getDeleted()
- permanentDelete(id)
Timestamps
- Supabase
- Convex
Add automatic timestamp handling to [table_name]:
created_at: Set on insertupdated_at: Set on insert and update
- Column definitions with defaults
- Trigger for auto-updating
updated_at - TypeScript interface updates
Quick Reference
Supabase
| Operation | Method |
|---|---|
| Select all | .from('table').select('*') |
| Select with filter | .eq('column', value) |
| Select single | .single() |
| Insert | .insert(data) |
| Update | .update(data).eq('id', id) |
| Delete | .delete().eq('id', id) |
| Upsert | .upsert(data) |
| Count | .select('*', { count: 'exact' }) |
| Join | .select('*, related_table(*)') |
Convex
| Operation | Method |
|---|---|
| Query all | ctx.db.query("table").collect() |
| Query with filter | .filter((q) => q.eq(q.field("field"), value)) |
| Query with index | .withIndex("index_name", (q) => q.eq("field", value)) |
| Get by ID | ctx.db.get(id) |
| Insert | ctx.db.insert("table", data) |
| Update | ctx.db.patch(id, data) |
| Replace | ctx.db.replace(id, data) |
| Delete | ctx.db.delete(id) |
| Paginate | .paginate(opts) |

