Overview
Reportr uses PostgreSQL with Prisma ORM for type-safe database access. The schema supports multi-tenant white-label agencies with client management, report generation, subscription billing, and API usage tracking. Database: PostgreSQLORM: Prisma Client
Schema Location:
prisma/schema.prisma
Core Models
User
Agency owners with white-label branding settings and subscription management.- White-label branding (custom colors, logos, company names)
- PayPal subscription integration
- Email verification and trial management
- Billing cycle tracking
- Signup flow tracking for analytics
- Primary color:
#8B5CF6(purple) - Plan:
FREE - Subscription status:
"free"
Client
Agency clients with Google API connections and metrics tracking.- Google Search Console and Analytics 4 OAuth tokens
- Custom metrics storage (JSON)
- Report generation tracking
- Cascade delete when user deleted
- Refresh tokens should be encrypted before storage
- Access tokens have expiry tracking
Report
Generated SEO reports with processing metadata and AI insights.- Complete processing lifecycle tracking
- AI cost tracking for billing analytics
- Multiple insight sources (AI, rule-based, fallback)
- Performance metrics (generation time)
Payment
PayPal subscription and payment tracking.- FREE - 5 reports/month, 2 clients
- STARTER - 25 reports/month, 5 clients
- PROFESSIONAL - 100 reports/month, 20 clients
- AGENCY - Unlimited reports, unlimited clients
Supporting Models
ApiUsage
API usage tracking for analytics and billing.- Track API endpoint usage per user
- Monitor performance (response times)
- Calculate costs (AI API usage)
- Rate limit monitoring
WebhookEvent
Webhook processing queue with retry logic.- Automatic retry with exponential backoff
- PayPal webhook processing
- Error tracking per attempt
AppSetting
Global application settings (key-value store).VerificationToken
Email verification tokens.EmailLog
Email delivery tracking to prevent duplicate sends.Relationships
One-to-Many
- User → Clients - One agency owner has many clients
- User → Reports - One user creates many reports
- User → Payments - One user has many payment transactions
- User → EmailLogs - One user receives many emails
- Client → Reports - One client has many reports
Cascade Deletes
When a User is deleted:- ✅ All Clients deleted
- ✅ All Reports deleted
- ✅ All Payments deleted
- ✅ All EmailLogs deleted
- ✅ All Reports for that client deleted
Indexes
Optimized queries with strategic indexes:Migration Commands
Usage Examples
Create User with Client
Query Reports with Relations
Update Subscription Status
Track API Usage
Security Best Practices
- Never expose raw tokens - Always encrypt Google refresh tokens before storage
- Use Prisma transactions - For operations affecting multiple tables
- Validate ownership - Always check
userIdmatches authenticated user - Use indexes - Query performance critical for large datasets
- Cascade deletes - Configured for data integrity
- Unique constraints - Prevent duplicate emails, PayPal IDs
Schema File Location
Full schema:~/workspace/source/prisma/schema.prisma
See also: