lapeh 1.0.1 → 1.0.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/readme.md CHANGED
@@ -1,120 +1,126 @@
1
- # API Lapeh Framework
2
-
3
- **API Lapeh** adalah framework berbasis Express.js yang terstandarisasi, dirancang untuk mempercepat pengembangan REST API dengan struktur yang solid, aman, dan scalable. Terinspirasi oleh struktur Laravel dan NestJS, namun tetap menjaga kesederhanaan Express.
4
-
5
- ## šŸš€ Fitur Utama
6
-
7
- - **Struktur Modular**: Terorganisir rapi dengan Controllers, Services, Routes, dan Middleware.
8
- - **TypeScript Ready**: Full TypeScript support untuk type-safety.
9
- - **Prisma ORM**: Integrasi database yang modern dan type-safe.
10
- - **Schema Terpisah**: Mendukung pemisahan schema Prisma per model (mirip Eloquent).
11
- - **Generator Tools**: CLI commands untuk generate Module dan Model dengan cepat.
12
- - **Security Best Practices**: Dilengkapi dengan Helmet, Rate Limiting, CORS, dan JWT Authentication.
13
- - **Validasi Data**: Menggunakan Zod untuk validasi request yang kuat.
14
-
15
- ## šŸ“¦ Instalasi & Penggunaan
16
-
17
- Buat project baru cukup dengan satu perintah:
18
-
19
- ```bash
20
- npx lapeh-cli nama-project-anda
21
- ```
22
-
23
- ### Apa yang terjadi otomatis?
24
-
25
- 1. Struktur project dibuat.
26
- 2. Dependencies diinstall.
27
- 3. Environment variable (`.env`) disiapkan.
28
- 4. **JWT Secret** di-generate otomatis.
29
-
30
- Masuk ke folder project dan jalankan:
31
-
32
- ```bash
33
- cd nama-project-anda
34
- npm run dev
35
- ```
36
-
37
- Server akan berjalan di `http://localhost:4000`.
38
-
39
- ---
40
-
41
- ## šŸ›  Development Tools
42
-
43
- API Lapeh menyediakan tools untuk mempercepat development, mirip dengan `artisan` di Laravel.
44
-
45
- ### 1. Membuat Module (Resource)
46
-
47
- Membuat Controller, Service, dan Route sekaligus.
48
-
49
- ```bash
50
- npm run make:module NamaResource
51
- # Contoh: npm run make:module Product
52
- ```
53
-
54
- Command ini akan membuat:
55
-
56
- - `src/controllers/product.controller.ts`
57
- - `src/services/product.service.ts`
58
- - `src/routes/product.route.ts` (dan otomatis didaftarkan di `src/routes/index.ts` jika memungkinkan)
59
-
60
- ### 2. Membuat Model Database
61
-
62
- Membuat file model Prisma baru di dalam folder `src/models/` (atau `prisma/models` tergantung konfigurasi).
63
-
64
- ```bash
65
- npm run make:model NamaModel
66
- # Contoh: npm run make:model User
67
- ```
68
-
69
- Ini akan membuat file `src/models/User.prisma`.
70
-
71
- ### 3. Workflow Database (Prisma)
72
-
73
- Karena framework ini menggunakan **Schema Terpisah** (split schema), Anda **TIDAK BOLEH** mengedit `prisma/schema.prisma` secara manual.
74
-
75
- - **Edit Models**: Lakukan perubahan di `src/models/*.prisma`.
76
- - **Apply Changes**: Jalankan perintah migrasi standar, sistem akan otomatis menggabungkan (compile) schema Anda.
77
-
78
- ```bash
79
- # Generate Prisma Client (setiap ada perubahan model)
80
- npm run prisma:generate
81
-
82
- # Migrasi Database (Development)
83
- npm run prisma:migrate
84
-
85
- # Deploy ke Production
86
- npm run prisma:deploy
87
- ```
88
-
89
- > **Catatan:** Script `compile-schema.js` akan otomatis berjalan sebelum perintah prisma di atas dieksekusi.
90
-
91
- ### 4. Generate JWT Secret
92
-
93
- Jika Anda perlu me-refresh secret key JWT:
94
-
95
- ```bash
96
- npm run generate:jwt
97
- ```
98
-
99
- ---
100
-
101
- ## šŸ“‚ Struktur Folder
102
-
103
- ```text
104
- src/
105
- ā”œā”€ā”€ controllers/ # Logika Request & Response
106
- ā”œā”€ā”€ services/ # Business Logic
107
- ā”œā”€ā”€ routes/ # Definisi Route API
108
- ā”œā”€ā”€ models/ # Definisi Schema Prisma per Model
109
- ā”œā”€ā”€ middleware/ # Auth, Validation, Error Handling
110
- ā”œā”€ā”€ schema/ # Zod Validation Schemas
111
- ā”œā”€ā”€ utils/ # Helper Functions
112
- └── index.ts # App Entry Point
113
- prisma/
114
- ā”œā”€ā”€ schema.prisma # [GENERATED] Jangan edit file ini
115
- └── base.prisma # Konfigurasi Datasource & Generator
116
- ```
117
-
118
- ## šŸ“ Lisensi
119
-
120
- MIT
1
+ # Lapeh Framework
2
+
3
+ **Lapeh** adalah framework berbasis Express.js yang terstandarisasi, dirancang untuk mempercepat pengembangan REST API dengan struktur yang solid, aman, dan scalable. Terinspirasi oleh struktur Laravel dan NestJS, namun tetap menjaga kesederhanaan Express.
4
+
5
+ ## šŸš€ Fitur Utama
6
+
7
+ - **Struktur Modular**: Terorganisir rapi dengan Controllers, Services, Routes, dan Middleware.
8
+ - **TypeScript Ready**: Full TypeScript support untuk type-safety.
9
+ - **Prisma ORM**: Integrasi database yang modern dan type-safe.
10
+ - **Schema Terpisah**: Mendukung pemisahan schema Prisma per model (mirip Eloquent).
11
+ - **Generator Tools**: CLI commands untuk generate Module dan Model dengan cepat.
12
+ - **Security Best Practices**: Dilengkapi dengan Helmet, Rate Limiting, CORS, dan JWT Authentication.
13
+ - **Validasi Data**: Menggunakan Zod untuk validasi request yang kuat.
14
+
15
+ ## šŸ“¦ Instalasi & Penggunaan
16
+
17
+ Buat project baru cukup dengan satu perintah:
18
+
19
+ ```bash
20
+ npx lapeh nama-project-anda
21
+ ```
22
+
23
+ ### Apa yang terjadi otomatis?
24
+
25
+ 1. Struktur project dibuat.
26
+ 2. Dependencies diinstall.
27
+ 3. Environment variable (`.env`) disiapkan.
28
+ 4. **JWT Secret** di-generate otomatis.
29
+
30
+ Masuk ke folder project dan jalankan:
31
+
32
+ ```bash
33
+ cd nama-project-anda
34
+ npm run dev
35
+ ```
36
+
37
+ Server akan berjalan di `http://localhost:4000`.
38
+
39
+ ---
40
+
41
+ ## šŸ›  Development Tools
42
+
43
+ API Lapeh menyediakan tools untuk mempercepat development, mirip dengan `artisan` di Laravel.
44
+
45
+ ### 1. Membuat Module (Resource)
46
+
47
+ Membuat Controller, Service, dan Route sekaligus.
48
+
49
+ ```bash
50
+ npm run make:module NamaResource
51
+ # Contoh: npm run make:module Product
52
+ ```
53
+
54
+ Command ini akan membuat:
55
+
56
+ - `src/controllers/product.controller.ts`
57
+ - `src/services/product.service.ts`
58
+ - `src/routes/product.route.ts` (dan otomatis didaftarkan di `src/routes/index.ts` jika memungkinkan)
59
+
60
+ ### 2. Membuat Model Database
61
+
62
+ Membuat file model Prisma baru di dalam folder `src/models/` (atau `prisma/models` tergantung konfigurasi).
63
+
64
+ ```bash
65
+ npm run make:model NamaModel
66
+ # Contoh: npm run make:model User
67
+ ```
68
+
69
+ Ini akan membuat file `src/models/User.prisma`.
70
+
71
+ ### 3. Workflow Database (Prisma)
72
+
73
+ Karena framework ini menggunakan **Schema Terpisah** (split schema), Anda **TIDAK BOLEH** mengedit `prisma/schema.prisma` secara manual.
74
+
75
+ - **Edit Models**: Lakukan perubahan di `src/models/*.prisma`.
76
+ - **Apply Changes**: Jalankan perintah migrasi standar, sistem akan otomatis menggabungkan (compile) schema Anda.
77
+
78
+ ```bash
79
+ # Generate Prisma Client (setiap ada perubahan model)
80
+ npm run prisma:generate
81
+
82
+ # Migrasi Database (Development)
83
+ npm run prisma:migrate
84
+
85
+ # Membuka GUI Database (Prisma Studio)
86
+ npm run db:studio
87
+
88
+ # Migrasi Database Dan Seed (Development - Reset Total default option for development)
89
+ npm run db:reset
90
+
91
+ # Deploy ke Production
92
+ npm run prisma:deploy
93
+ ```
94
+
95
+ > **Catatan:** Script `compile-schema.js` akan otomatis berjalan sebelum perintah prisma di atas dieksekusi.
96
+
97
+ ### 4. Generate JWT Secret
98
+
99
+ Jika Anda perlu me-refresh secret key JWT:
100
+
101
+ ```bash
102
+ npm run generate:jwt
103
+ ```
104
+
105
+ ---
106
+
107
+ ## šŸ“‚ Struktur Folder
108
+
109
+ ```text
110
+ src/
111
+ ā”œā”€ā”€ controllers/ # Logika Request & Response
112
+ ā”œā”€ā”€ services/ # Business Logic
113
+ ā”œā”€ā”€ routes/ # Definisi Route API
114
+ ā”œā”€ā”€ models/ # Definisi Schema Prisma per Model
115
+ ā”œā”€ā”€ middleware/ # Auth, Validation, Error Handling
116
+ ā”œā”€ā”€ schema/ # Zod Validation Schemas
117
+ ā”œā”€ā”€ utils/ # Helper Functions
118
+ └── index.ts # App Entry Point
119
+ prisma/
120
+ ā”œā”€ā”€ schema.prisma # [GENERATED] Jangan edit file ini
121
+ └── base.prisma # Konfigurasi Datasource & Generator
122
+ ```
123
+
124
+ ## šŸ“ Lisensi
125
+
126
+ MIT
@@ -1,38 +1,38 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
- const crypto = require('crypto');
4
-
5
- const envPath = path.join(__dirname, '..', '.env');
6
-
7
- function generateSecret() {
8
- return crypto.randomBytes(64).toString('hex');
9
- }
10
-
11
- try {
12
- const secret = generateSecret();
13
- let envContent = '';
14
-
15
- if (fs.existsSync(envPath)) {
16
- envContent = fs.readFileSync(envPath, 'utf8');
17
- } else {
18
- console.log('.env file not found, creating one...');
19
- }
20
-
21
- // Check if JWT_SECRET exists
22
- if (envContent.match(/^JWT_SECRET=/m)) {
23
- envContent = envContent.replace(/^JWT_SECRET=.*/m, `JWT_SECRET="${secret}"`);
24
- } else {
25
- // Ensure there is a newline before appending if the file is not empty
26
- if (envContent && !envContent.endsWith('\n')) {
27
- envContent += '\n';
28
- }
29
- envContent += `JWT_SECRET="${secret}"\n`;
30
- }
31
-
32
- fs.writeFileSync(envPath, envContent);
33
- console.log('āœ… JWT Secret generated and updated in .env file.');
34
- console.log('šŸ”‘ New Secret has been set.');
35
- } catch (error) {
36
- console.error('āŒ Error updating .env file:', error);
37
- process.exit(1);
38
- }
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const crypto = require('crypto');
4
+
5
+ const envPath = path.join(__dirname, '..', '.env');
6
+
7
+ function generateSecret() {
8
+ return crypto.randomBytes(64).toString('hex');
9
+ }
10
+
11
+ try {
12
+ const secret = generateSecret();
13
+ let envContent = '';
14
+
15
+ if (fs.existsSync(envPath)) {
16
+ envContent = fs.readFileSync(envPath, 'utf8');
17
+ } else {
18
+ console.log('.env file not found, creating one...');
19
+ }
20
+
21
+ // Check if JWT_SECRET exists
22
+ if (envContent.match(/^JWT_SECRET=/m)) {
23
+ envContent = envContent.replace(/^JWT_SECRET=.*/m, `JWT_SECRET="${secret}"`);
24
+ } else {
25
+ // Ensure there is a newline before appending if the file is not empty
26
+ if (envContent && !envContent.endsWith('\n')) {
27
+ envContent += '\n';
28
+ }
29
+ envContent += `JWT_SECRET="${secret}"\n`;
30
+ }
31
+
32
+ fs.writeFileSync(envPath, envContent);
33
+ console.log('āœ… JWT Secret generated and updated in .env file.');
34
+ console.log('šŸ”‘ New Secret has been set.');
35
+ } catch (error) {
36
+ console.error('āŒ Error updating .env file:', error);
37
+ process.exit(1);
38
+ }
@@ -0,0 +1,71 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const { execSync } = require("child_process");
4
+
5
+ const rootDir = path.join(__dirname, "..");
6
+ const envExample = path.join(rootDir, ".env.example");
7
+ const envFile = path.join(rootDir, ".env");
8
+
9
+ console.log("šŸš€ Starting project initialization...");
10
+
11
+ try {
12
+ // 1. Copy .env
13
+ if (!fs.existsSync(envFile)) {
14
+ console.log("šŸ“„ Copying .env.example to .env...");
15
+ fs.copyFileSync(envExample, envFile);
16
+ } else {
17
+ console.log("āš ļø .env already exists, skipping copy.");
18
+ }
19
+
20
+ // 2. Install dependencies
21
+ console.log("šŸ“¦ Installing dependencies...");
22
+ execSync("npm install", { stdio: "inherit", cwd: rootDir });
23
+
24
+ // 3. Generate JWT Secret
25
+ console.log("šŸ”‘ Generating JWT Secret...");
26
+ execSync("node scripts/generate-jwt-secret.js", {
27
+ stdio: "inherit",
28
+ cwd: rootDir,
29
+ });
30
+
31
+ // 4. Setup Database (Migrate)
32
+ console.log("šŸ—„ļø Setting up database...");
33
+ // We skip the interactive prompt by providing a name if needed,
34
+ // or just let it run. However, if it prompts, the script might hang or fail if not interactive.
35
+ // 'prisma migrate dev' asks for name if there are changes.
36
+ // We try to run it. If it fails, we inform the user.
37
+ // We use npx to ensure we use the local binary.
38
+ try {
39
+ // We use the script defined in package.json but add --name init to avoid prompt for new migration
40
+ // However, if migrations already exist, --name might create a new one.
41
+ // If it's a fresh clone, 'prisma migrate dev' checks existing migrations.
42
+ // Let's just run the compile-schema first, then migrate.
43
+ execSync('node scripts/compile-schema.js', { stdio: 'inherit', cwd: rootDir });
44
+
45
+ // Explicitly generate Prisma Client before migration to ensure it exists
46
+ console.log('āš™ļø Generating Prisma Client...');
47
+ execSync('npx prisma generate', { stdio: 'inherit', cwd: rootDir });
48
+
49
+ execSync('npx prisma migrate dev --name init_setup', { stdio: 'inherit', cwd: rootDir });
50
+ } catch (error) {
51
+ console.warn(
52
+ 'āš ļø Database migration had an issue. Please check your database connection in .env and run "npm run prisma:migrate" manually.'
53
+ );
54
+ }
55
+
56
+ // 5. Seed Database
57
+ console.log("🌱 Seeding database...");
58
+ try {
59
+ execSync("npm run db:seed", { stdio: "inherit", cwd: rootDir });
60
+ } catch (error) {
61
+ console.warn(
62
+ 'āš ļø Database seeding had an issue. You might need to run "npm run db:seed" manually.'
63
+ );
64
+ }
65
+
66
+ console.log("\nāœ… Setup complete! You can now run:");
67
+ console.log(" npm run dev");
68
+ } catch (error) {
69
+ console.error("āŒ Setup failed:", error.message);
70
+ process.exit(1);
71
+ }
@@ -1,42 +1,42 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
-
4
- const modelName = process.argv[2];
5
-
6
- if (!modelName) {
7
- console.error('āŒ Please specify the model name.');
8
- console.error(' Usage: npm run make:model <ModelName>');
9
- console.error(' Example: npm run make:model Product');
10
- process.exit(1);
11
- }
12
-
13
- const PascalCaseName = modelName.charAt(0).toUpperCase() + modelName.slice(1);
14
- const tableName = modelName.toLowerCase() + 's'; // simple pluralization
15
-
16
- const modelsDir = path.join(__dirname, '..', 'src', 'models');
17
- const modelPath = path.join(modelsDir, `${PascalCaseName}.prisma`);
18
-
19
- if (fs.existsSync(modelPath)) {
20
- console.error(`āŒ Model ${PascalCaseName} already exists at ${modelPath}`);
21
- process.exit(1);
22
- }
23
-
24
- // Ensure models directory exists
25
- if (!fs.existsSync(modelsDir)) {
26
- fs.mkdirSync(modelsDir, { recursive: true });
27
- }
28
-
29
- const content = `model ${tableName} {
30
- id BigInt @id @default(autoincrement())
31
- name String @db.VarChar(255)
32
- createdAt DateTime? @default(now()) @db.Timestamp(0)
33
- updatedAt DateTime? @updatedAt @db.Timestamp(0)
34
- }
35
- `;
36
-
37
- fs.writeFileSync(modelPath, content);
38
-
39
- console.log(`āœ… Model created: src/models/${PascalCaseName}.prisma`);
40
- console.log(`\nNext steps:`);
41
- console.log(`1. Edit the model file.`);
42
- console.log(`2. Run 'npm run prisma:migrate' to update the database.`);
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ const modelName = process.argv[2];
5
+
6
+ if (!modelName) {
7
+ console.error('āŒ Please specify the model name.');
8
+ console.error(' Usage: npm run make:model <ModelName>');
9
+ console.error(' Example: npm run make:model Product');
10
+ process.exit(1);
11
+ }
12
+
13
+ const PascalCaseName = modelName.charAt(0).toUpperCase() + modelName.slice(1);
14
+ const tableName = modelName.toLowerCase() + 's'; // simple pluralization
15
+
16
+ const modelsDir = path.join(__dirname, '..', 'src', 'models');
17
+ const modelPath = path.join(modelsDir, `${PascalCaseName}.prisma`);
18
+
19
+ if (fs.existsSync(modelPath)) {
20
+ console.error(`āŒ Model ${PascalCaseName} already exists at ${modelPath}`);
21
+ process.exit(1);
22
+ }
23
+
24
+ // Ensure models directory exists
25
+ if (!fs.existsSync(modelsDir)) {
26
+ fs.mkdirSync(modelsDir, { recursive: true });
27
+ }
28
+
29
+ const content = `model ${tableName} {
30
+ id BigInt @id @default(autoincrement())
31
+ name String @db.VarChar(255)
32
+ createdAt DateTime? @default(now()) @db.Timestamp(0)
33
+ updatedAt DateTime? @updatedAt @db.Timestamp(0)
34
+ }
35
+ `;
36
+
37
+ fs.writeFileSync(modelPath, content);
38
+
39
+ console.log(`āœ… Model created: src/models/${PascalCaseName}.prisma`);
40
+ console.log(`\nNext steps:`);
41
+ console.log(`1. Edit the model file.`);
42
+ console.log(`2. Run 'npm run prisma:migrate' to update the database.`);