lapeh 2.2.6 → 2.2.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/api-testing-sepuluh/.env.example +19 -0
- package/api-testing-sepuluh/doc/ARCHITECTURE_GUIDE.md +73 -0
- package/api-testing-sepuluh/doc/CHANGELOG.md +77 -0
- package/api-testing-sepuluh/doc/CHEATSHEET.md +94 -0
- package/api-testing-sepuluh/doc/CLI.md +106 -0
- package/api-testing-sepuluh/doc/CONTRIBUTING.md +105 -0
- package/api-testing-sepuluh/doc/DEPLOYMENT.md +122 -0
- package/api-testing-sepuluh/doc/FAQ.md +81 -0
- package/api-testing-sepuluh/doc/FEATURES.md +165 -0
- package/api-testing-sepuluh/doc/GETTING_STARTED.md +108 -0
- package/api-testing-sepuluh/doc/INTRODUCTION.md +60 -0
- package/api-testing-sepuluh/doc/PACKAGES.md +66 -0
- package/api-testing-sepuluh/doc/PERFORMANCE.md +91 -0
- package/api-testing-sepuluh/doc/ROADMAP.md +93 -0
- package/api-testing-sepuluh/doc/SECURITY.md +93 -0
- package/api-testing-sepuluh/doc/STRUCTURE.md +90 -0
- package/api-testing-sepuluh/doc/TUTORIAL.md +192 -0
- package/api-testing-sepuluh/docker-compose.yml +24 -0
- package/api-testing-sepuluh/eslint.config.mjs +26 -0
- package/api-testing-sepuluh/framework.md +168 -0
- package/api-testing-sepuluh/nodemon.json +6 -0
- package/api-testing-sepuluh/package-lock.json +5539 -0
- package/api-testing-sepuluh/package.json +103 -0
- package/api-testing-sepuluh/prisma/base.prisma.template +7 -0
- package/api-testing-sepuluh/prisma/migrations/20251227034737_init_setup/migration.sql +248 -0
- package/api-testing-sepuluh/prisma/migrations/migration_lock.toml +3 -0
- package/api-testing-sepuluh/prisma/schema.prisma +183 -0
- package/api-testing-sepuluh/prisma/seed.ts +411 -0
- package/api-testing-sepuluh/prisma.config.ts +15 -0
- package/api-testing-sepuluh/readme.md +414 -0
- package/api-testing-sepuluh/scripts/check-update.js +92 -0
- package/api-testing-sepuluh/scripts/compile-schema.js +29 -0
- package/api-testing-sepuluh/scripts/config-clear.js +45 -0
- package/api-testing-sepuluh/scripts/generate-jwt-secret.js +38 -0
- package/api-testing-sepuluh/scripts/init-project.js +178 -0
- package/api-testing-sepuluh/scripts/make-controller.js +205 -0
- package/api-testing-sepuluh/scripts/make-model.js +42 -0
- package/api-testing-sepuluh/scripts/make-module.js +158 -0
- package/api-testing-sepuluh/scripts/verify-rbac-functional.js +187 -0
- package/api-testing-sepuluh/src/controllers/authController.ts +469 -0
- package/api-testing-sepuluh/src/controllers/petController.ts +194 -0
- package/api-testing-sepuluh/src/controllers/rbacController.ts +478 -0
- package/api-testing-sepuluh/src/models/core.prisma +163 -0
- package/api-testing-sepuluh/src/models/pets.prisma +9 -0
- package/api-testing-sepuluh/src/routes/auth.ts +74 -0
- package/api-testing-sepuluh/src/routes/index.ts +10 -0
- package/api-testing-sepuluh/src/routes/pets.ts +13 -0
- package/api-testing-sepuluh/src/routes/rbac.ts +42 -0
- package/api-testing-sepuluh/storage/logs/.gitkeep +0 -0
- package/api-testing-sepuluh/tsconfig.json +39 -0
- package/bin/index.js +68 -13
- package/package.json +1 -1
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import dotenv from "dotenv";
|
|
2
|
+
dotenv.config();
|
|
3
|
+
|
|
4
|
+
import { prisma } from "@lapeh/core/database";
|
|
5
|
+
import bcrypt from "bcryptjs";
|
|
6
|
+
import { v4 as uuidv4 } from "uuid";
|
|
7
|
+
|
|
8
|
+
async function main() {
|
|
9
|
+
console.log("Start seeding...");
|
|
10
|
+
|
|
11
|
+
// 1. Create Users (Super Admin, Admin & User)
|
|
12
|
+
const passwordHash = await bcrypt.hash("string", 10);
|
|
13
|
+
|
|
14
|
+
const superAdmin = await prisma.users.upsert({
|
|
15
|
+
where: { email: "sa@sa.com" },
|
|
16
|
+
update: {},
|
|
17
|
+
create: {
|
|
18
|
+
uuid: uuidv4(),
|
|
19
|
+
name: "Super Admin",
|
|
20
|
+
email: "sa@sa.com",
|
|
21
|
+
password: passwordHash,
|
|
22
|
+
created_at: new Date(),
|
|
23
|
+
updated_at: new Date(),
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
console.log(`Created super admin: ${superAdmin.name}`);
|
|
27
|
+
|
|
28
|
+
const admin = await prisma.users.upsert({
|
|
29
|
+
where: { email: "a@a.com" },
|
|
30
|
+
update: {},
|
|
31
|
+
create: {
|
|
32
|
+
uuid: uuidv4(),
|
|
33
|
+
name: "Admin User",
|
|
34
|
+
email: "a@a.com",
|
|
35
|
+
password: passwordHash,
|
|
36
|
+
created_at: new Date(),
|
|
37
|
+
updated_at: new Date(),
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
console.log(`Created admin: ${admin.name}`);
|
|
41
|
+
|
|
42
|
+
const user = await prisma.users.upsert({
|
|
43
|
+
where: { email: "u@u.com" },
|
|
44
|
+
update: {},
|
|
45
|
+
create: {
|
|
46
|
+
uuid: uuidv4(),
|
|
47
|
+
name: "Regular User",
|
|
48
|
+
email: "u@u.com",
|
|
49
|
+
password: passwordHash,
|
|
50
|
+
created_at: new Date(),
|
|
51
|
+
updated_at: new Date(),
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
console.log(`Created user: ${user.name}`);
|
|
55
|
+
|
|
56
|
+
// 2. Seed Roles
|
|
57
|
+
const superAdminRole = await prisma.roles.upsert({
|
|
58
|
+
where: { slug: "super_admin" },
|
|
59
|
+
update: {},
|
|
60
|
+
create: {
|
|
61
|
+
name: "Super Admin",
|
|
62
|
+
slug: "super_admin",
|
|
63
|
+
description: "Full access to all resources",
|
|
64
|
+
created_at: new Date(),
|
|
65
|
+
updated_at: new Date(),
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const adminRole = await prisma.roles.upsert({
|
|
70
|
+
where: { slug: "admin" },
|
|
71
|
+
update: {},
|
|
72
|
+
create: {
|
|
73
|
+
name: "Admin",
|
|
74
|
+
slug: "admin",
|
|
75
|
+
description: "Administrator",
|
|
76
|
+
created_at: new Date(),
|
|
77
|
+
updated_at: new Date(),
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const userRole = await prisma.roles.upsert({
|
|
82
|
+
where: { slug: "user" },
|
|
83
|
+
update: {},
|
|
84
|
+
create: {
|
|
85
|
+
name: "User",
|
|
86
|
+
slug: "user",
|
|
87
|
+
description: "Regular user",
|
|
88
|
+
created_at: new Date(),
|
|
89
|
+
updated_at: new Date(),
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
console.log("Seeded roles");
|
|
94
|
+
|
|
95
|
+
// 3. Seed Permissions
|
|
96
|
+
const manageUsers = await prisma.permissions.upsert({
|
|
97
|
+
where: { slug: "users.manage" },
|
|
98
|
+
update: {},
|
|
99
|
+
create: {
|
|
100
|
+
name: "Manage Users",
|
|
101
|
+
slug: "users.manage",
|
|
102
|
+
description: "Create, update, and delete users",
|
|
103
|
+
created_at: new Date(),
|
|
104
|
+
updated_at: new Date(),
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
const manageUsersCreate = await prisma.permissions.upsert({
|
|
108
|
+
where: { slug: "users.create" },
|
|
109
|
+
update: {},
|
|
110
|
+
create: {
|
|
111
|
+
name: "Create Users",
|
|
112
|
+
slug: "users.create",
|
|
113
|
+
description: "Create users",
|
|
114
|
+
created_at: new Date(),
|
|
115
|
+
updated_at: new Date(),
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
const manageUsersView = await prisma.permissions.upsert({
|
|
119
|
+
where: { slug: "users.view" },
|
|
120
|
+
update: {},
|
|
121
|
+
create: {
|
|
122
|
+
name: "View Users",
|
|
123
|
+
slug: "users.view",
|
|
124
|
+
description: "View users",
|
|
125
|
+
created_at: new Date(),
|
|
126
|
+
updated_at: new Date(),
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
const manageUsersUpdate = await prisma.permissions.upsert({
|
|
130
|
+
where: { slug: "users.update" },
|
|
131
|
+
update: {},
|
|
132
|
+
create: {
|
|
133
|
+
name: "Update Users",
|
|
134
|
+
slug: "users.update",
|
|
135
|
+
description: "Update users",
|
|
136
|
+
created_at: new Date(),
|
|
137
|
+
updated_at: new Date(),
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
const manageUsersDelete = await prisma.permissions.upsert({
|
|
141
|
+
where: { slug: "users.delete" },
|
|
142
|
+
update: {},
|
|
143
|
+
create: {
|
|
144
|
+
name: "Delete Users",
|
|
145
|
+
slug: "users.delete",
|
|
146
|
+
description: "Delete users",
|
|
147
|
+
created_at: new Date(),
|
|
148
|
+
updated_at: new Date(),
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const manageRoles = await prisma.permissions.upsert({
|
|
153
|
+
where: { slug: "roles.manage" },
|
|
154
|
+
update: {},
|
|
155
|
+
create: {
|
|
156
|
+
name: "Manage Roles",
|
|
157
|
+
slug: "roles.manage",
|
|
158
|
+
description: "Create, update, and delete roles",
|
|
159
|
+
created_at: new Date(),
|
|
160
|
+
updated_at: new Date(),
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const managePermissions = await prisma.permissions.upsert({
|
|
165
|
+
where: { slug: "permissions.manage" },
|
|
166
|
+
update: {},
|
|
167
|
+
create: {
|
|
168
|
+
name: "Manage Permissions",
|
|
169
|
+
slug: "permissions.manage",
|
|
170
|
+
description: "Create, update, and delete permissions",
|
|
171
|
+
created_at: new Date(),
|
|
172
|
+
updated_at: new Date(),
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const manageProfiles = await prisma.permissions.upsert({
|
|
177
|
+
where: { slug: "profiles.manage" },
|
|
178
|
+
update: {},
|
|
179
|
+
create: {
|
|
180
|
+
name: "Manage Profiles",
|
|
181
|
+
slug: "profiles.manage",
|
|
182
|
+
description: "Manage user profiles",
|
|
183
|
+
created_at: new Date(),
|
|
184
|
+
updated_at: new Date(),
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
console.log("Seeded permissions");
|
|
189
|
+
|
|
190
|
+
// 4. Assign permissions to roles
|
|
191
|
+
const postsCreate = await prisma.permissions.upsert({
|
|
192
|
+
where: { slug: "posts.create" },
|
|
193
|
+
update: {},
|
|
194
|
+
create: {
|
|
195
|
+
name: "Create Posts",
|
|
196
|
+
slug: "posts.create",
|
|
197
|
+
description: "Create posts",
|
|
198
|
+
created_at: new Date(),
|
|
199
|
+
updated_at: new Date(),
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const postsUpdate = await prisma.permissions.upsert({
|
|
204
|
+
where: { slug: "posts.update" },
|
|
205
|
+
update: {},
|
|
206
|
+
create: {
|
|
207
|
+
name: "Update Posts",
|
|
208
|
+
slug: "posts.update",
|
|
209
|
+
description: "Update posts",
|
|
210
|
+
created_at: new Date(),
|
|
211
|
+
updated_at: new Date(),
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const postsDelete = await prisma.permissions.upsert({
|
|
216
|
+
where: { slug: "posts.delete" },
|
|
217
|
+
update: {},
|
|
218
|
+
create: {
|
|
219
|
+
name: "Delete Posts",
|
|
220
|
+
slug: "posts.delete",
|
|
221
|
+
description: "Delete posts",
|
|
222
|
+
created_at: new Date(),
|
|
223
|
+
updated_at: new Date(),
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
const postsView = await prisma.permissions.upsert({
|
|
228
|
+
where: { slug: "posts.view" },
|
|
229
|
+
update: {},
|
|
230
|
+
create: {
|
|
231
|
+
name: "View Posts",
|
|
232
|
+
slug: "posts.view",
|
|
233
|
+
description: "View posts",
|
|
234
|
+
created_at: new Date(),
|
|
235
|
+
updated_at: new Date(),
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
const categoriesManage = await prisma.permissions.upsert({
|
|
240
|
+
where: { slug: "categories.manage" },
|
|
241
|
+
update: {},
|
|
242
|
+
create: {
|
|
243
|
+
name: "Manage Categories",
|
|
244
|
+
slug: "categories.manage",
|
|
245
|
+
description: "Manage categories",
|
|
246
|
+
created_at: new Date(),
|
|
247
|
+
updated_at: new Date(),
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
const commentsManage = await prisma.permissions.upsert({
|
|
252
|
+
where: { slug: "comments.manage" },
|
|
253
|
+
update: {},
|
|
254
|
+
create: {
|
|
255
|
+
name: "Manage Comments",
|
|
256
|
+
slug: "comments.manage",
|
|
257
|
+
description: "Manage comments",
|
|
258
|
+
created_at: new Date(),
|
|
259
|
+
updated_at: new Date(),
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
console.log("Seeded resource permissions");
|
|
264
|
+
|
|
265
|
+
// 4. Assign permissions to roles
|
|
266
|
+
const rolePermPairs: { roleId: bigint; permId: bigint }[] = [
|
|
267
|
+
// super_admin gets all users permissions + other management permissions
|
|
268
|
+
{ roleId: superAdminRole.id, permId: manageUsers.id },
|
|
269
|
+
{ roleId: superAdminRole.id, permId: manageUsersCreate.id },
|
|
270
|
+
{ roleId: superAdminRole.id, permId: manageUsersView.id },
|
|
271
|
+
{ roleId: superAdminRole.id, permId: manageUsersUpdate.id },
|
|
272
|
+
{ roleId: superAdminRole.id, permId: manageUsersDelete.id },
|
|
273
|
+
{ roleId: superAdminRole.id, permId: manageRoles.id },
|
|
274
|
+
{ roleId: superAdminRole.id, permId: managePermissions.id },
|
|
275
|
+
{ roleId: superAdminRole.id, permId: manageProfiles.id },
|
|
276
|
+
{ roleId: superAdminRole.id, permId: postsCreate.id },
|
|
277
|
+
{ roleId: superAdminRole.id, permId: postsUpdate.id },
|
|
278
|
+
{ roleId: superAdminRole.id, permId: postsDelete.id },
|
|
279
|
+
{ roleId: superAdminRole.id, permId: postsView.id },
|
|
280
|
+
{ roleId: superAdminRole.id, permId: categoriesManage.id },
|
|
281
|
+
{ roleId: superAdminRole.id, permId: commentsManage.id },
|
|
282
|
+
// admin gets users permissions except delete
|
|
283
|
+
{ roleId: adminRole.id, permId: manageUsersCreate.id },
|
|
284
|
+
{ roleId: adminRole.id, permId: manageUsersView.id },
|
|
285
|
+
{ roleId: adminRole.id, permId: manageUsersUpdate.id },
|
|
286
|
+
// admin gets other management permissions
|
|
287
|
+
{ roleId: adminRole.id, permId: manageRoles.id },
|
|
288
|
+
{ roleId: adminRole.id, permId: managePermissions.id },
|
|
289
|
+
{ roleId: adminRole.id, permId: postsCreate.id },
|
|
290
|
+
{ roleId: adminRole.id, permId: postsUpdate.id },
|
|
291
|
+
{ roleId: adminRole.id, permId: postsDelete.id },
|
|
292
|
+
{ roleId: adminRole.id, permId: postsView.id },
|
|
293
|
+
{ roleId: adminRole.id, permId: categoriesManage.id },
|
|
294
|
+
{ roleId: adminRole.id, permId: commentsManage.id },
|
|
295
|
+
// user gets read-only posts
|
|
296
|
+
{ roleId: userRole.id, permId: postsView.id },
|
|
297
|
+
];
|
|
298
|
+
|
|
299
|
+
for (const pair of rolePermPairs) {
|
|
300
|
+
await prisma.role_permissions.upsert({
|
|
301
|
+
where: {
|
|
302
|
+
role_id_permission_id: {
|
|
303
|
+
role_id: pair.roleId,
|
|
304
|
+
permission_id: pair.permId,
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
create: {
|
|
308
|
+
role_id: pair.roleId,
|
|
309
|
+
permission_id: pair.permId,
|
|
310
|
+
created_at: new Date(),
|
|
311
|
+
},
|
|
312
|
+
update: {},
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
console.log("Assigned permissions to roles");
|
|
317
|
+
|
|
318
|
+
// 5. Assign roles to users
|
|
319
|
+
const userRolePairs: { userId: bigint; roleId: bigint }[] = [
|
|
320
|
+
{ userId: superAdmin.id, roleId: superAdminRole.id },
|
|
321
|
+
{ userId: admin.id, roleId: adminRole.id },
|
|
322
|
+
{ userId: user.id, roleId: userRole.id },
|
|
323
|
+
];
|
|
324
|
+
|
|
325
|
+
for (const pair of userRolePairs) {
|
|
326
|
+
await prisma.user_roles.upsert({
|
|
327
|
+
where: {
|
|
328
|
+
user_id_role_id: {
|
|
329
|
+
user_id: pair.userId,
|
|
330
|
+
role_id: pair.roleId,
|
|
331
|
+
},
|
|
332
|
+
},
|
|
333
|
+
create: {
|
|
334
|
+
user_id: pair.userId,
|
|
335
|
+
role_id: pair.roleId,
|
|
336
|
+
created_at: new Date(),
|
|
337
|
+
},
|
|
338
|
+
update: {},
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
console.log("Assigned roles to users");
|
|
343
|
+
|
|
344
|
+
// 6. Seed Pets (Massive Seeding)
|
|
345
|
+
console.log("Starting massive pet seeding (50,000 records)...");
|
|
346
|
+
const petData = [];
|
|
347
|
+
const speciesList = [
|
|
348
|
+
"Dog",
|
|
349
|
+
"Cat",
|
|
350
|
+
"Bird",
|
|
351
|
+
"Fish",
|
|
352
|
+
"Rabbit",
|
|
353
|
+
"Hamster",
|
|
354
|
+
"Turtle",
|
|
355
|
+
"Parrot",
|
|
356
|
+
];
|
|
357
|
+
const names = [
|
|
358
|
+
"Bella",
|
|
359
|
+
"Max",
|
|
360
|
+
"Charlie",
|
|
361
|
+
"Luna",
|
|
362
|
+
"Lucy",
|
|
363
|
+
"Cooper",
|
|
364
|
+
"Bailey",
|
|
365
|
+
"Daisy",
|
|
366
|
+
"Sadie",
|
|
367
|
+
"Molly",
|
|
368
|
+
];
|
|
369
|
+
|
|
370
|
+
for (let i = 0; i < 50000; i++) {
|
|
371
|
+
const species = speciesList[Math.floor(Math.random() * speciesList.length)];
|
|
372
|
+
const name = names[Math.floor(Math.random() * names.length)] + " " + i;
|
|
373
|
+
const age = Math.floor(Math.random() * 15) + 1;
|
|
374
|
+
|
|
375
|
+
petData.push({
|
|
376
|
+
name,
|
|
377
|
+
species,
|
|
378
|
+
age,
|
|
379
|
+
created_at: new Date(),
|
|
380
|
+
updated_at: new Date(),
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
// Batch insert every 5000 records to prevent memory issues
|
|
384
|
+
if (petData.length === 5000) {
|
|
385
|
+
await prisma.pets.createMany({
|
|
386
|
+
data: petData,
|
|
387
|
+
});
|
|
388
|
+
petData.length = 0; // Clear array
|
|
389
|
+
console.log(`Seeded ${i + 1} pets...`);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// Insert remaining pets
|
|
394
|
+
if (petData.length > 0) {
|
|
395
|
+
await prisma.pets.createMany({
|
|
396
|
+
data: petData,
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
console.log("Finished seeding 50,000 pets.");
|
|
400
|
+
|
|
401
|
+
console.log("Seeding finished.");
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
main()
|
|
405
|
+
.catch((e) => {
|
|
406
|
+
console.error(e);
|
|
407
|
+
process.exit(1);
|
|
408
|
+
})
|
|
409
|
+
.finally(async () => {
|
|
410
|
+
await prisma.$disconnect();
|
|
411
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// This file was generated by Prisma, and assumes you have installed the following:
|
|
2
|
+
// npm install --save-dev prisma dotenv
|
|
3
|
+
import "dotenv/config";
|
|
4
|
+
import { defineConfig, env } from "prisma/config";
|
|
5
|
+
|
|
6
|
+
export default defineConfig({
|
|
7
|
+
schema: "prisma/schema.prisma",
|
|
8
|
+
migrations: {
|
|
9
|
+
path: "prisma/migrations",
|
|
10
|
+
seed: "ts-node -r tsconfig-paths/register prisma/seed.ts",
|
|
11
|
+
},
|
|
12
|
+
datasource: {
|
|
13
|
+
url: env("DATABASE_URL"),
|
|
14
|
+
},
|
|
15
|
+
});
|