Data Models
Actual Supabase Database Schema
This ER diagram shows the ACTUAL tables and relationships currently implemented in the Earna AI Supabase database with Row Level Security (RLS).
Entity Details
Core Entities:
Entity | Description | Primary Storage |
---|---|---|
auth.users | Supabase Auth users | Supabase Auth Schema |
users | Platform user profiles | Supabase Public Schema |
chats | AI chat conversations | Supabase with RLS |
messages | Chat messages | Supabase with RLS |
projects | Multi-model projects | Supabase with RLS |
chat_attachments | File attachments | Supabase Storage |
Data Volume Estimates:
- Users: ~100,000 (target)
- Chat Sessions: ~500,000/month
- Messages: ~5,000,000/month
- Tool Calls: ~2,000,000/month
- Credit Profiles: ~100,000
- Credit Scores: ~200,000/month
- Improvement Plans: ~50,000/month
Supabase Database Architecture
This section details the Supabase-specific implementation and features used in the Earna AI platform.
Supabase Features
Supabase Services Used:
Service | Purpose | Implementation |
---|---|---|
Database | PostgreSQL 15 | Core data storage with RLS |
Auth | User authentication | Email/password, OAuth providers |
Storage | File storage | Document uploads, reports |
Realtime | Live updates | Chat messages, notifications |
Edge Functions | Serverless compute | AI processing, webhooks |
Vector | Embeddings | pgvector for semantic search |
Supabase Configuration:
// Supabase Client Configuration
import { createClient } from '@supabase/supabase-js'
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
auth: {
autoRefreshToken: true,
persistSession: true,
detectSessionInUrl: true
},
realtime: {
params: {
eventsPerSecond: 10
}
},
db: {
schema: 'public'
}
})
// Supabase Admin Client (Server-side)
const supabaseAdmin = createClient(
supabaseUrl,
process.env.SUPABASE_SERVICE_ROLE_KEY,
{
auth: {
autoRefreshToken: false,
persistSession: false
}
}
)
Database Extensions:
uuid-ossp
: UUID generationpgcrypto
: Encryption functionspg_stat_statements
: Query performancepgvector
: Vector similarity searchpg_trgm
: Text similarity search
Credit Analysis Data Model
This ER diagram shows the Canadian credit analysis data model for credit score tracking and improvement planning.
Credit Entities
Credit Data Entities:
Entity | Description | Encryption |
---|---|---|
CreditProfile | User credit identity | PII encrypted |
CreditReport | Bureau credit reports | Encrypted at rest |
TradeLine | Credit accounts | Encrypted |
PaymentHistory | Payment records | Encrypted |
PublicRecord | Bankruptcies, liens | Encrypted |
CreditInquiry | Credit checks | Audit trail |
InquiryImpact | Inquiry effects | Encrypted |
ScoreSimulation | What-if scenarios | Encrypted |
SimulationAction | Simulated actions | Encrypted |
SimulationResult | Projected outcomes | Encrypted |
Credit Score Factors (Canadian):
- Payment History: 35%
- Credit Utilization: 30%
- Length of Credit History: 15%
- Types of Credit: 10%
- New Credit Inquiries: 10%
Banking Integration Data Model
This section details the data model for Plaid banking integration and financial analysis.
Banking Entities
Banking Data Entities:
Entity | Description | Update Frequency |
---|---|---|
PlaidConnection | User’s Plaid link | On demand |
BankAccount | Linked bank accounts | Daily sync |
Transaction | Account transactions | Real-time webhook |
TransactionCategory | Spending categories | Per transaction |
Balance | Current balances | Daily |
BalanceHistory | Historical balances | Daily snapshot |
SpendingAnalysis | Spending patterns | Weekly/Monthly |
SpendingCategory | Category breakdown | Per analysis |
CreditUtilization | Credit card usage | Daily |
Plaid Data Scope:
- Transactions: 2 years history
- Balances: Real-time
- Account details: On connection
- Identity: Optional verification
- Liabilities: Credit cards, loans
AI SDK v5 Message Types
This section documents the AI SDK v5 message type system used throughout the application.
Type Definitions
AI SDK v5 Type Definitions:
// From @ai-sdk/react
export interface UIMessage {
id: string;
role: 'user' | 'assistant' | 'system';
parts: UIMessagePart[];
}
export type UIMessagePart =
| TextUIPart
| ToolInvocationUIPart
| ImageUIPart
| FileUIPart
| ReasoningUIPart;
export interface TextUIPart {
type: 'text';
text: string;
}
export interface ToolInvocationUIPart {
type: 'tool-invocation';
toolInvocation: ToolInvocation;
}
// From @ai-sdk/ui-utils
export interface Message {
id: string;
content: string;
role: 'user' | 'assistant' | 'system' | 'data';
createdAt?: Date;
experimental_attachments?: Attachment[];
parts?: UIMessagePart[];
toolInvocations?: ToolInvocation[];
}
export interface Attachment {
name: string;
contentType: string;
url: string;
}
Role Types:
user
: Messages from the userassistant
: AI model responsessystem
: System prompts and instructionsdata
: Data-only messages (Message type only)