lapeh 2.4.6 → 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/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/{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/{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
|
+
```
|
|
@@ -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).
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# Dokumentasi Perubahan Lapeh Framework
|
|
2
|
+
|
|
3
|
+
File ini mencatat semua perubahan, pembaruan, dan perbaikan yang dilakukan pada framework Lapeh, diurutkan berdasarkan tanggal.
|
|
4
|
+
|
|
5
|
+
## [2025-12-28] - Minggu, 28 Desember 2025 - Perbaikan Kompatibilitas & Automasi
|
|
6
|
+
|
|
7
|
+
### 🛠️ Perbaikan Bug (Bug Fixes)
|
|
8
|
+
|
|
9
|
+
- **Linux/Mac Path Compatibility (v2.4.1)**:
|
|
10
|
+
|
|
11
|
+
- Memperbaiki masalah `MODULE_NOT_FOUND` pada sistem operasi Linux dan macOS ketika path proyek mengandung spasi (misalnya: `/Folder Saya/Proyek Lapeh`).
|
|
12
|
+
- Mengubah logika escaping argumen pada `nodemon` di `bin/index.js` agar menggunakan _single quotes_ pada sistem berbasis Unix.
|
|
13
|
+
|
|
14
|
+
- **Auto Prisma Generate (v2.4.2)**:
|
|
15
|
+
|
|
16
|
+
- Memperbaiki error `Cannot find module '.prisma/client/default'` yang sering muncul setelah instalasi bersih.
|
|
17
|
+
- Menambahkan eksekusi otomatis `npx prisma generate` saat menjalankan perintah `npm run dev` dan `npm run build`.
|
|
18
|
+
- Memastikan Prisma Client selalu tersedia sebelum server berjalan, meningkatkan pengalaman pengguna baru.
|
|
19
|
+
|
|
20
|
+
- **PM2 Ecosystem Config (v2.4.4)**:
|
|
21
|
+
- Menambahkan file `ecosystem.config.js` secara otomatis ke dalam proyek baru dan proyek yang di-upgrade.
|
|
22
|
+
- File ini berisi konfigurasi siap pakai untuk menjalankan aplikasi dalam mode **Cluster** (load balancing) di production menggunakan PM2.
|
|
23
|
+
- Memperbarui dokumentasi `doc/DEPLOYMENT.md` dengan instruksi penggunaan PM2 yang baru.
|
|
24
|
+
|
|
25
|
+
## [2025-12-27] - Code Quality & Standardization Update
|
|
26
|
+
|
|
27
|
+
### 🚀 Fitur & Standarisasi
|
|
28
|
+
|
|
29
|
+
- **Standardized Import Paths**:
|
|
30
|
+
- Implementasi path alias `@/` untuk import yang lebih bersih (e.g., `import { prisma } from "@/core/database"`).
|
|
31
|
+
- Penghapusan penggunaan relative paths yang dalam (`../../../`).
|
|
32
|
+
- Konfigurasi `tsconfig.json` tanpa `baseUrl` (mengikuti standar TypeScript 6.0+).
|
|
33
|
+
- **Strict Linting & Code Quality**:
|
|
34
|
+
- Implementasi aturan **ESLint** ketat untuk mencegah "Dead Code".
|
|
35
|
+
- Error otomatis untuk variabel, parameter, dan import yang tidak digunakan (`no-unused-vars`).
|
|
36
|
+
- Script `npm run lint` dan `npm run lint:fix` untuk pembersihan kode otomatis.
|
|
37
|
+
- **Fastify-Style Standardization**:
|
|
38
|
+
- Penerapan standar respon cepat (`sendFastSuccess`) di seluruh controller (`AuthController`, `RbacController`, `PetController`).
|
|
39
|
+
- Penggunaan **Schema-based Serialization** untuk performa JSON maksimal.
|
|
40
|
+
- Konversi otomatis `BigInt` ke `string` dalam respon JSON.
|
|
41
|
+
|
|
42
|
+
## [2025-12-27] - High Performance & Scalability Update
|
|
43
|
+
|
|
44
|
+
### 🚀 Fitur Baru
|
|
45
|
+
|
|
46
|
+
- **High Performance Serialization (Fastify-Style)**:
|
|
47
|
+
- Implementasi `fast-json-stringify` untuk serialisasi JSON super cepat (2x-3x lebih cepat dari `JSON.stringify`).
|
|
48
|
+
- Helper `sendFastSuccess` di `src/utils/response.ts` untuk mem-bypass overhead Express.
|
|
49
|
+
- Caching schema serializer otomatis di `src/core/serializer.ts`.
|
|
50
|
+
- **Scalability & Clustering**:
|
|
51
|
+
- Dukungan **Load Balancing** dengan Nginx.
|
|
52
|
+
- Dukungan **Redis Clustering** untuk Rate Limiter (`rate-limit-redis`).
|
|
53
|
+
- File konfigurasi `docker-compose.cluster.yml` untuk simulasi cluster lokal (1 Nginx + 2 App Instances + 1 Redis).
|
|
54
|
+
- **Smart Error Handling**:
|
|
55
|
+
- Deteksi otomatis port bentrok (`EADDRINUSE`) saat startup.
|
|
56
|
+
- Memberikan saran command _copy-paste_ untuk mematikan process yang memblokir port (support Windows, Mac, Linux).
|
|
57
|
+
- **SEO Optimization**:
|
|
58
|
+
- Update metadata `package.json` dan `README.md` agar framework lebih mudah ditemukan di Google/NPM.
|
|
59
|
+
|
|
60
|
+
## [2025-12-27] - Pembaruan Struktur & Validasi
|
|
61
|
+
|
|
62
|
+
### 🚀 Fitur Baru
|
|
63
|
+
|
|
64
|
+
- **Laravel-style Validator**:
|
|
65
|
+
- Implementasi utility `Validator` baru di `src/utils/validator.ts` yang meniru gaya validasi Laravel.
|
|
66
|
+
- Mendukung rule string seperti `required|string|min:3|email`.
|
|
67
|
+
- Penambahan rule `unique` untuk pengecekan database otomatis (Prisma).
|
|
68
|
+
- Penambahan rule `mimes`, `image`, `max` (file size) untuk validasi upload file.
|
|
69
|
+
- Penambahan rule `sometimes` untuk field opsional.
|
|
70
|
+
- **Framework Hardening (Keamanan & Stabilitas)**:
|
|
71
|
+
- **Rate Limiting**: Middleware anti-spam/brute-force di `src/middleware/rateLimit.ts`.
|
|
72
|
+
- **Request Logger**: Pencatatan log request masuk di `src/middleware/requestLogger.ts`.
|
|
73
|
+
- **Health Check**: Endpoint `/` kini mengembalikan status kesehatan server.
|
|
74
|
+
- **Graceful Shutdown**: Penanganan penutupan koneksi Database dan Redis yang aman saat server berhenti (`SIGTERM`/`SIGINT`).
|
|
75
|
+
- **Environment Validation**: Validasi variabel `.env` wajib (seperti `DATABASE_URL`, `JWT_SECRET`) saat startup.
|
|
76
|
+
- **Struktur Folder Baru**:
|
|
77
|
+
- Pemisahan konfigurasi inti ke `src/core/` (`server.ts`, `database.ts`, `redis.ts`, `realtime.ts`) agar folder `src` lebih bersih.
|
|
78
|
+
- Sentralisasi route di `src/routes/index.ts` (WIP).
|
|
79
|
+
- **CLI Improvements**:
|
|
80
|
+
- `npx lapeh <project-name> --full` kini otomatis menjalankan server dev setelah instalasi selesai, sehingga user bisa langsung melihat hasil tanpa mengetik perintah tambahan.
|
|
81
|
+
|
|
82
|
+
### 🛠️ Perbaikan & Refactoring
|
|
83
|
+
|
|
84
|
+
- **Controller Refactoring**:
|
|
85
|
+
- `AuthController`: Migrasi ke `Validator` baru, termasuk validasi upload avatar.
|
|
86
|
+
- `PetController`: Migrasi ke `Validator` baru.
|
|
87
|
+
- `RbacController`: Migrasi sebagian ke `Validator` baru.
|
|
88
|
+
- **Pembersihan**:
|
|
89
|
+
- Penghapusan folder `src/schema/` (Zod schema lama) karena sudah digantikan oleh `Validator` utility.
|
|
90
|
+
- Penghapusan file duplikat/lama di root `src/` setelah migrasi ke `src/core/`.
|
|
91
|
+
|
|
92
|
+
### 📝 Catatan Teknis
|
|
93
|
+
|
|
94
|
+
- **Validator Async**: Method `fails()`, `passes()`, dan `validated()` kini bersifat `async` untuk mendukung pengecekan database (`unique`).
|
|
95
|
+
- **Type Safety**: Semua perubahan telah diverifikasi dengan `npm run typecheck`.
|
|
96
|
+
|
|
97
|
+
---
|