System Architecture
KnowledgeCheckr is a modern SaaS platform built with Next.js 16 and React 19, designed to create and manage knowledge assessments with support for practice and examination modes.Tech Stack Overview
Frontend
- Framework: Next.js 16.0.7 with App Router
- React: 19.1.0 with Server Components
- TypeScript: Type-safe development
- Styling: Tailwind CSS 4.1.18
- UI Components:
- Radix UI primitives for accessible components
- HeroUI for specialized components
- Framer Motion for animations
- Lucide React for icons
Backend
- Runtime: Node.js with Next.js API routes
- Database: MySQL 2 (via mysql2 3.14.0)
- ORM: Drizzle ORM 0.44.7
- Authentication: Better Auth 1.2.5
- Validation: Zod 3.24.2
State Management
- Client State: Zustand 5.0.3
- Server State: React Server Components
- Forms: React Hook Form 7.55.0 with @hookform/resolvers
Internationalization
- Library: next-international 1.3.1
- Supported Locales: English (en), German (de)
Development & Testing
- Testing: Cypress 15.7.0 (E2E and Component testing)
- Code Coverage: @cypress/code-coverage
- Linting: ESLint 9 with Next.js config
- Formatting: Prettier 3.5.2
- Git Hooks: Husky 8 with lint-staged
Monitoring & Logging
- Error Tracking: Sentry (@sentry/nextjs 10.26.0)
- Logging: Winston 3.19.0 with daily rotate file
Application Architecture
Directory Structure
Design Patterns
Server-First Architecture
KnowledgeCheckr leverages Next.js Server Components for optimal performance:- Server Components handle data fetching
- Client Components only where interactivity is needed
- Server Actions for mutations
- Streaming for progressive rendering
Database Layer
The application uses a layered database approach:- Connection Layer (
database/Pool.ts): Manages MySQL connection pooling - ORM Layer (
database/Database.ts): Drizzle ORM integration - Repository Layer: Domain-specific database operations
- Production: New pool per request
- Development: Cached global pool to prevent “Too many connections” errors
Authentication Flow
Better Auth provides flexible authentication with multiple providers:Internationalization Strategy
Next-international provides type-safe i18n:- Server-side:
getI18n(),getScopedI18n(),getCurrentLocale() - Client-side:
useI18n(),useScopedI18n(),useCurrentLocale() - URL strategy:
rewriteDefault(hides default locale from URL)
Data Flow
Request Lifecycle
- Client Request → User accesses route
- Middleware → i18n middleware processes locale
- Server Component → Fetches data using Drizzle ORM
- Authentication Check → Better Auth validates session
- Data Transform → Zod validates and transforms data
- Response → Server Component renders or returns JSON
Knowledge Check Flow
User Relationships
- Owner: Creates and manages knowledge checks
- Collaborator: Can contribute to knowledge checks
- Guest: Takes examinations and practice sessions
- Anonymous: Temporary users (can be linked to accounts)
Deployment Architecture
Build Configuration
- Standalone output for containerization
- Auth interrupts for Better Auth
- Image optimization for avatars (GitHub, Google, ui-avatars.com)
Environment Management
Environment validation occurs at build time usingsrc/lib/Shared/Env.ts:
Database Migrations
Drizzle Kit manages schema migrations:Performance Optimizations
Frontend
- Code Splitting: Automatic with Next.js App Router
- Image Optimization: Built-in Next.js image optimization
- Font Optimization: Automatic font loading optimization
- Bundle Analysis: Tree-shaking with ES modules
Database
- Connection Pooling: Reused connections (limit: 10)
- Query Optimization: Drizzle ORM generates efficient queries
- Indexes: Strategic indexes on foreign keys and lookup fields
Caching Strategy
- Static Generation: Pre-rendered pages where possible
- Incremental Static Regeneration: For dynamic content
- API Route Caching: Cached responses with revalidation
Security Considerations
Authentication
- Session-based authentication via Better Auth
- Secure cookie handling with httpOnly and secure flags
- CSRF protection built into Better Auth
- Rate limiting enabled in production
Database
- Prepared statements via Drizzle ORM (SQL injection prevention)
- Foreign key constraints for referential integrity
- Cascade deletes for data consistency
API Security
- Server-side validation with Zod schemas
- Authentication checks on protected routes
- IP address and user agent tracking for sessions
Monitoring & Observability
Error Tracking
Sentry integration for comprehensive error monitoring:Logging
Winston logger with module-level logging:- Daily rotating log files
- Structured logging
- Module-specific log contexts
Next Steps
- Database Schema - Detailed schema documentation
- Authentication - Better Auth implementation
- Internationalization - i18n setup guide