Skip to main content
Stork SMS Database Schema 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

The chats table stores metadata for NFT-based conversations between users.
Key Features:
  • 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
Indexes:

2. Messages Table

The messages table stores encrypted message content and metadata.
Key Features:
  • 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
Message Types:
  • text: Regular text messages
  • image: Image attachments with file_url reference
  • voice: Voice messages with audio file reference
  • sticker: Sticker messages with predefined sticker IDs
  • file: File attachments with metadata
  • system: System-generated messages (notifications, etc.)
Indexes:

3. Chat Participants Table

The chat_participants table manages user access rights and participation in chats.
Key Features:
  • 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
Participant Roles:
  • owner: Creator of the chat with full permissions
  • participant: Regular chat member with message permissions
  • admin: Enhanced permissions for group management (future)
Indexes:

4. Fee Transactions Table

The fee_transactions table tracks all fee payments for NFT creation and other services.
Key Features:
  • 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
Fee Transaction Statuses:
  • pending: Transaction submitted but not yet confirmed
  • confirmed: Transaction confirmed on blockchain
  • failed: Transaction failed or rejected
Indexes:

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 the wallet.address setting being properly configured during authentication:

Database Functions & Triggers

Automatic Timestamp Updates

All tables with updated_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
This schema design provides a solid foundation for Stork SMS to scale while maintaining security, performance, and data integrity across all operations.