@gapcm/cli 1.0.4
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/LICENSE +21 -0
- package/README.md +72 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +56 -0
- package/dist/index.mjs +33 -0
- package/package.json +46 -0
- package/templates/controller.template.ejs +66 -0
- package/templates/entity.template.ejs +52 -0
- package/templates/repository.template.ejs +58 -0
- package/templates/route.template.ejs +47 -0
- package/templates/service.template.ejs +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gapcm
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# @gapcm/cli
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@gapcm/cli)
|
|
4
|
+
[](https://github.com/nam-rgba/gapcm.git)
|
|
5
|
+
|
|
6
|
+
`@gapcm/cli` is the public CLI package for the GAPCM ecosystem. It is the starting point for a larger full-stack framework that aims to support both backend and frontend development from one shared toolchain.
|
|
7
|
+
|
|
8
|
+
It starts with CLI-based scaffolding for API modules, and is designed to grow into a broader ecosystem that can later include UI generation, shared patterns, and framework-level tooling for both BE and FE.
|
|
9
|
+
|
|
10
|
+
Repository: [nam-rgba/gapcm](https://github.com/nam-rgba/gapcm.git)
|
|
11
|
+
|
|
12
|
+
## What it does today
|
|
13
|
+
|
|
14
|
+
- Generates backend module files from EJS templates.
|
|
15
|
+
- Keeps entity, repository, service, controller, and route names aligned with the module name.
|
|
16
|
+
- Exposes a single CLI entry point: `gapcm`.
|
|
17
|
+
|
|
18
|
+
## Install
|
|
19
|
+
|
|
20
|
+
```powershell
|
|
21
|
+
npm install -g @gapcm/cli
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
Generate a module:
|
|
27
|
+
|
|
28
|
+
```powershell
|
|
29
|
+
gapcm add sample
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Short alias:
|
|
33
|
+
|
|
34
|
+
```powershell
|
|
35
|
+
gapcm g sample
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Run the command from your target project folder so files are created in the correct app structure.
|
|
39
|
+
|
|
40
|
+
## Generated files
|
|
41
|
+
|
|
42
|
+
For a module named `sample`, the CLI creates:
|
|
43
|
+
|
|
44
|
+
- `src/entities/sample.entity.ts`
|
|
45
|
+
- `src/repository/sample.repository.ts`
|
|
46
|
+
- `src/services/sample.service.ts`
|
|
47
|
+
- `src/controllers/sample.controller.ts`
|
|
48
|
+
- `src/routes/sample.admin.ts`
|
|
49
|
+
|
|
50
|
+
## Example
|
|
51
|
+
|
|
52
|
+
```powershell
|
|
53
|
+
cd app/api
|
|
54
|
+
gapcm add user-profile
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
This generates `UserProfile`-based class names and `userProfile`-based file names.
|
|
58
|
+
|
|
59
|
+
## Roadmap
|
|
60
|
+
|
|
61
|
+
`gapcm` is intended to evolve beyond API scaffolding into a broader framework that can support:
|
|
62
|
+
|
|
63
|
+
- shared backend conventions
|
|
64
|
+
- frontend scaffolding and UI generation
|
|
65
|
+
- reusable patterns across the full stack
|
|
66
|
+
- tooling that keeps BE and FE development aligned
|
|
67
|
+
|
|
68
|
+
## Notes
|
|
69
|
+
|
|
70
|
+
- The package publishes `dist` and `templates`.
|
|
71
|
+
- The CLI entry point is `gapcm`.
|
|
72
|
+
- The package is published publicly on npm.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
25
|
+
|
|
26
|
+
// src/index.ts
|
|
27
|
+
var import_commander = require("commander");
|
|
28
|
+
var import_fs_extra = __toESM(require("fs-extra"));
|
|
29
|
+
var import_path = __toESM(require("path"));
|
|
30
|
+
var import_ejs = __toESM(require("ejs"));
|
|
31
|
+
var import_change_case = require("change-case");
|
|
32
|
+
var program = new import_commander.Command();
|
|
33
|
+
program.name("gapcm").description("GAPCM resource generator");
|
|
34
|
+
program.command("add <name>").alias("g").description("Generate resource").action(async (name) => {
|
|
35
|
+
const moduleName = name;
|
|
36
|
+
const pascalName = (0, import_change_case.pascalCase)(name);
|
|
37
|
+
const camelName = (0, import_change_case.camelCase)(name);
|
|
38
|
+
const templates = [
|
|
39
|
+
{ name: "entity", path: "entity.template.ejs", outputDir: "src/entities", outputExt: ".entity.ts" },
|
|
40
|
+
{ name: "repository", path: "repository.template.ejs", outputDir: "src/repository", outputExt: ".repository.ts" },
|
|
41
|
+
{ name: "service", path: "service.template.ejs", outputDir: "src/services", outputExt: ".service.ts" },
|
|
42
|
+
{ name: "controller", path: "controller.template.ejs", outputDir: "src/controllers", outputExt: ".controller.ts" },
|
|
43
|
+
{ name: "route", path: "route.template.ejs", outputDir: "src/routes", outputExt: ".admin.ts" }
|
|
44
|
+
];
|
|
45
|
+
for (const temp of templates) {
|
|
46
|
+
const templateContent = await import_fs_extra.default.readFile(
|
|
47
|
+
import_path.default.join(__dirname, `../templates/${temp.path}`),
|
|
48
|
+
"utf-8"
|
|
49
|
+
);
|
|
50
|
+
const rendered = import_ejs.default.render(templateContent, { moduleName, pascalName, camelName });
|
|
51
|
+
const outputPath = import_path.default.join(process.cwd(), `${temp.outputDir}/${camelName}${temp.outputExt}`);
|
|
52
|
+
await import_fs_extra.default.outputFile(outputPath, rendered);
|
|
53
|
+
console.log(`Generated: ${outputPath}`);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
program.parse(process.argv);
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { Command } from "commander";
|
|
5
|
+
import fs from "fs-extra";
|
|
6
|
+
import path from "path";
|
|
7
|
+
import ejs from "ejs";
|
|
8
|
+
import { pascalCase, camelCase } from "change-case";
|
|
9
|
+
var program = new Command();
|
|
10
|
+
program.name("gapcm").description("GAPCM resource generator");
|
|
11
|
+
program.command("add <name>").alias("g").description("Generate resource").action(async (name) => {
|
|
12
|
+
const moduleName = name;
|
|
13
|
+
const pascalName = pascalCase(name);
|
|
14
|
+
const camelName = camelCase(name);
|
|
15
|
+
const templates = [
|
|
16
|
+
{ name: "entity", path: "entity.template.ejs", outputDir: "src/entities", outputExt: ".entity.ts" },
|
|
17
|
+
{ name: "repository", path: "repository.template.ejs", outputDir: "src/repository", outputExt: ".repository.ts" },
|
|
18
|
+
{ name: "service", path: "service.template.ejs", outputDir: "src/services", outputExt: ".service.ts" },
|
|
19
|
+
{ name: "controller", path: "controller.template.ejs", outputDir: "src/controllers", outputExt: ".controller.ts" },
|
|
20
|
+
{ name: "route", path: "route.template.ejs", outputDir: "src/routes", outputExt: ".admin.ts" }
|
|
21
|
+
];
|
|
22
|
+
for (const temp of templates) {
|
|
23
|
+
const templateContent = await fs.readFile(
|
|
24
|
+
path.join(__dirname, `../templates/${temp.path}`),
|
|
25
|
+
"utf-8"
|
|
26
|
+
);
|
|
27
|
+
const rendered = ejs.render(templateContent, { moduleName, pascalName, camelName });
|
|
28
|
+
const outputPath = path.join(process.cwd(), `${temp.outputDir}/${camelName}${temp.outputExt}`);
|
|
29
|
+
await fs.outputFile(outputPath, rendered);
|
|
30
|
+
console.log(`Generated: ${outputPath}`);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
program.parse(process.argv);
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gapcm/cli",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "CLI scaffolder for building a larger full-stack framework across backend and frontend apps.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"templates"
|
|
11
|
+
],
|
|
12
|
+
"bin": {
|
|
13
|
+
"gapcm": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
17
|
+
"dev": "tsup src/index.ts --format cjs,esm --watch --dts"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/ejs": "^3.1.5",
|
|
21
|
+
"@types/fs-extra": "^11.0.4",
|
|
22
|
+
"tsup": "^8.0.0",
|
|
23
|
+
"typescript": "^5.0.0"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"cli",
|
|
27
|
+
"generator",
|
|
28
|
+
"scaffold",
|
|
29
|
+
"framework",
|
|
30
|
+
"fullstack",
|
|
31
|
+
"backend",
|
|
32
|
+
"frontend",
|
|
33
|
+
"typescript",
|
|
34
|
+
"ejs"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"chalk": "^5.6.2",
|
|
38
|
+
"change-case": "^5.4.4",
|
|
39
|
+
"commander": "^15.0.0",
|
|
40
|
+
"ejs": "^6.0.1",
|
|
41
|
+
"fs-extra": "^11.3.5",
|
|
42
|
+
"ora": "^9.4.1",
|
|
43
|
+
"prompts": "^2.4.2",
|
|
44
|
+
"ts-morph": "^28.0.0"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
<%
|
|
2
|
+
const inputModuleName = typeof moduleName === "string" && moduleName.trim().length > 0
|
|
3
|
+
? moduleName.trim()
|
|
4
|
+
: "sample"
|
|
5
|
+
const words = inputModuleName
|
|
6
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
7
|
+
.split(/[-_\s]+/)
|
|
8
|
+
.filter(Boolean)
|
|
9
|
+
const pascalName = words.map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join("")
|
|
10
|
+
const camelName = pascalName.charAt(0).toLowerCase() + pascalName.slice(1)
|
|
11
|
+
const pluralName = `${camelName}s`
|
|
12
|
+
const kebabName = words.map((w) => w.toLowerCase()).join("-")
|
|
13
|
+
%>
|
|
14
|
+
|
|
15
|
+
import { NextFunction, Request, Response } from "express";
|
|
16
|
+
import <%= camelName %>Service from "~/services/<%= kebabName %>.service.js";
|
|
17
|
+
import { CreatedResponse, OKResponse } from "~/utils/success.res.js";
|
|
18
|
+
|
|
19
|
+
class <%= pascalName %>Controller {
|
|
20
|
+
|
|
21
|
+
getAll = async(req: Request, res: Response, next: NextFunction) => {
|
|
22
|
+
return new OKResponse(
|
|
23
|
+
"Get <%= pluralName %> successfully!",
|
|
24
|
+
200,
|
|
25
|
+
await <%= camelName %>Service.getAll(req.query)
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
getById = async(req: Request, res: Response, next: NextFunction) => {
|
|
30
|
+
const id = Number(req.params.id)
|
|
31
|
+
return new OKResponse(
|
|
32
|
+
"Get <%= camelName %> successfully!",
|
|
33
|
+
200,
|
|
34
|
+
await <%= camelName %>Service.getById(id)
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
create = async(req: Request, res: Response, next: NextFunction) => {
|
|
39
|
+
return new CreatedResponse(
|
|
40
|
+
"Create <%= camelName %> successfully!",
|
|
41
|
+
201,
|
|
42
|
+
await <%= camelName %>Service.create(req.body)
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
update = async(req: Request, res: Response, next: NextFunction) => {
|
|
47
|
+
const id = Number(req.params.id)
|
|
48
|
+
return new OKResponse(
|
|
49
|
+
"Update <%= camelName %> successfully!",
|
|
50
|
+
200,
|
|
51
|
+
await <%= camelName %>Service.update(id, req.body)
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
delete = async(req: Request, res: Response, next: NextFunction) => {
|
|
56
|
+
const id = Number(req.params.id)
|
|
57
|
+
return new OKResponse(
|
|
58
|
+
"Delete <%= camelName %> successfully!",
|
|
59
|
+
200,
|
|
60
|
+
await <%= camelName %>Service.delete(id)
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const <%= camelName %>Controller = new <%= pascalName %>Controller();
|
|
66
|
+
export default <%= camelName %>Controller;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
<%
|
|
2
|
+
const inputModuleName = typeof moduleName === 'string' && moduleName.trim().length > 0
|
|
3
|
+
? moduleName.trim()
|
|
4
|
+
: 'Sample'
|
|
5
|
+
const normalized = inputModuleName.replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : ''))
|
|
6
|
+
const entityName = normalized.charAt(0).toUpperCase() + normalized.slice(1)
|
|
7
|
+
const entityNameLower = entityName.charAt(0).toLowerCase() + entityName.slice(1)
|
|
8
|
+
%>
|
|
9
|
+
|
|
10
|
+
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'
|
|
11
|
+
import z from 'zod'
|
|
12
|
+
|
|
13
|
+
@Entity()
|
|
14
|
+
export class <%= entityName %> {
|
|
15
|
+
@PrimaryGeneratedColumn()
|
|
16
|
+
id!: number
|
|
17
|
+
|
|
18
|
+
@Column({
|
|
19
|
+
nullable: false
|
|
20
|
+
})
|
|
21
|
+
name!: string
|
|
22
|
+
|
|
23
|
+
@Column()
|
|
24
|
+
image?: string
|
|
25
|
+
|
|
26
|
+
@Column({
|
|
27
|
+
unique: true,
|
|
28
|
+
nullable: false
|
|
29
|
+
})
|
|
30
|
+
description!: string
|
|
31
|
+
|
|
32
|
+
@Column({
|
|
33
|
+
type: 'boolean',
|
|
34
|
+
default: true
|
|
35
|
+
})
|
|
36
|
+
isVerify: boolean = true
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const <%= entityName %>Query = z.object({
|
|
40
|
+
page: z.number().int().min(1).default(1),
|
|
41
|
+
limit: z.number().int().min(1).max(100).default(10),
|
|
42
|
+
search: z.string().optional(),
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
export const <%= entityName %>CreateSchema = z.object({
|
|
46
|
+
name: z.string().min(1, { message: 'Name is required' }),
|
|
47
|
+
description: z.string().min(1, { message: 'Description is required' }),
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
export const <%= entityName %>ParamsSchema = z.object({
|
|
51
|
+
id: z.number().int().min(1, { message: 'Invalid <%= entityNameLower %> ID' }),
|
|
52
|
+
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
<%
|
|
2
|
+
const inputModuleName = typeof moduleName === "string" && moduleName.trim().length > 0
|
|
3
|
+
? moduleName.trim()
|
|
4
|
+
: "sample"
|
|
5
|
+
const words = inputModuleName
|
|
6
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
7
|
+
.split(/[-_\s]+/)
|
|
8
|
+
.filter(Boolean)
|
|
9
|
+
const pascalName = words.map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join("")
|
|
10
|
+
const camelName = pascalName.charAt(0).toLowerCase() + pascalName.slice(1)
|
|
11
|
+
const kebabName = words.map((w) => w.toLowerCase()).join("-")
|
|
12
|
+
const pluralName = `${camelName}s`
|
|
13
|
+
%>
|
|
14
|
+
|
|
15
|
+
import { DataSource, Repository } from "typeorm";
|
|
16
|
+
import z from "zod";
|
|
17
|
+
import { <%= pascalName %>, <%= pascalName %>Query } from "~/entities/<%= kebabName %>.entity.js";
|
|
18
|
+
|
|
19
|
+
export class <%= pascalName %>Repository {
|
|
20
|
+
constructor(appDataSource: DataSource) {
|
|
21
|
+
this.repo = appDataSource.getRepository(<%= pascalName %>)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private repo: Repository<<%= pascalName %>>;
|
|
25
|
+
|
|
26
|
+
// find all <%= pluralName %> ==============================================
|
|
27
|
+
public findAll = async(query: z.infer<typeof <%= pascalName %>Query>) => {
|
|
28
|
+
const { page, limit, search } = query;
|
|
29
|
+
const [<%= pluralName %>, total] = await this.repo.findAndCount({
|
|
30
|
+
where: search ? { name: z.string().regex(new RegExp(search, "i")).parse(search) } as any : {},
|
|
31
|
+
skip: (page - 1) * limit,
|
|
32
|
+
take: limit,
|
|
33
|
+
});
|
|
34
|
+
return { <%= pluralName %>, total };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// find <%= camelName %> by id ==============================================
|
|
38
|
+
public findById = async(id: number) => {
|
|
39
|
+
return await this.repo.findOneBy({ id });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// create new <%= camelName %> ==============================================
|
|
43
|
+
public create = async(entity: Partial<<%= pascalName %>>) => {
|
|
44
|
+
const newEntity = this.repo.create(entity);
|
|
45
|
+
return await this.repo.save(newEntity);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// update <%= camelName %> by id ==============================================
|
|
49
|
+
public update = async(id: number, entity: Partial<<%= pascalName %>>) => {
|
|
50
|
+
await this.repo.update(id, entity);
|
|
51
|
+
return await this.repo.findOneBy({ id });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// delete <%= camelName %> by id ==============================================
|
|
55
|
+
public delete = async(id: number) => {
|
|
56
|
+
return await this.repo.delete(id);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
<%
|
|
2
|
+
const inputModuleName = typeof moduleName === "string" && moduleName.trim().length > 0
|
|
3
|
+
? moduleName.trim()
|
|
4
|
+
: "sample"
|
|
5
|
+
const words = inputModuleName
|
|
6
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
7
|
+
.split(/[-_\s]+/)
|
|
8
|
+
.filter(Boolean)
|
|
9
|
+
const pascalName = words.map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join("")
|
|
10
|
+
const kebabName = words.map((w) => w.toLowerCase()).join("-")
|
|
11
|
+
const camelName = pascalName.charAt(0).toLowerCase() + pascalName.slice(1)
|
|
12
|
+
%>
|
|
13
|
+
|
|
14
|
+
import { Router } from "express";
|
|
15
|
+
import <%= camelName %>Controller from "~/controllers/<%= kebabName %>.controller.js";
|
|
16
|
+
import {
|
|
17
|
+
<%= pascalName %>CreateSchema,
|
|
18
|
+
<%= pascalName %>ParamsSchema,
|
|
19
|
+
<%= pascalName %>Query,
|
|
20
|
+
} from "~/entities/<%= kebabName %>.entity.js";
|
|
21
|
+
import { validate } from "~/middlewares/validate.js";
|
|
22
|
+
import AsyncHandler from "~/utils/async-handler.js";
|
|
23
|
+
|
|
24
|
+
const router = Router();
|
|
25
|
+
|
|
26
|
+
router.get("/", validate({
|
|
27
|
+
query: <%= pascalName %>Query
|
|
28
|
+
}), AsyncHandler(<%= camelName %>Controller.getAll));
|
|
29
|
+
|
|
30
|
+
router.get("/:id", validate({
|
|
31
|
+
params: <%= pascalName %>ParamsSchema
|
|
32
|
+
}), AsyncHandler(<%= camelName %>Controller.getById));
|
|
33
|
+
|
|
34
|
+
router.post("/", validate({
|
|
35
|
+
body: <%= pascalName %>CreateSchema
|
|
36
|
+
}), AsyncHandler(<%= camelName %>Controller.create));
|
|
37
|
+
|
|
38
|
+
router.put("/:id", validate({
|
|
39
|
+
params: <%= pascalName %>ParamsSchema,
|
|
40
|
+
body: <%= pascalName %>CreateSchema
|
|
41
|
+
}), AsyncHandler(<%= camelName %>Controller.update));
|
|
42
|
+
|
|
43
|
+
router.delete("/:id", validate({
|
|
44
|
+
params: <%= pascalName %>ParamsSchema
|
|
45
|
+
}), AsyncHandler(<%= camelName %>Controller.delete));
|
|
46
|
+
|
|
47
|
+
export default router;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
<%
|
|
2
|
+
const inputModuleName = typeof moduleName === "string" && moduleName.trim().length > 0
|
|
3
|
+
? moduleName.trim()
|
|
4
|
+
: "sample"
|
|
5
|
+
const words = inputModuleName
|
|
6
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
7
|
+
.split(/[-_\s]+/)
|
|
8
|
+
.filter(Boolean)
|
|
9
|
+
const pascalName = words.map((w) => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()).join("")
|
|
10
|
+
const camelName = pascalName.charAt(0).toLowerCase() + pascalName.slice(1)
|
|
11
|
+
const kebabName = words.map((w) => w.toLowerCase()).join("-")
|
|
12
|
+
%>
|
|
13
|
+
|
|
14
|
+
import { pick } from "lodash";
|
|
15
|
+
import { AppDataSource } from "~/data-source.js";
|
|
16
|
+
import { <%= pascalName %>Repository } from "~/repository/<%= kebabName %>.repository.js";
|
|
17
|
+
|
|
18
|
+
export class <%= pascalName %>Service {
|
|
19
|
+
|
|
20
|
+
constructor(repo: <%= pascalName %>Repository) {
|
|
21
|
+
this.repo = repo;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
private repo: <%= pascalName %>Repository;
|
|
25
|
+
|
|
26
|
+
public getAll = async(query: any) => {
|
|
27
|
+
return await this.repo.findAll(query)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public getById = async(id: number) => {
|
|
31
|
+
const entity = await this.repo.findById(id)
|
|
32
|
+
if (!entity) throw new Error(`<%= pascalName %> with id ${id} not found`)
|
|
33
|
+
return entity ? pick(entity, ["id", "name", "description", "isVerify"]) : null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public create = async(payload: any) => {
|
|
37
|
+
return await this.repo.create(payload)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public update = async(id: number, payload: any) => {
|
|
41
|
+
return await this.repo.update(id, payload)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public delete = async(id: number) => {
|
|
45
|
+
return await this.repo.delete(id)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const <%= camelName %>Repo = new <%= pascalName %>Repository(AppDataSource);
|
|
50
|
+
const <%= camelName %>Service = new <%= pascalName %>Service(<%= camelName %>Repo);
|
|
51
|
+
|
|
52
|
+
export default <%= camelName %>Service;
|