|
| 1 | +# Code Architecture |
| 2 | + |
| 3 | +## Framework Stack |
| 4 | + |
| 5 | +- **NestJS** - Server framework |
| 6 | +- **GraphQL** - API layer (Apollo Server) |
| 7 | +- **MongoDB** - Database (Mongoose ODM) |
| 8 | + |
| 9 | +## Two-Layer Structure |
| 10 | + |
| 11 | +1. **Core Layer** (`src/core/`) - Reusable framework components (exported to consumers) |
| 12 | +2. **Server Layer** (`src/server/`) - Internal test/demo implementation (not exported) |
| 13 | + |
| 14 | +## Core Module (`src/core.module.ts`) |
| 15 | + |
| 16 | +- Dynamic module providing base functionality |
| 17 | +- Configures GraphQL with Apollo Server, MongoDB with Mongoose |
| 18 | +- Provides global services: ConfigService, EmailService, TemplateService |
| 19 | +- Sets up security interceptors, validation pipes, complexity plugins |
| 20 | +- Handles GraphQL subscriptions with authentication |
| 21 | + |
| 22 | +## Configuration System (`src/config.env.ts`) |
| 23 | + |
| 24 | +Environment-based configuration (development, local, production) with multiple sources: |
| 25 | + |
| 26 | +- Direct environment variables in config file |
| 27 | +- `NEST_SERVER_CONFIG` JSON environment variable |
| 28 | +- `NSC__*` prefixed single environment variables |
| 29 | + |
| 30 | +Key areas: JWT, MongoDB, GraphQL, email, security, static assets |
| 31 | + |
| 32 | +## Core Common Components (`src/core/common/`) |
| 33 | + |
| 34 | +| Type | Components | |
| 35 | +|------|------------| |
| 36 | +| **Decorators** | `@Restricted()`, `@Roles()`, `@CurrentUser()`, `@UnifiedField()` | |
| 37 | +| **Helpers** | Database, GraphQL, filtering, validation utilities | |
| 38 | +| **Security** | Response/security interceptors, input validation pipes | |
| 39 | +| **Scalars** | Custom GraphQL scalars (Date, JSON, Any) | |
| 40 | +| **Services** | CRUD operations, email (Mailjet/SMTP), template rendering | |
| 41 | + |
| 42 | +## Core Modules (`src/core/modules/`) |
| 43 | + |
| 44 | +| Module | Purpose | |
| 45 | +|--------|---------| |
| 46 | +| **Auth** | JWT authentication, refresh tokens, role-based access | |
| 47 | +| **BetterAuth** | Modern auth integration (2FA, Passkey, Social) | |
| 48 | +| **File** | File upload/download with GridFS storage | |
| 49 | +| **User** | Core user management functionality | |
| 50 | +| **HealthCheck** | Application health monitoring | |
| 51 | + |
| 52 | +## Security Implementation |
| 53 | + |
| 54 | +- `@Restricted()` - Field-level access control |
| 55 | +- `@Roles()` - Method-level authorization |
| 56 | +- `CheckResponseInterceptor` - Filters restricted fields |
| 57 | +- `CheckSecurityInterceptor` - Processes `securityCheck()` methods |
| 58 | + |
| 59 | +## Model Inheritance |
| 60 | + |
| 61 | +- `CorePersistenceModel` - Base for database entities |
| 62 | +- `CoreModel` - Base for GraphQL types |
| 63 | +- Automatic ID handling with custom Mongoose plugin |
| 64 | + |
| 65 | +## Input Validation |
| 66 | + |
| 67 | +- `MapAndValidatePipe` - Automatic validation |
| 68 | +- class-validator decorators on input classes |
| 69 | +- Core args classes for filtering/pagination |
0 commit comments