lapeh 2.4.5 → 2.4.7
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/en/CLI.md +101 -0
- package/doc/{DEPLOYMENT.md → en/DEPLOYMENT.md} +102 -1
- package/doc/en/FEATURES.md +99 -0
- package/doc/en/GETTING_STARTED.md +101 -0
- package/doc/en/INTRODUCTION.md +60 -0
- package/doc/en/STRUCTURE.md +90 -0
- package/doc/id/ARCHITECTURE_GUIDE.md +73 -0
- package/doc/id/CHANGELOG.md +97 -0
- package/doc/id/CHEATSHEET.md +94 -0
- package/doc/id/CONTRIBUTING.md +105 -0
- package/doc/id/DEPLOYMENT.md +245 -0
- package/doc/id/FAQ.md +81 -0
- package/doc/id/PACKAGES.md +66 -0
- package/doc/id/PERFORMANCE.md +91 -0
- package/doc/id/ROADMAP.md +93 -0
- package/doc/id/SECURITY.md +93 -0
- package/doc/id/TUTORIAL.md +192 -0
- package/package.json +2 -2
- /package/doc/{ARCHITECTURE_GUIDE.md → en/ARCHITECTURE_GUIDE.md} +0 -0
- /package/doc/{CHANGELOG.md → en/CHANGELOG.md} +0 -0
- /package/doc/{CHEATSHEET.md → en/CHEATSHEET.md} +0 -0
- /package/doc/{CONTRIBUTING.md → en/CONTRIBUTING.md} +0 -0
- /package/doc/{FAQ.md → en/FAQ.md} +0 -0
- /package/doc/{PACKAGES.md → en/PACKAGES.md} +0 -0
- /package/doc/{PERFORMANCE.md → en/PERFORMANCE.md} +0 -0
- /package/doc/{ROADMAP.md → en/ROADMAP.md} +0 -0
- /package/doc/{SECURITY.md → en/SECURITY.md} +0 -0
- /package/doc/{TUTORIAL.md → en/TUTORIAL.md} +0 -0
- /package/doc/{CLI.md → id/CLI.md} +0 -0
- /package/doc/{FEATURES.md → id/FEATURES.md} +0 -0
- /package/doc/{GETTING_STARTED.md → id/GETTING_STARTED.md} +0 -0
- /package/doc/{INTRODUCTION.md → id/INTRODUCTION.md} +0 -0
- /package/doc/{STRUCTURE.md → id/STRUCTURE.md} +0 -0
package/doc/en/CLI.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# CLI Tools & Scripts
|
|
2
|
+
|
|
3
|
+
Lapeh Framework comes with various CLI scripts to speed up the development process, ranging from code generation to database management.
|
|
4
|
+
|
|
5
|
+
All commands are executed using `npm run <command>`.
|
|
6
|
+
|
|
7
|
+
> **Info:** Behind the scenes, these `npm run` scripts call the internal framework CLI (`lapeh`). You can also run these commands directly using `npx lapeh <command>`.
|
|
8
|
+
|
|
9
|
+
## Core Commands
|
|
10
|
+
|
|
11
|
+
Main commands to run the application:
|
|
12
|
+
|
|
13
|
+
### 1. Development Server (`dev`)
|
|
14
|
+
Runs the server in development mode with hot-reload feature.
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm run dev
|
|
18
|
+
# or
|
|
19
|
+
npx lapeh dev
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### 2. Production Server (`start`)
|
|
23
|
+
Runs the server in production mode (ensure it has been built).
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm run start
|
|
27
|
+
# or
|
|
28
|
+
npx lapeh start
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### 3. Build Project (`build`)
|
|
32
|
+
Compiles TypeScript code to JavaScript in the `dist` folder.
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm run build
|
|
36
|
+
# or
|
|
37
|
+
npx lapeh build
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Code Generators
|
|
41
|
+
|
|
42
|
+
Use these commands to create boilerplate files automatically.
|
|
43
|
+
|
|
44
|
+
### 1. Create Complete Module (`make:module`)
|
|
45
|
+
Creates Controller, Route, and Model (Schema) at once.
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npm run make:module <module-name>
|
|
49
|
+
```
|
|
50
|
+
**Example:** `npm run make:module Product`
|
|
51
|
+
|
|
52
|
+
Output:
|
|
53
|
+
- `src/controllers/productController.ts`
|
|
54
|
+
- `src/routes/product.ts`
|
|
55
|
+
- `src/models/product.prisma`
|
|
56
|
+
|
|
57
|
+
### 2. Create Controller (`make:controller`)
|
|
58
|
+
Only creates a controller file with basic CRUD methods.
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
npm run make:controller <controller-name>
|
|
62
|
+
```
|
|
63
|
+
**Example:** `npm run make:controller Order` (Will create `src/controllers/orderController.ts`)
|
|
64
|
+
|
|
65
|
+
### 3. Create Database Model (`make:model`)
|
|
66
|
+
Only creates a new Prisma schema file.
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
npm run make:model <model-name>
|
|
70
|
+
```
|
|
71
|
+
**Example:** `npm run make:model Transaction` (Will create `src/models/transaction.prisma`)
|
|
72
|
+
|
|
73
|
+
## Database Management (Prisma)
|
|
74
|
+
|
|
75
|
+
This framework uses a **Multi-File Schema** system. You don't edit `schema.prisma` directly, but instead edit small files in `src/models/*.prisma`.
|
|
76
|
+
|
|
77
|
+
### 1. Database Migration (`prisma:migrate`)
|
|
78
|
+
Run this every time you change a model definition in `src/models/*.prisma`.
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
npm run prisma:migrate
|
|
82
|
+
```
|
|
83
|
+
This command will:
|
|
84
|
+
1. Merge all `.prisma` files in `src/models/` into one `prisma/schema.prisma`.
|
|
85
|
+
2. Create SQL migration files.
|
|
86
|
+
3. Apply changes to the local database.
|
|
87
|
+
4. Regenerate Prisma Client (Type Definitions).
|
|
88
|
+
|
|
89
|
+
### 2. Deploy to Production (`prisma:deploy`)
|
|
90
|
+
Use in production server. Only applies existing migrations without resetting data.
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
npm run prisma:deploy
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 3. Database Studio (`db:studio`)
|
|
97
|
+
Opens a GUI in the browser to view and edit database data.
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
npm run db:studio
|
|
101
|
+
```
|
|
@@ -49,21 +49,25 @@ npm run prisma:deploy
|
|
|
49
49
|
Lapeh kini menyertakan konfigurasi otomatis PM2 (`ecosystem.config.js`).
|
|
50
50
|
|
|
51
51
|
1. **Install PM2 Global**:
|
|
52
|
+
|
|
52
53
|
```bash
|
|
53
54
|
npm install -g pm2
|
|
54
55
|
```
|
|
55
56
|
|
|
56
57
|
2. **Jalankan Aplikasi**:
|
|
58
|
+
|
|
57
59
|
```bash
|
|
58
60
|
pm2 start ecosystem.config.js
|
|
59
61
|
```
|
|
60
62
|
|
|
61
63
|
Perintah ini akan:
|
|
64
|
+
|
|
62
65
|
- Menjalankan aplikasi dalam mode **Cluster** (menggunakan semua core CPU yang tersedia).
|
|
63
66
|
- Mengatur `NODE_ENV` ke `production`.
|
|
64
67
|
- Mengaktifkan auto-restart jika aplikasi crash atau penggunaan memori melebihi 1GB.
|
|
65
68
|
|
|
66
69
|
3. **Cek Status**:
|
|
70
|
+
|
|
67
71
|
```bash
|
|
68
72
|
pm2 status
|
|
69
73
|
pm2 logs
|
|
@@ -75,7 +79,104 @@ Lapeh kini menyertakan konfigurasi otomatis PM2 (`ecosystem.config.js`).
|
|
|
75
79
|
pm2 startup
|
|
76
80
|
```
|
|
77
81
|
|
|
78
|
-
###
|
|
82
|
+
### ❓ FAQ: Mengapa Aplikasi Saya Muncul Ganda di PM2?
|
|
83
|
+
|
|
84
|
+
Jika Anda menjalankan `pm2 list` dan melihat nama aplikasi Anda muncul lebih dari satu kali (misal: `my-app` ada 2 atau 4 baris), **JANGAN KHAWATIR**. Ini adalah fitur, bukan bug.
|
|
85
|
+
|
|
86
|
+
- **Penyebab**: Konfigurasi `instances: "max"` dan `exec_mode: "cluster"` di `ecosystem.config.js`.
|
|
87
|
+
- **Fungsi**: PM2 mendeteksi jumlah inti CPU (Core) di VPS Anda dan membuat 1 proses worker untuk setiap core.
|
|
88
|
+
- Jika VPS punya 2 vCPU -> Muncul 2 proses.
|
|
89
|
+
- Jika VPS punya 4 vCPU -> Muncul 4 proses.
|
|
90
|
+
- **Keuntungan**: Aplikasi Anda menjadi **Multi-Threaded**. Request yang masuk akan dibagi rata ke semua proses, meningkatkan performa 2x-4x lipat dibanding mode biasa.
|
|
91
|
+
|
|
92
|
+
**Cara mengubah ke Single Instance (Hemat RAM):**
|
|
93
|
+
Jika RAM server Anda terbatas (misal 512MB/1GB) dan ingin menghemat resource, ubah `ecosystem.config.js`:
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
module.exports = {
|
|
97
|
+
apps: [
|
|
98
|
+
{
|
|
99
|
+
name: "my-app",
|
|
100
|
+
// ...
|
|
101
|
+
instances: 1, // Ubah "max" menjadi 1
|
|
102
|
+
// ...
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
};
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Lalu jalankan `pm2 reload ecosystem.config.js`.
|
|
109
|
+
|
|
110
|
+
### 7. Advanced: Menjalankan Beberapa Aplikasi (Multi-App)
|
|
111
|
+
|
|
112
|
+
Jika Anda memiliki beberapa aplikasi Node.js dalam satu VPS (misalnya: Backend API, Frontend React SSR, dan API Lapeh), Anda bisa menggabungkannya dalam satu file `ecosystem.config.js`.
|
|
113
|
+
|
|
114
|
+
Berikut adalah contoh konfigurasi **Real World** untuk menjalankan 3 aplikasi sekaligus:
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
module.exports = {
|
|
118
|
+
apps: [
|
|
119
|
+
// 1. APLIKASI LAIN (Contoh: Backend MERN)
|
|
120
|
+
{
|
|
121
|
+
name: "api-mern-news",
|
|
122
|
+
cwd: "/var/www/html/node/api-mern-news",
|
|
123
|
+
script: "dist/src/index.js",
|
|
124
|
+
env: {
|
|
125
|
+
NODE_ENV: "production",
|
|
126
|
+
PORT: 4000,
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
// 2. APLIKASI LAIN (Contoh: Frontend React/Next.js)
|
|
131
|
+
{
|
|
132
|
+
name: "web-mern-news",
|
|
133
|
+
cwd: "/var/www/html/node/web-mern-news",
|
|
134
|
+
script: "npm",
|
|
135
|
+
args: "start", // Menjalankan 'npm start'
|
|
136
|
+
env: {
|
|
137
|
+
NODE_ENV: "production",
|
|
138
|
+
PORT: 3001,
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
// 3. APLIKASI LAPEH FRAMEWORK
|
|
143
|
+
{
|
|
144
|
+
name: "api-lapeh-project",
|
|
145
|
+
cwd: "/var/www/html/node/my-lapeh-project",
|
|
146
|
+
|
|
147
|
+
// PENTING: Gunakan binary Lapeh dari node_modules lokal
|
|
148
|
+
script: "./node_modules/lapeh/bin/index.js",
|
|
149
|
+
|
|
150
|
+
// Argument 'start' untuk mode produksi
|
|
151
|
+
args: "start",
|
|
152
|
+
|
|
153
|
+
// Mode Cluster (Gunakan semua Core CPU)
|
|
154
|
+
instances: "max",
|
|
155
|
+
exec_mode: "cluster",
|
|
156
|
+
|
|
157
|
+
// Restart jika memori bocor > 1GB
|
|
158
|
+
max_memory_restart: "1G",
|
|
159
|
+
|
|
160
|
+
// Matikan watch di production
|
|
161
|
+
watch: false,
|
|
162
|
+
|
|
163
|
+
env: {
|
|
164
|
+
NODE_ENV: "production",
|
|
165
|
+
PORT: 8001,
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
};
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
**Tips:**
|
|
173
|
+
|
|
174
|
+
1. Sesuaikan `cwd` (Current Working Directory) dengan lokasi folder proyek Anda di VPS.
|
|
175
|
+
2. Pastikan port tidak bentrok antar aplikasi (contoh di atas: 4000, 3001, 8001).
|
|
176
|
+
3. Simpan file ini di root folder proyek utama atau di folder khusus konfigurasi server Anda.
|
|
177
|
+
4. Jalankan semua aplikasi sekaligus dengan: `pm2 start ecosystem.config.js`.
|
|
178
|
+
|
|
179
|
+
### 8. Reverse Proxy (Nginx)
|
|
79
180
|
|
|
80
181
|
Jangan expose port 4000 langsung. Gunakan Nginx di depannya.
|
|
81
182
|
Config Nginx block:
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# Features & Core Concepts
|
|
2
|
+
|
|
3
|
+
This document explains the key features of Lapeh Framework and how to use them in depth.
|
|
4
|
+
|
|
5
|
+
## 1. Data Validation (Laravel-Style)
|
|
6
|
+
|
|
7
|
+
The framework provides a `Validator` utility inspired by Laravel, using `zod` behind the scenes but with an API that is more string-based and readable.
|
|
8
|
+
|
|
9
|
+
**Location:** `@lapeh/utils/validator`
|
|
10
|
+
|
|
11
|
+
### Basic Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Validator } from "@lapeh/utils/validator";
|
|
15
|
+
|
|
16
|
+
export async function createProduct(req: Request, res: Response) {
|
|
17
|
+
const validator = await Validator.make(req.body, {
|
|
18
|
+
name: "required|string|min:3",
|
|
19
|
+
price: "required|number|min:1000",
|
|
20
|
+
email: "required|email|unique:user,email", // Check unique in user table email column
|
|
21
|
+
category_id: "required|exists:category,id", // Check exists in category table id column
|
|
22
|
+
photo: "required|image|max:2048", // Validate file upload (Max 2MB)
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
if (validator.fails()) {
|
|
26
|
+
return sendError(res, 400, "Validation failed", validator.errors());
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const data = validator.validated();
|
|
30
|
+
// Continue saving process...
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Available Rules
|
|
35
|
+
|
|
36
|
+
- `required`: Must be filled.
|
|
37
|
+
- `string`, `number`, `boolean`: Data type.
|
|
38
|
+
- `email`: Valid email format.
|
|
39
|
+
- `min:X`, `max:X`: String length or number value.
|
|
40
|
+
- `unique:table,column`: Ensure value does not exist in database (Async).
|
|
41
|
+
- `exists:table,column`: Ensure value exists in database (Async).
|
|
42
|
+
- `image`: File must be an image (jpg, png, webp, etc).
|
|
43
|
+
- `mimes:types`: File must be a specific type (e.g., `mimes:pdf,docx`).
|
|
44
|
+
|
|
45
|
+
## 2. High Performance Response (Fastify-Style)
|
|
46
|
+
|
|
47
|
+
For endpoints requiring high performance (e.g., large data lists), use schema-based serialization. This is much faster than standard Express `res.json`.
|
|
48
|
+
|
|
49
|
+
**Location:** `@/utils/response`, `@/core/serializer`
|
|
50
|
+
|
|
51
|
+
### Implementation Steps
|
|
52
|
+
|
|
53
|
+
1. **Define Output Schema**
|
|
54
|
+
Match with the fields you want to show to the user.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
const productSchema = {
|
|
58
|
+
type: "object",
|
|
59
|
+
properties: {
|
|
60
|
+
id: { type: "string" }, // BigInt automatically becomes string
|
|
61
|
+
name: { type: "string" },
|
|
62
|
+
price: { type: "number" },
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
2. **Create Serializer (Cached)**
|
|
68
|
+
Store outside the handler function so it compiles only once.
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { getSerializer, createResponseSchema } from "@/core/serializer";
|
|
72
|
+
|
|
73
|
+
const productSerializer = getSerializer(
|
|
74
|
+
"product-single",
|
|
75
|
+
createResponseSchema(productSchema)
|
|
76
|
+
);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
3. **Send Response**
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { sendFastSuccess } from "@lapeh/utils/response";
|
|
83
|
+
|
|
84
|
+
// Inside controller
|
|
85
|
+
sendFastSuccess(res, 200, productSerializer, {
|
|
86
|
+
status: "success",
|
|
87
|
+
message: "Data retrieved",
|
|
88
|
+
data: productData,
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## 3. Authentication & Authorization (RBAC)
|
|
93
|
+
|
|
94
|
+
The authentication system uses JWT (JSON Web Token) and supports Role-Based Access Control.
|
|
95
|
+
|
|
96
|
+
### Auth Middleware
|
|
97
|
+
|
|
98
|
+
- `requireAuth`: Ensures user is logged in (sends header `Authorization: Bearer <token>`).
|
|
99
|
+
- `requireAdmin`: Ensures user is logged in AND has role `admin` or `super_admin`.
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# Getting Started with Lapeh Framework
|
|
2
|
+
|
|
3
|
+
Welcome to the official documentation for **Lapeh Framework**. This guide will help you get started with installation, configuration, and understanding the basic project structure.
|
|
4
|
+
|
|
5
|
+
## System Requirements
|
|
6
|
+
|
|
7
|
+
Before you begin, ensure your system meets the following requirements:
|
|
8
|
+
|
|
9
|
+
- **Node.js**: Version 18.x or newer.
|
|
10
|
+
- **Database**: PostgreSQL (Recommended) or MySQL/MariaDB.
|
|
11
|
+
- **Package Manager**: NPM (bundled with Node.js).
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
The easiest way to start is by using the `npx` CLI generator.
|
|
16
|
+
|
|
17
|
+
### 1. Create a New Project
|
|
18
|
+
|
|
19
|
+
Run the following command in your terminal:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npx lapeh@latest your-project-name
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Or for a full setup (with dummy user & role data):
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
npx lapeh@latest your-project-name --full
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### 2. Initial Setup
|
|
32
|
+
|
|
33
|
+
Once the project is created, navigate into the project directory and run the setup wizard:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
cd your-project-name
|
|
37
|
+
npm run first
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
This script will automatically perform the following:
|
|
41
|
+
|
|
42
|
+
1. Copy `.env.example` to `.env`.
|
|
43
|
+
2. Install all dependencies (`npm install`).
|
|
44
|
+
3. Generate a secure **JWT Secret**.
|
|
45
|
+
4. Run database migrations (create tables).
|
|
46
|
+
5. Run the seeder (populate initial data).
|
|
47
|
+
|
|
48
|
+
### 3. Run Development Server
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npm run dev
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The server will run at `http://localhost:4000` (or the port specified in `.env`).
|
|
55
|
+
|
|
56
|
+
## Directory Structure
|
|
57
|
+
|
|
58
|
+
Here is the standard folder structure of Lapeh Framework:
|
|
59
|
+
|
|
60
|
+
```
|
|
61
|
+
my-app/
|
|
62
|
+
├── bin/ # CLI scripts for npx
|
|
63
|
+
├── doc/ # Project documentation
|
|
64
|
+
├── prisma/ # Database Configuration & Schema
|
|
65
|
+
│ ├── migrations/ # Database migration history files
|
|
66
|
+
│ ├── base.prisma.template # Database configuration template
|
|
67
|
+
│ ├── schema.prisma # Combined schema file (Auto-generated)
|
|
68
|
+
│ └── seed.ts # Script for populating initial data
|
|
69
|
+
├── scripts/ # Utility scripts (Generator, Compiler)
|
|
70
|
+
├── src/ # Main application source code
|
|
71
|
+
│ ├── controllers/ # Business logic (Request handlers)
|
|
72
|
+
│ ├── core/ # Core configuration (DB, Redis, Server)
|
|
73
|
+
│ ├── middleware/ # Express Middleware (Auth, RateLimit)
|
|
74
|
+
│ ├── models/ # Prisma Schema definitions per feature
|
|
75
|
+
│ ├── routes/ # API routing definitions
|
|
76
|
+
│ ├── utils/ # Helper functions (Response, Validator)
|
|
77
|
+
│ └── index.ts # Application entry point
|
|
78
|
+
├── .env # Environment variables (Secrets)
|
|
79
|
+
├── package.json # NPM Dependencies & Scripts
|
|
80
|
+
└── tsconfig.json # TypeScript Configuration
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Environment Configuration (.env)
|
|
84
|
+
|
|
85
|
+
The `.env` file stores important configurations. Here are the key variables:
|
|
86
|
+
|
|
87
|
+
```ini
|
|
88
|
+
# Server
|
|
89
|
+
PORT=4000
|
|
90
|
+
NODE_ENV=development
|
|
91
|
+
|
|
92
|
+
# Database (Change according to your credentials)
|
|
93
|
+
DATABASE_URL="postgresql://user:password@localhost:5432/mydb?schema=public"
|
|
94
|
+
|
|
95
|
+
# Security
|
|
96
|
+
JWT_SECRET="super-long-and-random-secret"
|
|
97
|
+
ACCESS_TOKEN_EXPIRES_IN=3600 # 1 hour
|
|
98
|
+
|
|
99
|
+
# Redis (Optional - automatically mocked if absent)
|
|
100
|
+
REDIS_URL="redis://localhost:6379"
|
|
101
|
+
```
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Introduction to Lapeh Framework
|
|
2
|
+
|
|
3
|
+
## What is Lapeh?
|
|
4
|
+
|
|
5
|
+
**Lapeh** is a Backend Framework for Node.js built on top of **Express** and **TypeScript**.
|
|
6
|
+
|
|
7
|
+
If you have ever used **Laravel** (PHP) or **NestJS** (Node.js), you will feel very familiar. Lapeh adopts the philosophy of ease-of-use & clean structure from Laravel, while maintaining the flexibility and speed of Express.
|
|
8
|
+
|
|
9
|
+
The name "Lapeh" comes from the Minang language which means "Loose" or "Free", symbolizing the freedom for developers to build applications quickly without being burdened by complicated configurations.
|
|
10
|
+
|
|
11
|
+
## Why was Lapeh Created?
|
|
12
|
+
|
|
13
|
+
In the Node.js ecosystem, developers often experience "Decision Fatigue":
|
|
14
|
+
- "Which ORM to use? Prisma, TypeORM, or Drizzle?"
|
|
15
|
+
- "Validation using Joi, Zod, or express-validator?"
|
|
16
|
+
- "How about the folder structure? MVC? Clean Architecture?"
|
|
17
|
+
- "How to handle Auth?"
|
|
18
|
+
|
|
19
|
+
Lapeh answers all of that with **Opinionated Defaults**:
|
|
20
|
+
1. **ORM**: Prisma (Current industry standard).
|
|
21
|
+
2. **Validation**: Zod (with Laravel-style wrapper syntax).
|
|
22
|
+
3. **Structure**: Modular MVC (Controller, Model, Route separated but cohesive).
|
|
23
|
+
4. **Auth**: Ready-to-use JWT + RBAC (Role Based Access Control).
|
|
24
|
+
|
|
25
|
+
## Comparison with Other Frameworks
|
|
26
|
+
|
|
27
|
+
| Feature | Express (Raw) | NestJS | Lapeh Framework |
|
|
28
|
+
| :--- | :--- | :--- | :--- |
|
|
29
|
+
| **Learning Curve** | Low (but confusing structure) | High (Angular-style, Decorators) | **Medium** (Express + Clear Structure) |
|
|
30
|
+
| **Boilerplate** | Empty | Very Heavy | **Just Right (Ready to use)** |
|
|
31
|
+
| **Type Safety** | Manual | Strict | **Strict (Native TypeScript)** |
|
|
32
|
+
| **Dev Speed** | Slow (manual setup) | Medium | **Fast (CLI Generator)** |
|
|
33
|
+
| **Flexibility** | Very High | Rigid | **High** |
|
|
34
|
+
|
|
35
|
+
## "The Lapeh Way" Philosophy
|
|
36
|
+
|
|
37
|
+
1. **Developer Experience (DX) First**: CLI tools, clear error messages, and hot-reload are priorities.
|
|
38
|
+
2. **Performance by Default**: Fast JSON serialization (Fastify-style) and integrated Redis caching.
|
|
39
|
+
3. **Explicit is Better than Implicit**: No "magic" that is too dark. Your controller code is standard Express code that you understand.
|
|
40
|
+
4. **Production Ready**: Security (Helmet, Rate Limit) and Scalability (Docker, Cluster) are not afterthoughts, but built-in.
|
|
41
|
+
|
|
42
|
+
## Request Lifecycle
|
|
43
|
+
|
|
44
|
+
How does Lapeh handle a single request from a user?
|
|
45
|
+
|
|
46
|
+
1. **Incoming Request** (`GET /api/users`)
|
|
47
|
+
2. **Security Middleware**: Helmet (Headers), CORS, Rate Limiter.
|
|
48
|
+
3. **Global Middleware**: Request Logger, Body Parser (JSON).
|
|
49
|
+
4. **Routing**: Matching URL in `src/routes/`.
|
|
50
|
+
5. **Auth Middleware** (Optional): Check JWT token & Role.
|
|
51
|
+
6. **Validator** (Optional): Validate body/query input.
|
|
52
|
+
7. **Controller**: Main business logic executed.
|
|
53
|
+
- Call Database (Prisma).
|
|
54
|
+
- Call Cache (Redis).
|
|
55
|
+
8. **Serializer**: Data formatted & sanitized (e.g., hide password).
|
|
56
|
+
9. **Response**: JSON sent back to user.
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
**Next:** Learn about the folder structure in [Project Structure](structure.md).
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Project Structure Breakdown
|
|
2
|
+
|
|
3
|
+
To fully understand Lapeh Framework, you need to know what each file and folder does. Here is a complete "Tour" of the project directory.
|
|
4
|
+
|
|
5
|
+
## Root Directory
|
|
6
|
+
|
|
7
|
+
| File/Folder | Description |
|
|
8
|
+
| :------------------- | :----------------------------------------------------------------------------- |
|
|
9
|
+
| `bin/` | Contains execution scripts for CLI (`npx lapeh`). You rarely touch this. |
|
|
10
|
+
| `doc/` | Project documentation resides here. |
|
|
11
|
+
| `lib/` | **Framework Core**. Internal parts of the framework you rarely touch. |
|
|
12
|
+
| `prisma/` | The heart of Database configuration. |
|
|
13
|
+
| `scripts/` | Collection of Node.js utility scripts (generators, schema compilers, etc). |
|
|
14
|
+
| `src/` | **Main Source Code**. 99% of your coding happens here. |
|
|
15
|
+
| `.env` | Secret variables (Database URL, API Keys). **Do not commit this file to Git!** |
|
|
16
|
+
| `docker-compose.yml` | Docker configuration for running local Database & Redis. |
|
|
17
|
+
| `nodemon.json` | Auto-restart configuration during development. |
|
|
18
|
+
| `package.json` | List of libraries (dependencies) and commands (`npm run ...`). |
|
|
19
|
+
| `tsconfig.json` | TypeScript configuration. |
|
|
20
|
+
|
|
21
|
+
## `src/` Folder (Source Code - User Space)
|
|
22
|
+
|
|
23
|
+
This is where you work every day.
|
|
24
|
+
|
|
25
|
+
### `src/controllers/`
|
|
26
|
+
|
|
27
|
+
Contains application logic. Controllers receive Requests, process them, and return Responses.
|
|
28
|
+
|
|
29
|
+
- **Example**: `authController.ts` handles login/register.
|
|
30
|
+
- **Tip**: Do not put overly complex _business logic_ here. Use Services (optional) if the controller gets too fat.
|
|
31
|
+
|
|
32
|
+
### `src/models/`
|
|
33
|
+
|
|
34
|
+
Contains database table definitions (Prisma Schema).
|
|
35
|
+
|
|
36
|
+
- **Unique in Lapeh**: We break down the large `schema.prisma` into small files per feature (e.g., `user.prisma`, `product.prisma`) for easier management. The `prisma:migrate` script will merge them later.
|
|
37
|
+
|
|
38
|
+
### `src/routes/`
|
|
39
|
+
|
|
40
|
+
Defines endpoint URLs.
|
|
41
|
+
|
|
42
|
+
- Connects URLs (e.g., `/api/login`) to functions in Controllers.
|
|
43
|
+
- Attaches Middleware (e.g., `requireAuth`).
|
|
44
|
+
|
|
45
|
+
## `lib/` Folder (Framework Internals)
|
|
46
|
+
|
|
47
|
+
This part is similar to `node_modules` or the `.next` folder in Next.js. This is the framework engine.
|
|
48
|
+
|
|
49
|
+
### `lib/core/`
|
|
50
|
+
|
|
51
|
+
The "Engine" part of the framework.
|
|
52
|
+
|
|
53
|
+
- `server.ts`: Express App setup.
|
|
54
|
+
- `database.ts`: Prisma Client instance.
|
|
55
|
+
- `redis.ts`: Redis connection.
|
|
56
|
+
- `serializer.ts`: JSON Schema caching logic.
|
|
57
|
+
|
|
58
|
+
### `lib/middleware/`
|
|
59
|
+
|
|
60
|
+
Built-in framework middleware.
|
|
61
|
+
|
|
62
|
+
- `auth.ts`: Check JWT Token.
|
|
63
|
+
- `rateLimit.ts`: Limit request count.
|
|
64
|
+
- `requestLogger.ts`: Log every incoming request.
|
|
65
|
+
|
|
66
|
+
### `lib/utils/`
|
|
67
|
+
|
|
68
|
+
Built-in Helper functions.
|
|
69
|
+
|
|
70
|
+
- `validator.ts`: Laravel-style input validation.
|
|
71
|
+
- `response.ts`: Standard JSON response format (`sendFastSuccess`, `sendError`).
|
|
72
|
+
- `logger.ts`: Logging system (Winston).
|
|
73
|
+
|
|
74
|
+
## `prisma/` Folder
|
|
75
|
+
|
|
76
|
+
- `migrations/`: Database change history (SQL files). Do not edit manually.
|
|
77
|
+
- `base.prisma.template`: Header of the database schema (contains db datasource config).
|
|
78
|
+
- `seed.ts`: Script for populating initial data (Data Seeding).
|
|
79
|
+
|
|
80
|
+
## `scripts/` Folder
|
|
81
|
+
|
|
82
|
+
"Magic" scripts executed by `npm run`.
|
|
83
|
+
|
|
84
|
+
- `make-controller.js`: Controller generator.
|
|
85
|
+
- `compile-schema.js`: `.prisma` file merger.
|
|
86
|
+
- `init-project.js`: Initial setup wizard.
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
By understanding this structure, you won't get lost when adding new features or debugging.
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# Panduan Arsitektur: Menuju "Framework as a Dependency" (Next.js Style)
|
|
2
|
+
|
|
3
|
+
Saat ini, Lapeh menggunakan pendekatan **Boilerplate** (seperti Laravel), di mana pengguna mendapatkan seluruh kode sumber (`src/`) dan bertanggung jawab atas `express`, `prisma`, dll.
|
|
4
|
+
|
|
5
|
+
Untuk mengubahnya menjadi seperti **Next.js** (di mana pengguna hanya menginstall `lapeh` dan `package.json` mereka bersih), kita perlu mengubah arsitektur menjadi **Library**.
|
|
6
|
+
|
|
7
|
+
## 1. Perbedaan Utama
|
|
8
|
+
|
|
9
|
+
| Fitur | Boilerplate (Lapeh Saat Ini) | Library (Next.js Style) |
|
|
10
|
+
| :--- | :--- | :--- |
|
|
11
|
+
| **Instalasi** | `git clone` / `npx create-lapeh` | `npm install lapeh` |
|
|
12
|
+
| **package.json** | Banyak dependency (`express`, `cors`, dll) | Sedikit (`lapeh`, `react`) |
|
|
13
|
+
| **Scripts** | Panjang (`nodemon src/index.ts`) | Pendek (`lapeh dev`) |
|
|
14
|
+
| **Core Code** | Terbuka di `src/core/` | Tersembunyi di `node_modules/lapeh` |
|
|
15
|
+
| **Update** | Susah (harus merge manual) | Mudah (`npm update lapeh`) |
|
|
16
|
+
|
|
17
|
+
## 2. Langkah Implementasi
|
|
18
|
+
|
|
19
|
+
Saya telah memulai langkah pertama dengan menambahkan **CLI Runner** di `bin/index.js`.
|
|
20
|
+
|
|
21
|
+
### A. Update CLI (`bin/index.js`) ✅ (Sudah Dilakukan)
|
|
22
|
+
Saya sudah menambahkan command `dev`, `start`, dan `build` ke dalam CLI Lapeh. Ini memungkinkan pengguna menjalankan server tanpa tahu perintah aslinya.
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
// Contoh penggunaan nanti:
|
|
26
|
+
"scripts": {
|
|
27
|
+
"dev": "lapeh dev",
|
|
28
|
+
"build": "lapeh build",
|
|
29
|
+
"start": "lapeh start"
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### B. Struktur Project Pengguna (Target)
|
|
34
|
+
Nantinya, project pengguna Lapeh hanya akan berisi file bisnis mereka:
|
|
35
|
+
|
|
36
|
+
```text
|
|
37
|
+
my-app/
|
|
38
|
+
├── src/
|
|
39
|
+
│ ├── controllers/
|
|
40
|
+
│ ├── routes/
|
|
41
|
+
│ └── models/
|
|
42
|
+
├── lapeh.config.ts <-- Konfigurasi framework (pengganti edit core)
|
|
43
|
+
└── package.json
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Dan `package.json` mereka akan terlihat seperti ini:
|
|
47
|
+
|
|
48
|
+
```json
|
|
49
|
+
{
|
|
50
|
+
"name": "my-app",
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"lapeh": "^2.0.0"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"dev": "lapeh dev",
|
|
56
|
+
"build": "lapeh build",
|
|
57
|
+
"start": "lapeh start"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### C. Apa yang Harus Dilakukan Selanjutnya?
|
|
63
|
+
|
|
64
|
+
1. **Publish Package**: Anda perlu mempublish folder framework ini ke NPM (atau private registry).
|
|
65
|
+
* Pastikan `express`, `cors`, `helmet`, dll ada di `dependencies` (bukan `devDependencies`).
|
|
66
|
+
2. **Abstraksi `src/index.ts`**:
|
|
67
|
+
* Saat ini `src/index.ts` adalah entry point yang diedit user.
|
|
68
|
+
* Ubah agar `lapeh dev` menjalankan server internal yang **mengimpor** routes/controller user secara dinamis (seperti Next.js pages router).
|
|
69
|
+
3. **Config Loader**:
|
|
70
|
+
* Buat sistem pembacaan `lapeh.config.ts` untuk mengatur Port, Database URL, dll tanpa mengedit kode core.
|
|
71
|
+
|
|
72
|
+
## 3. Kesimpulan
|
|
73
|
+
Perubahan yang saya lakukan di `bin/index.js` adalah fondasi untuk CLI style. Untuk mencapai "Clean package.json" sepenuhnya, Anda harus memisahkan **Framework Core** (repo ini) dengan **User Project** (repo baru yang menginstall framework ini).
|