games_app/backend/docs/ARCHITECTURE.md
2026-06-29 15:01:23 +02:00

60 lines
2.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Backend architecture: Model – Repository – Service – Controller
This project uses the **same layered pattern as Spring Boot** in Node.js:
| Layer | Role | In this project |
|------------|------|------------------|
| **Model** | Domain shapes / DB entities | `src/types/index.ts` (interfaces: `User`, `WalletType`, `LedgerAccount`, `Transaction`, etc.) |
| **Repository** | Data access (DB, external APIs) | `src/repositories/*.ts` |
| **Service** | Business logic, orchestration | `src/services/*.ts` |
| **Controller** | HTTP: parse request, call service, send response | `src/controllers/*.ts` |
Request flow:
```
HTTP Request → Controller → Service → Repository(ies) → DB / external
← ← ←
Response return return Model(s)
```
---
## Folder layout (Spring-like)
```
src/
├── types/ # Model layer (entity / DTO interfaces)
├── repositories/ # Repository layer (data access)
├── services/ # Service layer (business logic)
├── controllers/ # Controller layer (HTTP)
├── routes/ # Maps URLs → controllers (like Spring @RequestMapping)
├── middleware/ # Auth, rate limit, audit (like Spring filters)
├── config/ # Configuration
├── db/ # DB connection, schema, migrations
└── utils/ # Helpers (crypto, etc.)
```
---
## Rules (to keep the pattern clear)
1. **Controllers**
- Only: read `req`, call **services**, write `res`.
- No direct use of repositories or DB.
2. **Services**
- Contain business logic.
- Call **repositories** and other services.
- Use **types** (models) for parameters and return values.
3. **Repositories**
- Only: run queries / commands (SQL, external API).
- Return **types** (models) or primitives.
- No business rules.
4. **Models (types)**
- Interfaces (and optionally DTOs) for entities and API shapes.
- No logic, only structure.
Optional: you can rename `src/types` to `src/models` if you want the folder name to match “Model” exactly; the role stays the same.