Stork SMS uses Supabase (PostgreSQL) as its primary database with a carefully designed schema that supports NFT-based messaging, end-to-end encryption, and real-time communication features.
Overview
The database schema is designed with the following principles:- Security First: Row Level Security (RLS) ensures data isolation
- Performance Optimized: Strategic indexing for common query patterns
- Scalability Ready: Designed to handle millions of users and messages
- Real-time Enabled: Optimized for live messaging features
- Audit Trail: Complete transaction history and metadata tracking
Core Tables
1. Chats Table
Thechats table stores metadata for NFT-based conversations between users.
- NFT-Based Identity: Each chat is tied to specific NFT mint addresses
- Bidirectional Mapping: Links sender and recipient NFTs and wallets
- Fee Tracking: Tracks chat creation fees and payment status
- Unique Constraint: Prevents duplicate chats between the same NFT pair
2. Messages Table
Themessages table stores encrypted message content and metadata.
- End-to-End Encryption: All content stored encrypted with AES-256-GCM
- Multi-Media Support: Handles text, images, voice, stickers, and files
- File Integration: Links to Cloudflare R2 storage for media files
- Soft Deletion: Messages can be deleted without losing chat history
- Audit Trail: Tracks creation and modification timestamps
text: Regular text messagesimage: Image attachments with file_url referencevoice: Voice messages with audio file referencesticker: Sticker messages with predefined sticker IDsfile: File attachments with metadatasystem: System-generated messages (notifications, etc.)
3. Chat Participants Table
Thechat_participants table manages user access rights and participation in chats.
- NFT-Based Access Control: Links wallet addresses to NFT ownership
- Role Management: Supports different permission levels
- Ownership Verification: Tracks NFT ownership verification status
- Read Receipts: Tracks last read message for unread count calculation
- Activity Tracking: Monitors user participation and activity
owner: Creator of the chat with full permissionsparticipant: Regular chat member with message permissionsadmin: Enhanced permissions for group management (future)
4. Fee Transactions Table
Thefee_transactions table tracks all fee payments for NFT creation and other services.
- Dual Currency Tracking: Stores amounts in both SOL and lamports
- Transaction Status: Tracks payment confirmation status
- Blockchain Integration: Links to Solana transaction signatures
- Flexible Metadata: JSONB field for additional transaction data
- Audit Compliance: Complete payment history with timestamps
pending: Transaction submitted but not yet confirmedconfirmed: Transaction confirmed on blockchainfailed: Transaction failed or rejected
Security Implementation
Row Level Security (RLS)
All tables implement Row Level Security to ensure users can only access their own data.Chats RLS Policies
Messages RLS Policies
Chat Participants RLS Policies
Fee Transactions RLS Policies
Authentication Context
The RLS policies rely on thewallet.address setting being properly configured during authentication:
Database Functions & Triggers
Automatic Timestamp Updates
All tables withupdated_at columns use triggers for automatic timestamp management:
Activity Tracking
Chat participants have automatic activity tracking:Real-time Subscriptions
Message Subscriptions
Clients subscribe to real-time message updates:Chat Status Updates
Real-time updates for chat activity and participant changes:Query Patterns & Optimization
Common Query Patterns
Get User’s Chats with Last Message
Get Messages for Chat with Pagination
Get Unread Message Count
Performance Considerations
Index Usage
- All foreign keys are indexed for efficient JOINs
- Composite indexes for common WHERE clause combinations
- Partial indexes for filtered queries (e.g., active records only)
Query Optimization
- Use LATERAL JOINs for correlated subqueries
- Implement proper LIMIT/OFFSET for pagination
- Use covering indexes where beneficial
Backup & Recovery
Automated Backups
- Point-in-time Recovery: Enabled with 7-day retention
- Daily Snapshots: Automated full database backups
- Transaction Log Backup: Continuous WAL archiving
Data Retention
- Messages: Retained indefinitely unless explicitly deleted
- Fee Transactions: Retained permanently for audit compliance
- Activity Logs: Participant activity retained for 1 year
Migration Strategy
Schema Versioning
All schema changes are managed through numbered migration files:Migration Best Practices
- Backward Compatibility: Migrations never break existing functionality
- Rollback Strategy: Each migration includes rollback procedures
- Testing: All migrations tested on staging before production
- Monitoring: Database performance monitored during migrations
Monitoring & Metrics
Key Metrics
- Query Performance: Average response times per table
- Connection Pool: Active connections and wait times
- Storage Growth: Table sizes and growth rates
- Index Usage: Index hit ratios and unused indexes
Alerting
- Database connection failures
- Slow query detection (>1s)
- Disk space thresholds
- Unusual error rates
