60 lines
2.1 KiB
Markdown
60 lines
2.1 KiB
Markdown
# 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.
|