High-Level Overview
RLink is built as a monolithic Next.js application using the App Router pattern, with clear separation between three main business domains: CMS, CRM, and IAM.Architecture Principles
Server-First Approach
API routes handle all business logic and database operations, keeping the client thin
Type Safety
End-to-end TypeScript with Drizzle ORM providing type-safe database queries
Separation of Concerns
Clear domain boundaries between CMS, CRM, and IAM modules
Performance Optimization
TanStack Query for caching, lazy loading, and skeleton states
Application Structure
Directory Organization
Data Flow
1. Client-Side Data Fetching
RLink uses TanStack Query for all client-side data fetching with centralized caching:- Eliminates redundant API calls when switching tabs
- Automatic background refetching
- Optimistic updates
- Stale data invalidation
2. API Route Pattern
All API routes follow a consistent RESTful pattern:3. Database Access
Drizzle ORM provides type-safe database queries:Module Breakdown
Content Management System (CMS)
Purpose: Manage website content, projects, careers, and articles Key Features:- Project CRUD with photo galleries
- Markdown article editor
- SEO optimization tools
- Analytics integration
projectsproject_galleriesamenitiescareersarticles
Customer Relationship Management (CRM)
Purpose: Track leads, reservations, inquiries, and marketing campaigns Key Features:- Lead tracking with filtering
- Reservation management
- Inquiry inbox
- Newsletter campaigns
- DSL (Document Status) tracker
leadsreservationsinquiriesnewsletterscampaignsinventory
Identity & Access Management (IAM)
Purpose: User management, access control, and audit logging Key Features:- User lifecycle management
- Role-based access control
- Module-level permissions
- Activity audit logs
- 2FA support
users(Better Auth)sessions(Better Auth)module_accessactivity_logsdepartments
Security Architecture
Authentication Flow
Protected Routes
- Client-Side Protection:
ProtectedRoutecomponent wraps dashboard pages - API Protection: Middleware validates session on every API call
- CORS Configuration:
ALLOWED_ORIGINenvironment variable controls cross-origin requests
Security Layers
Rate Limiting
Rate Limiting
Server-side rate limiting prevents API abuse and brute-force attempts.Rate limiting is likely implemented in API routes or middleware to throttle excessive requests.
Session Management
Session Management
Better Auth manages secure session tokens with automatic expiration and refresh.
Two-Factor Authentication
Two-Factor Authentication
TOTP-based 2FA using authenticator apps for enhanced account security.
Activity Logging
Activity Logging
All user actions are logged with automatic 90-day retention policy via cron job.
Email Architecture
Email Flow
Email Templates
Located intemplates/email/:
- Welcome Email: Sent to new users upon account creation
- Password Reset: Sent when users request password reset
- Campaign Email: Marketing and newsletter emails
Email Integration
Performance Optimizations
1. Centralized Data Fetching
Data is fetched once per module and cached, eliminating spinners on tab switches.2. Server-Side Pagination
Default limit of 10 records per request reduces memory overhead and response latency.3. Lazy Loading
Dashboard skeleton loaders provide immediate visual feedback while data loads.4. Image Optimization
- Next.js Image component for automatic optimization
- Thumbnail generation for galleries
- Quality optimization for performance
5. Query Caching
TanStack Query cache with 5-minute stale time reduces redundant database queries.Deployment Architecture
Vercel Deployment
RLink is deployed on Vercel with the following configuration:- Activity Log Retention: Daily cleanup of logs older than 90 days
Environment Configuration
Production environment requires:ALLOWED_ORIGINset to actual production domainNEXT_PUBLIC_APP_URLmatching deployed URLCRON_SECRETfor secure cron execution- All database and email credentials
Scaling Considerations
Database Scaling
Neon PostgreSQL supports automatic scaling. Consider connection pooling for high traffic.
CDN Integration
Vercel provides global CDN. Static assets are automatically cached at edge locations.
API Caching
Implement Redis or similar for API response caching if needed for high traffic.
Background Jobs
Consider queue system (Bull, BullMQ) for heavy operations instead of API routes.
Technology Decisions
Why Next.js 16 App Router?
- Server Components reduce client bundle size
- Built-in API routes eliminate separate backend
- Excellent TypeScript support
- Vercel deployment optimization
Why Drizzle ORM?
- Type-safe queries with TypeScript
- Lightweight compared to TypeORM/Prisma
- SQL-like syntax familiar to developers
- Excellent migration tooling
Why Better Auth?
- Simple setup compared to NextAuth
- Built-in 2FA support
- Admin plugin for user management
- Session management out of the box
Why TanStack Query?
- Automatic caching and refetching
- Optimistic updates
- Better developer experience than SWR
- Excellent TypeScript support
Next Steps
Database schema
Explore tables and relationships
Proxy and request flow
See how auth, cookies, and CORS enter the app
REST API reference
Review HTTP endpoints
Domain libraries
Understand
lib/cms, lib/crm, lib/iam, and lib/emailDeployment
Production and environment setup
