lapeh 2.2.1 → 2.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/doc/CHANGELOG.md +38 -0
- package/doc/CHEATSHEET.md +89 -0
- package/doc/CLI.md +106 -0
- package/doc/CONTRIBUTING.md +105 -0
- package/doc/DEPLOYMENT.md +122 -0
- package/doc/FAQ.md +81 -0
- package/doc/FEATURES.md +165 -0
- package/doc/GETTING_STARTED.md +108 -0
- package/doc/INTRODUCTION.md +60 -0
- package/doc/PACKAGES.md +66 -0
- package/doc/PERFORMANCE.md +91 -0
- package/doc/ROADMAP.md +93 -0
- package/doc/SECURITY.md +93 -0
- package/doc/STRUCTURE.md +72 -0
- package/doc/TUTORIAL.md +192 -0
- package/eslint.config.mjs +26 -0
- package/framework.md +168 -113
- package/nodemon.json +1 -1
- package/package.json +12 -3
- package/prisma/seed.ts +0 -1
- package/readme.md +46 -0
- package/src/controllers/authController.ts +145 -39
- package/src/controllers/petController.ts +70 -16
- package/src/controllers/rbacController.ts +82 -9
- package/src/core/redis.ts +5 -4
- package/src/core/serializer.ts +63 -0
- package/src/middleware/auth.ts +0 -1
- package/src/middleware/rateLimit.ts +1 -1
- package/src/routes/auth.ts +3 -3
- package/src/utils/response.ts +21 -0
- package/tsconfig.json +17 -12
- package/document.md +0 -205
package/doc/STRUCTURE.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Bedah Struktur Proyek
|
|
2
|
+
|
|
3
|
+
Untuk memahami Lapeh Framework sepenuhnya, Anda perlu tahu apa fungsi setiap file dan folder. Berikut adalah "Tour" lengkap ke dalam direktori proyek.
|
|
4
|
+
|
|
5
|
+
## Root Directory
|
|
6
|
+
|
|
7
|
+
| File/Folder | Deskripsi |
|
|
8
|
+
| :--- | :--- |
|
|
9
|
+
| `bin/` | Berisi script eksekusi untuk CLI (`npx lapeh`). Anda jarang menyentuh ini. |
|
|
10
|
+
| `doc/` | Dokumentasi proyek ini berada. |
|
|
11
|
+
| `prisma/` | Jantung konfigurasi Database. |
|
|
12
|
+
| `scripts/` | Kumpulan script Node.js untuk utility (generator, compiler schema, dll). |
|
|
13
|
+
| `src/` | **Source Code Utama**. 99% kodingan Anda ada di sini. |
|
|
14
|
+
| `.env` | Variabel rahasia (Database URL, API Keys). **Jangan commit file ini ke Git!** |
|
|
15
|
+
| `docker-compose.yml` | Konfigurasi Docker untuk menjalankan Database & Redis lokal. |
|
|
16
|
+
| `nodemon.json` | Konfigurasi auto-restart saat development. |
|
|
17
|
+
| `package.json` | Daftar library (dependencies) dan perintah (`npm run ...`). |
|
|
18
|
+
| `tsconfig.json` | Konfigurasi TypeScript. |
|
|
19
|
+
|
|
20
|
+
## Folder `src/` (Source Code)
|
|
21
|
+
|
|
22
|
+
Ini adalah tempat Anda bekerja setiap hari.
|
|
23
|
+
|
|
24
|
+
### `src/controllers/`
|
|
25
|
+
Berisi logika aplikasi. Controller menerima Request, memprosesnya, dan mengembalikan Response.
|
|
26
|
+
- **Contoh**: `authController.ts` menangani login/register.
|
|
27
|
+
- **Tips**: Jangan taruh *business logic* yang terlalu kompleks di sini. Gunakan Service (opsional) jika controller sudah terlalu gemuk.
|
|
28
|
+
|
|
29
|
+
### `src/models/`
|
|
30
|
+
Berisi definisi tabel database (Schema Prisma).
|
|
31
|
+
- **Unik di Lapeh**: Kami memecah `schema.prisma` yang besar menjadi file-file kecil per fitur (misal `user.prisma`, `product.prisma`) agar mudah di-manage. Script `prisma:migrate` akan menggabungkannya nanti.
|
|
32
|
+
|
|
33
|
+
### `src/routes/`
|
|
34
|
+
Mendefinisikan URL endpoint.
|
|
35
|
+
- Menghubungkan URL (misal `/api/login`) ke fungsi di Controller.
|
|
36
|
+
- Menempelkan Middleware (misal `requireAuth`).
|
|
37
|
+
|
|
38
|
+
### `src/middleware/`
|
|
39
|
+
Kode yang berjalan *sebelum* Controller.
|
|
40
|
+
- `auth.ts`: Cek JWT Token.
|
|
41
|
+
- `rateLimit.ts`: Batasi jumlah request.
|
|
42
|
+
- `requestLogger.ts`: Log setiap request yang masuk.
|
|
43
|
+
|
|
44
|
+
### `src/utils/`
|
|
45
|
+
Fungsi bantuan (Helper) yang bisa dipakai di mana saja.
|
|
46
|
+
- `validator.ts`: Validasi input ala Laravel.
|
|
47
|
+
- `response.ts`: Standar format JSON response (`sendSuccess`, `sendError`).
|
|
48
|
+
- `logger.ts`: Sistem logging (Winston).
|
|
49
|
+
|
|
50
|
+
### `src/core/`
|
|
51
|
+
Bagian "Mesin" framework. Anda jarang perlu mengubah ini kecuali ingin memodifikasi behavior dasar framework.
|
|
52
|
+
- `server.ts`: Setup Express App.
|
|
53
|
+
- `database.ts`: Instance Prisma Client.
|
|
54
|
+
- `redis.ts`: Koneksi Redis.
|
|
55
|
+
- `serializer.ts`: Logic caching JSON Schema.
|
|
56
|
+
|
|
57
|
+
## Folder `prisma/`
|
|
58
|
+
|
|
59
|
+
- `migrations/`: History perubahan database (SQL file). Jangan diedit manual.
|
|
60
|
+
- `base.prisma.template`: Header dari schema database (berisi konfigurasi datasource db).
|
|
61
|
+
- `seed.ts`: Script untuk mengisi data awal (Data Seeding).
|
|
62
|
+
|
|
63
|
+
## Folder `scripts/`
|
|
64
|
+
|
|
65
|
+
Script-script "Magic" yang dijalankan `npm run`.
|
|
66
|
+
- `make-controller.js`: Generator controller.
|
|
67
|
+
- `compile-schema.js`: Penggabung file `.prisma`.
|
|
68
|
+
- `init-project.js`: Wizard setup awal.
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
Dengan memahami struktur ini, Anda tidak akan tersesat saat ingin menambah fitur baru atau mencari bug.
|
package/doc/TUTORIAL.md
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# Tutorial: Membangun API Perpustakaan
|
|
2
|
+
|
|
3
|
+
Dalam tutorial ini, kita akan membangun fitur "Manajemen Buku" sederhana menggunakan semua fitur unggulan Lapeh Framework:
|
|
4
|
+
1. **CLI** untuk generate kode.
|
|
5
|
+
2. **Validator** untuk validasi input.
|
|
6
|
+
3. **Fast Serialization** untuk respon cepat.
|
|
7
|
+
4. **RBAC** untuk proteksi delete (Admin only).
|
|
8
|
+
|
|
9
|
+
## Langkah 1: Generate Model Database
|
|
10
|
+
|
|
11
|
+
Kita butuh tabel `books`. Gunakan CLI `make:model`.
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm run make:model Book
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
File baru akan muncul di `src/models/book.prisma`. Edit file tersebut:
|
|
18
|
+
|
|
19
|
+
```prisma
|
|
20
|
+
// src/models/book.prisma
|
|
21
|
+
|
|
22
|
+
model Book {
|
|
23
|
+
id BigInt @id @default(autoincrement())
|
|
24
|
+
title String
|
|
25
|
+
author String
|
|
26
|
+
isbn String @unique
|
|
27
|
+
publishedAt DateTime
|
|
28
|
+
stock Int @default(0)
|
|
29
|
+
created_at DateTime @default(now())
|
|
30
|
+
updated_at DateTime @updatedAt
|
|
31
|
+
|
|
32
|
+
@@map("books")
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Terapkan perubahan ke database:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm run prisma:migrate
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Langkah 2: Generate Module (Controller & Route)
|
|
43
|
+
|
|
44
|
+
Kita buat controller dan route sekaligus.
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npm run make:module Book
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Framework akan membuat:
|
|
51
|
+
- `src/controllers/bookController.ts`
|
|
52
|
+
- `src/routes/book.ts`
|
|
53
|
+
|
|
54
|
+
## Langkah 3: Implementasi Controller
|
|
55
|
+
|
|
56
|
+
Buka `src/controllers/bookController.ts` dan kita implementasikan fitur **Create** dan **List** dengan standar framework.
|
|
57
|
+
|
|
58
|
+
### Setup Import & Serializer
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { Request, Response } from "express";
|
|
62
|
+
import { prisma } from "@/core/database";
|
|
63
|
+
import { sendFastSuccess, sendError } from "@/utils/response";
|
|
64
|
+
import { Validator } from "@/utils/validator";
|
|
65
|
+
import { getSerializer, createResponseSchema } from "@/core/serializer";
|
|
66
|
+
|
|
67
|
+
// 1. Definisikan Schema Output (untuk Fastify Serialization)
|
|
68
|
+
const bookSchema = {
|
|
69
|
+
type: "object",
|
|
70
|
+
properties: {
|
|
71
|
+
id: { type: "string" }, // BigInt -> String
|
|
72
|
+
title: { type: "string" },
|
|
73
|
+
author: { type: "string" },
|
|
74
|
+
isbn: { type: "string" },
|
|
75
|
+
stock: { type: "number" }
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// 2. Buat Serializer
|
|
80
|
+
const bookDetailSerializer = getSerializer("book-detail", createResponseSchema(bookSchema));
|
|
81
|
+
const bookListSerializer = getSerializer("book-list", createResponseSchema({
|
|
82
|
+
type: "array",
|
|
83
|
+
items: bookSchema
|
|
84
|
+
}));
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Implementasi Create (dengan Validasi)
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
export async function createBook(req: Request, res: Response) {
|
|
91
|
+
// 1. Validasi Input
|
|
92
|
+
const validator = await Validator.make(req.body, {
|
|
93
|
+
title: "required|string|min:3",
|
|
94
|
+
author: "required|string",
|
|
95
|
+
isbn: "required|string|unique:books,isbn", // Cek unik di tabel books
|
|
96
|
+
stock: "required|number|min:1",
|
|
97
|
+
publishedAt: "required|string" // Format tanggal ISO
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
if (validator.fails()) {
|
|
101
|
+
return sendError(res, 400, "Validation Error", validator.errors());
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const data = validator.validated();
|
|
105
|
+
|
|
106
|
+
// 2. Simpan ke Database
|
|
107
|
+
const book = await prisma.book.create({
|
|
108
|
+
data: {
|
|
109
|
+
title: data.title,
|
|
110
|
+
author: data.author,
|
|
111
|
+
isbn: data.isbn,
|
|
112
|
+
stock: data.stock,
|
|
113
|
+
publishedAt: new Date(data.publishedAt)
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// 3. Return Response Cepat
|
|
118
|
+
return sendFastSuccess(res, 201, bookDetailSerializer, {
|
|
119
|
+
status: "success",
|
|
120
|
+
message: "Buku berhasil ditambahkan",
|
|
121
|
+
data: { ...book, id: book.id.toString() } // Konversi BigInt manual jika perlu
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Implementasi List (High Performance)
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
export async function getBooks(req: Request, res: Response) {
|
|
130
|
+
const books = await prisma.book.findMany({
|
|
131
|
+
take: 50, // Limit 50
|
|
132
|
+
orderBy: { created_at: "desc" }
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Convert BigInt to string sebelum passing ke serializer (opsional, tapi aman)
|
|
136
|
+
const safeBooks = books.map(b => ({ ...b, id: b.id.toString() }));
|
|
137
|
+
|
|
138
|
+
return sendFastSuccess(res, 200, bookListSerializer, {
|
|
139
|
+
status: "success",
|
|
140
|
+
message: "Daftar buku",
|
|
141
|
+
data: safeBooks
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
## Langkah 4: Daftarkan Route & Proteksi
|
|
147
|
+
|
|
148
|
+
Buka `src/routes/book.ts` (atau file yang digenerate). Pastikan route terhubung dan tambahkan middleware auth.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
import { Router } from "express";
|
|
152
|
+
import { createBook, getBooks } from "../controllers/bookController";
|
|
153
|
+
import { requireAuth, requireAdmin } from "../middleware/auth";
|
|
154
|
+
|
|
155
|
+
export const bookRouter = Router();
|
|
156
|
+
|
|
157
|
+
// Public route (bisa diakses siapa saja)
|
|
158
|
+
bookRouter.get("/", getBooks);
|
|
159
|
+
|
|
160
|
+
// Admin only (Butuh login + role admin)
|
|
161
|
+
bookRouter.post("/", requireAuth, requireAdmin, createBook);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Terakhir, daftarkan router ini di `src/routes/index.ts` (jika belum otomatis):
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
import { bookRouter } from "./book";
|
|
168
|
+
// ...
|
|
169
|
+
router.use("/books", bookRouter);
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Langkah 5: Testing
|
|
173
|
+
|
|
174
|
+
Jalankan server:
|
|
175
|
+
```bash
|
|
176
|
+
npm run dev
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Coba hit endpoint:
|
|
180
|
+
1. **POST /api/books** (Tanpa token) -> 401 Unauthorized.
|
|
181
|
+
2. **POST /api/books** (Token User Biasa) -> 403 Forbidden.
|
|
182
|
+
3. **POST /api/books** (Token Admin + Data Invalid) -> 400 Validation Error.
|
|
183
|
+
4. **POST /api/books** (Token Admin + Data Valid) -> 201 Created.
|
|
184
|
+
5. **GET /api/books** -> 200 OK (Super Cepat).
|
|
185
|
+
|
|
186
|
+
## Kesimpulan
|
|
187
|
+
|
|
188
|
+
Dengan Lapeh Framework, Anda telah membuat API yang:
|
|
189
|
+
- **Aman** (Validasi, Auth, RBAC).
|
|
190
|
+
- **Cepat** (Fast Serialization).
|
|
191
|
+
- **Rapi** (Struktur terstandarisasi).
|
|
192
|
+
- **Mudah** (CLI Generator).
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import globals from "globals";
|
|
2
|
+
import pluginJs from "@eslint/js";
|
|
3
|
+
import tseslint from "typescript-eslint";
|
|
4
|
+
|
|
5
|
+
export default [
|
|
6
|
+
{ files: ["**/*.{js,mjs,cjs,ts}"] },
|
|
7
|
+
{ languageOptions: { globals: globals.node } },
|
|
8
|
+
pluginJs.configs.recommended,
|
|
9
|
+
...tseslint.configs.recommended,
|
|
10
|
+
{
|
|
11
|
+
rules: {
|
|
12
|
+
"@typescript-eslint/no-unused-vars": [
|
|
13
|
+
"error",
|
|
14
|
+
{
|
|
15
|
+
"argsIgnorePattern": "^_",
|
|
16
|
+
"varsIgnorePattern": "^_",
|
|
17
|
+
"caughtErrorsIgnorePattern": "^_"
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
"@typescript-eslint/no-explicit-any": "warn"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
ignores: ["dist/", "node_modules/", "generated/", "scripts/"]
|
|
25
|
+
}
|
|
26
|
+
];
|
package/framework.md
CHANGED
|
@@ -1,113 +1,168 @@
|
|
|
1
|
-
# Lapeh Framework
|
|
2
|
-
|
|
3
|
-
## Quick Start
|
|
4
|
-
|
|
5
|
-
Untuk memulai project ini (Setup awal):
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm i
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
npm run first
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
Perintah di atas akan secara otomatis melakukan:
|
|
16
|
-
|
|
17
|
-
1. Copy `.env.example` ke `.env`
|
|
18
|
-
2. Install dependencies (`npm install`)
|
|
19
|
-
3. Generate JWT Secret baru di `.env`
|
|
20
|
-
4. Setup database (Migrate)
|
|
21
|
-
5. Menjalankan Database Seeder
|
|
22
|
-
|
|
23
|
-
Setelah selesai, Anda bisa langsung menjalankan project:
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
npm run dev
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
### Akun Default
|
|
30
|
-
|
|
31
|
-
Jika seeder dijalankan (via `npm run first` atau `npm run db:seed`), gunakan akun berikut:
|
|
32
|
-
|
|
33
|
-
- **Super Admin**: `sa@sa.com` / `string`
|
|
34
|
-
- **Admin**: `a@a.com` / `string`
|
|
35
|
-
- **User**: `u@u.com` / `string`
|
|
36
|
-
|
|
37
|
-
##
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
1
|
+
# Lapeh Framework
|
|
2
|
+
|
|
3
|
+
## Quick Start
|
|
4
|
+
|
|
5
|
+
Untuk memulai project ini (Setup awal):
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm run first
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Perintah di atas akan secara otomatis melakukan:
|
|
16
|
+
|
|
17
|
+
1. Copy `.env.example` ke `.env`
|
|
18
|
+
2. Install dependencies (`npm install`)
|
|
19
|
+
3. Generate JWT Secret baru di `.env`
|
|
20
|
+
4. Setup database (Migrate)
|
|
21
|
+
5. Menjalankan Database Seeder
|
|
22
|
+
|
|
23
|
+
Setelah selesai, Anda bisa langsung menjalankan project:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm run dev
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Akun Default
|
|
30
|
+
|
|
31
|
+
Jika seeder dijalankan (via `npm run first` atau `npm run db:seed`), gunakan akun berikut:
|
|
32
|
+
|
|
33
|
+
- **Super Admin**: `sa@sa.com` / `string`
|
|
34
|
+
- **Admin**: `a@a.com` / `string`
|
|
35
|
+
- **User**: `u@u.com` / `string`
|
|
36
|
+
|
|
37
|
+
## Code Standards & Best Practices (Baru)
|
|
38
|
+
|
|
39
|
+
### 1. Import Path Aliases
|
|
40
|
+
Gunakan alias `@/` untuk mengimpor module dari folder `src/`. Hindari relative path yang panjang seperti `../../utils/response`.
|
|
41
|
+
|
|
42
|
+
**Contoh:**
|
|
43
|
+
```typescript
|
|
44
|
+
// ✅ Benar (Recommended)
|
|
45
|
+
import { prisma } from "@/core/database";
|
|
46
|
+
import { sendSuccess } from "@/utils/response";
|
|
47
|
+
|
|
48
|
+
// ❌ Salah (Legacy)
|
|
49
|
+
import { prisma } from "../core/database";
|
|
50
|
+
import { sendSuccess } from "../../utils/response";
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 2. Strict Linting (Dead Code Elimination)
|
|
54
|
+
Framework ini menerapkan aturan linter yang ketat untuk menjaga kebersihan kode. Variabel, parameter, atau import yang tidak digunakan akan menyebabkan error.
|
|
55
|
+
|
|
56
|
+
- **Variabel tidak terpakai**: Hapus atau beri prefix `_` (underscore).
|
|
57
|
+
```typescript
|
|
58
|
+
// ✅ Benar
|
|
59
|
+
const _unusedVariable = 123;
|
|
60
|
+
function example(_req: Request, res: Response) { ... }
|
|
61
|
+
|
|
62
|
+
// ❌ Error
|
|
63
|
+
const unusedVariable = 123;
|
|
64
|
+
function example(req: Request, res: Response) { ... } // jika req tidak dipakai
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 3. High Performance Response (Fastify-Style)
|
|
68
|
+
Untuk endpoint dengan throughput tinggi (GET lists, data besar), gunakan `sendFastSuccess` dengan JSON Schema serializer. Ini 2-3x lebih cepat dari `res.json` standar Express.
|
|
69
|
+
|
|
70
|
+
**Langkah-langkah:**
|
|
71
|
+
|
|
72
|
+
1. **Definisikan Schema** (sesuai field Prisma):
|
|
73
|
+
```typescript
|
|
74
|
+
const userSchema = {
|
|
75
|
+
type: "object",
|
|
76
|
+
properties: {
|
|
77
|
+
id: { type: "string" }, // BigInt otomatis dicovert ke string
|
|
78
|
+
name: { type: "string" },
|
|
79
|
+
email: { type: "string" }
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
2. **Buat Serializer**:
|
|
85
|
+
```typescript
|
|
86
|
+
import { getSerializer, createResponseSchema } from "@/core/serializer";
|
|
87
|
+
|
|
88
|
+
const userSerializer = getSerializer("user-detail", createResponseSchema(userSchema));
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
3. **Gunakan di Controller**:
|
|
92
|
+
```typescript
|
|
93
|
+
import { sendFastSuccess } from "@/utils/response";
|
|
94
|
+
|
|
95
|
+
export async function getUser(req, res) {
|
|
96
|
+
const user = await prisma.user.findFirst();
|
|
97
|
+
sendFastSuccess(res, 200, userSerializer, {
|
|
98
|
+
status: "success",
|
|
99
|
+
message: "User found",
|
|
100
|
+
data: user
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Database Workflow (Prisma)
|
|
106
|
+
|
|
107
|
+
Framework ini menggunakan **Prisma ORM** dengan struktur schema yang modular (dipecah per file). Berikut adalah panduan lengkap dari Development hingga Deployment.
|
|
108
|
+
|
|
109
|
+
### 1. Development (Lokal)
|
|
110
|
+
|
|
111
|
+
Saat mengembangkan aplikasi di local environment:
|
|
112
|
+
|
|
113
|
+
**a. Mengupdate Schema Database**
|
|
114
|
+
Jika Anda mengubah file schema di `src/models/*.prisma` atau konfigurasi di `prisma/base.prisma.template`:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
npm run prisma:migrate
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
_Perintah ini akan menggabungkan semua file schema, membuat file migrasi baru, menerapkan ke database lokal, dan men-generate ulang Prisma Client._
|
|
121
|
+
|
|
122
|
+
**b. Melihat/Edit Data (GUI)**
|
|
123
|
+
Untuk membuka dashboard visual database:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
npm run db:studio
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
**c. Mengisi Data Awal (Seeding)**
|
|
130
|
+
Jika Anda butuh data dummy atau data awal (seperti roles/permissions):
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
npm run db:seed
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**d. Reset Database Total**
|
|
137
|
+
Jika database berantakan dan ingin mengulang dari awal (HATI-HATI: Menghapus semua data):
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
npm run db:reset
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
_Perintah ini akan menghapus database, membuat ulang schema dari awal, dan otomatis menjalankan seeder._
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
### 2. Deployment (Production)
|
|
148
|
+
|
|
149
|
+
Saat deploy ke server production:
|
|
150
|
+
|
|
151
|
+
**a. Setup Awal**
|
|
152
|
+
Pastikan `.env` di production sudah disetup dengan benar (DATABASE_URL, dll).
|
|
153
|
+
|
|
154
|
+
**b. Menerapkan Migrasi**
|
|
155
|
+
Jangan gunakan `migrate dev` di production. Gunakan perintah ini:
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
npm run prisma:deploy
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
_Perintah ini hanya akan menerapkan file migrasi yang sudah ada ke database production tanpa mereset data atau meminta konfirmasi interaktif._
|
|
162
|
+
|
|
163
|
+
**c. Generate Client (Opsional)**
|
|
164
|
+
Biasanya dilakukan otomatis saat `npm install` (karena `postinstall`), tapi jika perlu manual:
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
npm run prisma:generate
|
|
168
|
+
```
|
package/nodemon.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lapeh",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.3",
|
|
4
4
|
"description": "Framework API Express yang siap pakai (Standardized)",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -19,12 +19,14 @@
|
|
|
19
19
|
"dev": "node scripts/check-update.js && nodemon src/index.ts",
|
|
20
20
|
"first": "node scripts/init-project.js",
|
|
21
21
|
"prebuild": "npm run prisma:generate",
|
|
22
|
-
"build": "tsc",
|
|
22
|
+
"build": "tsc && tsc-alias",
|
|
23
23
|
"prestart": "npm run prisma:generate",
|
|
24
24
|
"start": "node dist/src/index.js",
|
|
25
25
|
"prestart:prod": "npm run prisma:generate",
|
|
26
26
|
"start:prod": "NODE_ENV=production node dist/src/index.js",
|
|
27
27
|
"typecheck": "tsc --noEmit",
|
|
28
|
+
"lint": "eslint .",
|
|
29
|
+
"lint:fix": "eslint . --fix",
|
|
28
30
|
"prisma:generate": "node scripts/compile-schema.js && prisma generate",
|
|
29
31
|
"prisma:migrate": "node scripts/compile-schema.js && prisma migrate dev",
|
|
30
32
|
"prisma:deploy": "node scripts/compile-schema.js && prisma migrate deploy",
|
|
@@ -69,6 +71,7 @@
|
|
|
69
71
|
"dotenv": "17.2.3",
|
|
70
72
|
"express": "5.2.1",
|
|
71
73
|
"express-rate-limit": "8.2.1",
|
|
74
|
+
"fast-json-stringify": "^6.1.1",
|
|
72
75
|
"helmet": "8.1.0",
|
|
73
76
|
"ioredis": "5.8.2",
|
|
74
77
|
"ioredis-mock": "^8.13.1",
|
|
@@ -83,6 +86,7 @@
|
|
|
83
86
|
"zod": "3.23.8"
|
|
84
87
|
},
|
|
85
88
|
"devDependencies": {
|
|
89
|
+
"@eslint/js": "^9.39.2",
|
|
86
90
|
"@types/bcryptjs": "2.4.6",
|
|
87
91
|
"@types/cors": "2.8.19",
|
|
88
92
|
"@types/express": "5.0.6",
|
|
@@ -91,9 +95,14 @@
|
|
|
91
95
|
"@types/node": "25.0.3",
|
|
92
96
|
"@types/pg": "8.16.0",
|
|
93
97
|
"@types/uuid": "10.0.0",
|
|
98
|
+
"eslint": "^9.39.2",
|
|
99
|
+
"globals": "^16.5.0",
|
|
94
100
|
"nodemon": "3.1.11",
|
|
95
101
|
"prisma": "7.2.0",
|
|
96
102
|
"ts-node": "10.9.2",
|
|
97
|
-
"
|
|
103
|
+
"tsc-alias": "^1.8.16",
|
|
104
|
+
"tsconfig-paths": "^4.2.0",
|
|
105
|
+
"typescript": "5.9.3",
|
|
106
|
+
"typescript-eslint": "^8.50.1"
|
|
98
107
|
}
|
|
99
108
|
}
|