Skip to main content

Database Prompts

Prompts for database schema design, queries, and backend operations. Works with both Supabase (PostgreSQL) and Convex.

Schema Design

Design a Table

Design a database table for [feature/entity].Requirements:
  • [List what data needs to be stored]
  • [Any relationships to other tables]
  • [Any constraints or validations]
I’m using Supabase (PostgreSQL). Provide:
  1. CREATE TABLE statement
  2. Row Level Security policies (users can only access their own data)
  3. Any necessary indexes
  4. TypeScript interface matching the schema

Design a database schema for [feature] with these entities:
  • Relationship: [describe relationship]
Provide:
  1. All CREATE TABLE statements
  2. Foreign key relationships
  3. RLS policies for each table
  4. TypeScript interfaces
  5. Example queries for common operations

Add Column to Existing Table

I need to add a new column to the [table_name] table:
  • Column name: [name]
  • Type: [type]
  • Nullable: [yes/no]
  • Default: [value or none]
Provide:
  1. ALTER TABLE statement
  2. Update to TypeScript interface
  3. 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
The table has a 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
Use ctx.auth.getUserIdentity() to check auth.

Role-Based Access

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
Assume we have a user_roles table with user_id and role columns.

CRUD Operations

Create Service Functions

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
Follow these patterns:
  • Use the Supabase client from @/services/supabase
  • Include proper TypeScript types
  • Handle errors appropriately
  • Return typed data
Put in: app/services/[name]Service.ts

Fetch with Filters

Create a function to fetch [items] from Supabase with filters:Filters needed:
  • Search by: [fields]
  • Sort by: [field] ascending/descending
  • Pagination: page, pageSize
Return both the data and total count for pagination.

Fetch with Relationships

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]
Use Supabase’s select with nested queries. Include TypeScript types for the nested response.

Real-time Subscriptions

Subscribe to Changes

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]
Include:
  • Subscription setup function
  • Cleanup function for unmount
  • TypeScript types for payload
  • Usage example in a component

Live Query Hook

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
Usage: const { data, loading, error } = useLive[Items]()

Migrations

Create Migration

Create a Supabase migration for: [Describe the change]Include:
  1. The migration SQL
  2. A descriptive migration name
  3. Any seed data if needed
  4. Rollback SQL (if possible)

Performance

Add Indexes

Suggest indexes for the [table_name] table to optimize these queries:
  1. [Describe query 1]
  2. [Describe query 2]
  3. [Describe query 3]
Provide CREATE INDEX statements and explain why each index helps.

Common Patterns

Soft Delete

Implement soft delete for [table_name]:
  1. Add deleted_at timestamp column
  2. Update RLS to filter deleted rows by default
  3. Create service functions:
    • softDelete(id)
    • restore(id)
    • getDeleted()
    • permanentDelete(id)
Update existing queries to exclude soft-deleted rows.

Timestamps

Add automatic timestamp handling to [table_name]:
  • created_at: Set on insert
  • updated_at: Set on insert and update
Include:
  1. Column definitions with defaults
  2. Trigger for auto-updating updated_at
  3. TypeScript interface updates

Quick Reference

Supabase

OperationMethod
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

OperationMethod
Query allctx.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 IDctx.db.get(id)
Insertctx.db.insert("table", data)
Updatectx.db.patch(id, data)
Replacectx.db.replace(id, data)
Deletectx.db.delete(id)
Paginate.paginate(opts)