lapeh 2.4.6 → 2.4.8
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/dist/lib/utils/validator.d.ts +3 -3
- package/dist/lib/utils/validator.d.ts.map +1 -1
- package/dist/lib/utils/validator.js +4 -4
- package/doc/{CHANGELOG.md → en/CHANGELOG.md} +2 -2
- package/doc/{CHEATSHEET.md → en/CHEATSHEET.md} +1 -1
- package/doc/en/CLI.md +101 -0
- package/doc/en/FEATURES.md +99 -0
- package/doc/en/GETTING_STARTED.md +101 -0
- package/doc/en/INTRODUCTION.md +62 -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/{FEATURES.md → id/FEATURES.md} +5 -3
- package/doc/{INTRODUCTION.md → id/INTRODUCTION.md} +10 -8
- 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/{STRUCTURE.md → id/STRUCTURE.md} +1 -1
- package/doc/id/TUTORIAL.md +192 -0
- package/lib/utils/validator.ts +4 -4
- package/package.json +4 -4
- package/readme.md +1 -1
- /package/doc/{ARCHITECTURE_GUIDE.md → en/ARCHITECTURE_GUIDE.md} +0 -0
- /package/doc/{CONTRIBUTING.md → en/CONTRIBUTING.md} +0 -0
- /package/doc/{DEPLOYMENT.md → en/DEPLOYMENT.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/{GETTING_STARTED.md → id/GETTING_STARTED.md} +0 -0
|
@@ -10,11 +10,11 @@ export declare class Validator {
|
|
|
10
10
|
/**
|
|
11
11
|
* Create a new Validator instance
|
|
12
12
|
* @param data The input data to validate
|
|
13
|
-
* @param schema Zod schema or object of Zod schemas /
|
|
14
|
-
* @param messages Optional custom error messages (
|
|
13
|
+
* @param schema Zod schema or object of Zod schemas / string-based rules
|
|
14
|
+
* @param messages Optional custom error messages (style: 'field.rule' => 'message')
|
|
15
15
|
*/
|
|
16
16
|
static make(data: any, schema: ZodSchema<any> | Record<string, any>, messages?: Record<string, string>): Validator;
|
|
17
|
-
private static
|
|
17
|
+
private static parseStringRule;
|
|
18
18
|
/**
|
|
19
19
|
* Check if validation fails
|
|
20
20
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../../lib/utils/validator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAK,SAAS,EAAsB,MAAM,KAAK,CAAC;AAGvD,qBAAa,SAAS;IACpB,OAAO,CAAC,IAAI,CAAM;IAClB,OAAO,CAAC,MAAM,CAAiB;IAC/B,OAAO,CAAC,cAAc,CAAyB;IAC/C,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,WAAW,CAAa;IAChC,OAAO,CAAC,MAAM,CAAkB;gBAG9B,IAAI,EAAE,GAAG,EACT,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC5C,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM;IAavC;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CACT,IAAI,EAAE,GAAG,EACT,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC5C,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM;IAiDvC,OAAO,CAAC,MAAM,CAAC,
|
|
1
|
+
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../../lib/utils/validator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAK,SAAS,EAAsB,MAAM,KAAK,CAAC;AAGvD,qBAAa,SAAS;IACpB,OAAO,CAAC,IAAI,CAAM;IAClB,OAAO,CAAC,MAAM,CAAiB;IAC/B,OAAO,CAAC,cAAc,CAAyB;IAC/C,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,WAAW,CAAa;IAChC,OAAO,CAAC,MAAM,CAAkB;gBAG9B,IAAI,EAAE,GAAG,EACT,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC5C,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM;IAavC;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CACT,IAAI,EAAE,GAAG,EACT,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC5C,QAAQ,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM;IAiDvC,OAAO,CAAC,MAAM,CAAC,eAAe;IAyN9B;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC;IAK/B;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC;IAIhC;;OAEG;IACH,MAAM;IAKN;;OAEG;IACG,SAAS;YAUD,GAAG;IAgBjB,OAAO,CAAC,YAAY;IAuBpB,OAAO,CAAC,gBAAgB;CAoDzB"}
|
|
@@ -21,8 +21,8 @@ class Validator {
|
|
|
21
21
|
/**
|
|
22
22
|
* Create a new Validator instance
|
|
23
23
|
* @param data The input data to validate
|
|
24
|
-
* @param schema Zod schema or object of Zod schemas /
|
|
25
|
-
* @param messages Optional custom error messages (
|
|
24
|
+
* @param schema Zod schema or object of Zod schemas / string-based rules
|
|
25
|
+
* @param messages Optional custom error messages (style: 'field.rule' => 'message')
|
|
26
26
|
*/
|
|
27
27
|
static make(data, schema, messages = {}) {
|
|
28
28
|
if (schema instanceof zod_1.ZodSchema) {
|
|
@@ -44,7 +44,7 @@ class Validator {
|
|
|
44
44
|
sameRules.push({ field: key, target: args });
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
-
parsedSchema[key] = Validator.
|
|
47
|
+
parsedSchema[key] = Validator.parseStringRule(rule);
|
|
48
48
|
}
|
|
49
49
|
else {
|
|
50
50
|
parsedSchema[key] = rule;
|
|
@@ -66,7 +66,7 @@ class Validator {
|
|
|
66
66
|
}
|
|
67
67
|
return new Validator(data, objectSchema, messages);
|
|
68
68
|
}
|
|
69
|
-
static
|
|
69
|
+
static parseStringRule(rule) {
|
|
70
70
|
const rules = Array.isArray(rule)
|
|
71
71
|
? rule
|
|
72
72
|
: rule.split("|").map((r) => r.trim());
|
|
@@ -61,8 +61,8 @@ File ini mencatat semua perubahan, pembaruan, dan perbaikan yang dilakukan pada
|
|
|
61
61
|
|
|
62
62
|
### 🚀 Fitur Baru
|
|
63
63
|
|
|
64
|
-
- **
|
|
65
|
-
- Implementasi utility `Validator` baru di `src/utils/validator.ts`
|
|
64
|
+
- **Expressive Validator**:
|
|
65
|
+
- Implementasi utility `Validator` baru di `src/utils/validator.ts` dengan gaya validasi yang lebih ekspresif.
|
|
66
66
|
- Mendukung rule string seperti `required|string|min:3|email`.
|
|
67
67
|
- Penambahan rule `unique` untuk pengecekan database otomatis (Prisma).
|
|
68
68
|
- Penambahan rule `mimes`, `image`, `max` (file size) untuk validasi upload file.
|
|
@@ -18,7 +18,7 @@ Referensi cepat untuk perintah dan kode yang sering digunakan.
|
|
|
18
18
|
| **`npm run db:seed`** | Isi data dummy. |
|
|
19
19
|
| **`npm run db:reset`** | Hapus DB & mulai dari nol. |
|
|
20
20
|
|
|
21
|
-
## 🛡️ Validator Rules (
|
|
21
|
+
## 🛡️ Validator Rules (Simple Syntax)
|
|
22
22
|
|
|
23
23
|
Gunakan di `Validator.make(data, rules)`.
|
|
24
24
|
|
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
|
+
```
|
|
@@ -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 (Simple & Powerful)
|
|
6
|
+
|
|
7
|
+
The framework provides a `Validator` utility inspired by expressive modern validation styles, 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-Like)
|
|
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,62 @@
|
|
|
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 other modern frameworks, you will feel very familiar. Lapeh adopts the philosophy of ease-of-use & clean structure, 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
|
+
|
|
15
|
+
- "Which ORM to use? Prisma, TypeORM, or Drizzle?"
|
|
16
|
+
- "Validation using Joi, Zod, or express-validator?"
|
|
17
|
+
- "How about the folder structure? MVC? Clean Architecture?"
|
|
18
|
+
- "How to handle Auth?"
|
|
19
|
+
|
|
20
|
+
Lapeh answers all of that with **Opinionated Defaults**:
|
|
21
|
+
|
|
22
|
+
1. **ORM**: Prisma (Current industry standard).
|
|
23
|
+
2. **Validation**: Zod (Powerful and readable schema validation).
|
|
24
|
+
3. **Structure**: Modular MVC (Controller, Model, Route separated but cohesive).
|
|
25
|
+
4. **Auth**: Ready-to-use JWT + RBAC (Role Based Access Control).
|
|
26
|
+
|
|
27
|
+
## Comparison with Other Frameworks
|
|
28
|
+
|
|
29
|
+
| Feature | Express (Raw) | NestJS | Lapeh Framework |
|
|
30
|
+
| :----------------- | :---------------------------- | :------------------------------- | :------------------------------------- |
|
|
31
|
+
| **Learning Curve** | Low (but confusing structure) | High (Angular-style, Decorators) | **Medium** (Express + Clear Structure) |
|
|
32
|
+
| **Boilerplate** | Empty | Very Heavy | **Just Right (Ready to use)** |
|
|
33
|
+
| **Type Safety** | Manual | Strict | **Strict (Native TypeScript)** |
|
|
34
|
+
| **Dev Speed** | Slow (manual setup) | Medium | **Fast (CLI Generator)** |
|
|
35
|
+
| **Flexibility** | Very High | Rigid | **High** |
|
|
36
|
+
|
|
37
|
+
## "The Lapeh Way" Philosophy
|
|
38
|
+
|
|
39
|
+
1. **Developer Experience (DX) First**: CLI tools, clear error messages, and hot-reload are priorities.
|
|
40
|
+
2. **Performance by Default**: Fast JSON serialization (Fastify-style) and integrated Redis caching.
|
|
41
|
+
3. **Explicit is Better than Implicit**: No "magic" that is too dark. Your controller code is standard Express code that you understand.
|
|
42
|
+
4. **Production Ready**: Security (Helmet, Rate Limit) and Scalability (Docker, Cluster) are not afterthoughts, but built-in.
|
|
43
|
+
|
|
44
|
+
## Request Lifecycle
|
|
45
|
+
|
|
46
|
+
How does Lapeh handle a single request from a user?
|
|
47
|
+
|
|
48
|
+
1. **Incoming Request** (`GET /api/users`)
|
|
49
|
+
2. **Security Middleware**: Helmet (Headers), CORS, Rate Limiter.
|
|
50
|
+
3. **Global Middleware**: Request Logger, Body Parser (JSON).
|
|
51
|
+
4. **Routing**: Matching URL in `src/routes/`.
|
|
52
|
+
5. **Auth Middleware** (Optional): Check JWT token & Role.
|
|
53
|
+
6. **Validator** (Optional): Validate body/query input.
|
|
54
|
+
7. **Controller**: Main business logic executed.
|
|
55
|
+
- Call Database (Prisma).
|
|
56
|
+
- Call Cache (Redis).
|
|
57
|
+
8. **Serializer**: Data formatted & sanitized (e.g., hide password).
|
|
58
|
+
9. **Response**: JSON sent back to user.
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
**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`: Powerful and expressive 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).
|