src/
├── infrastructure/
│ ├── persistance // semua akses ke database
│ ├── identity // orkestrator untuk authn & authz program
│ ├── services // orkestrator external program, eg: file-upload
│ ├── config // constant configurasi aplikasi
│ └── middlewares // middlewares
│
├── modules/
│ ├── common/
│ │ ├── helpers // fungsi pembantu
│ │ ├── interfaces // kontrak untuk service yang di infra
│ │ ├── types // kontrak data shared
│ │ └── constants // konstanta global
│ │
│ └── student/
│ ├── student.contract.ts // kontrak HTTP
│ ├── student.controller.ts // HTTP
│ ├── student.interfaces.ts // kontrak service
│ ├── student.routes.ts // rute defenition
│ └── student.service.ts // orkestrator program
│
└── server.ts
-
Scoped: Ketika kita butuh instance baru untuk tiap HTTP request karena ia memiliki attribute berbeda (tanya Althaf detailnya)
const something = (() => ({ const service = new Service(); })) // dan ulangi untuk aksi lain
-
Singleton Ketika kita butuh 1 instance sama yang shared dengan instance lain selama aplikasi hidup,
const db = new Database(); const service = new Service(db);
- Class:
- Nama Harus berupa pascal case
- Harus menambahkan controller/service setelah nama, eg: AuthController/StudentService
- Harus diawali export. eg: export class...
- Class service harus punya private constructor untuk menyimpan referensi ke instance database
- Method:
- Nama harus merepresentasikan aksi, eg: login, deleteById, getAll
- Nama tidak perlu menambahkan kata entitas yang terkait, cukup aksi saja seperti loginStudent (salah), login (benar)
- Menggunakan camelCase
- Jika return/input bukan void maka harus punya tipe return/input
- Tambahkan keyword async di awal jika ia asinkronus (biasanya cuman di service)
- Boleh diawali keyword public atau tidak
- Interfaces:
- Nama harus mencerminkan service yang akan dibuat, eg: IStudentService
- Berisi semua kontrak method serta tipe data return/inputnya
- Awali nama dengan prefix I (interface), eg IStudentService
- Types:
- Nama harus mencerminkan tipe data yang akan dibuat, eg: TProfileDetailsResponse
- Awali nama dengan prefix T (Types), eg TProfileDetailsResponse
- Umumnya kita gunakan untuk membuat API contract yaitu T{Aksi}{Entitas}Request (untuk data masuk) atau Response (data keluar)
- Bisa digunakan untuk custom tipe data internal, eg: TJWTClaims, TCookiePayload
- Variables
- Harus merepresentasikan apa yang disimpan, eg: const name, let tempData
- Hanya boleh gunakan keyword const dan let
- Jika ia constant maka buat dengan SCREAMING_SNAKE_CASE
- Jika ia sepenuhnya immutable (ga bisa diubah) maka tulis dengan const x = {...} as const
bun add helmet.js
bun add cors