@meghjs/cli 0.0.0-stage → 0.5.1
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/dist/bin.d.ts +2 -0
- package/dist/bin.js +10 -0
- package/dist/commands/build.d.ts +11 -0
- package/dist/commands/build.js +65 -0
- package/dist/commands/doctor.d.ts +10 -0
- package/dist/commands/doctor.js +96 -0
- package/dist/commands/migrate.d.ts +41 -0
- package/dist/commands/migrate.js +193 -0
- package/dist/commands/new.d.ts +24 -0
- package/dist/commands/new.js +377 -0
- package/dist/commands/route-list.d.ts +19 -0
- package/dist/commands/route-list.js +160 -0
- package/dist/commands/serve.d.ts +17 -0
- package/dist/commands/serve.js +238 -0
- package/dist/commands/start.d.ts +10 -0
- package/dist/commands/start.js +43 -0
- package/dist/compiler.d.ts +24 -0
- package/dist/compiler.js +83 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +16 -0
- package/dist/generators/make.d.ts +60 -0
- package/dist/generators/make.js +325 -0
- package/dist/generators/migration-schema.d.ts +29 -0
- package/dist/generators/migration-schema.js +398 -0
- package/dist/generators/orm-model.d.ts +23 -0
- package/dist/generators/orm-model.js +158 -0
- package/dist/generators/resource.d.ts +32 -0
- package/dist/generators/resource.js +277 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +42 -0
- package/dist/program.d.ts +22 -0
- package/dist/program.js +224 -0
- package/dist/utils/entry.d.ts +15 -0
- package/dist/utils/entry.js +55 -0
- package/dist/utils/env-types.d.ts +8 -0
- package/dist/utils/env-types.js +19 -0
- package/dist/utils/log.d.ts +25 -0
- package/dist/utils/log.js +39 -0
- package/dist/utils/names.d.ts +27 -0
- package/dist/utils/names.js +58 -0
- package/dist/utils/process.d.ts +18 -0
- package/dist/utils/process.js +84 -0
- package/dist/utils/project.d.ts +17 -0
- package/dist/utils/project.js +91 -0
- package/dist/utils/stubs.d.ts +15 -0
- package/dist/utils/stubs.js +52 -0
- package/dist/utils/tsconfig.d.ts +13 -0
- package/dist/utils/tsconfig.js +102 -0
- package/package.json +61 -4
- package/readme.md +155 -0
- package/stubs/config/config.stub +9 -0
- package/stubs/controllers/basic-controller.stub +9 -0
- package/stubs/controllers/model-controller.stub +34 -0
- package/stubs/controllers/resource-controller.stub +45 -0
- package/stubs/controllers/rest-controller.stub +29 -0
- package/stubs/dtos/dto.stub +11 -0
- package/stubs/dtos/resource-dto.stub +28 -0
- package/stubs/middlewares/middleware.stub +9 -0
- package/stubs/migrations/alter-table.stub +15 -0
- package/stubs/migrations/create-table.stub +15 -0
- package/stubs/migrations/migration.stub +11 -0
- package/stubs/models/entity-model.stub +16 -0
- package/stubs/models/orm-model-blank.stub +10 -0
- package/stubs/models/orm-model.stub +7 -0
- package/stubs/project/app-minimal.stub +7 -0
- package/stubs/project/app.stub +8 -0
- package/stubs/project/env.stub +3 -0
- package/stubs/project/gitignore.stub +5 -0
- package/stubs/project/home.controller.spec.stub +19 -0
- package/stubs/project/home.controller.stub +12 -0
- package/stubs/project/home.service.stub +8 -0
- package/stubs/project/openapi.config.stub +15 -0
- package/stubs/project/readme.stub +26 -0
- package/stubs/project/serve.stub +10 -0
- package/stubs/project/tsconfig.build.stub +4 -0
- package/stubs/project/tsconfig.stub +18 -0
- package/stubs/project/vitest-e2e.config.stub +10 -0
- package/stubs/project/vitest.config.stub +17 -0
- package/stubs/services/basic-service.stub +8 -0
- package/stubs/services/model-service.stub +42 -0
- package/stubs/services/orm-rest-service.stub +61 -0
- package/README.md +0 -3
package/readme.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
### ⚠️ WARNING: NOT FOR PRODUCTION USE
|
|
2
|
+
|
|
3
|
+
> **🚧 This project is in active development.**
|
|
4
|
+
>
|
|
5
|
+
> It is **not stable** and **not ready** for live environments.
|
|
6
|
+
> Use **only for testing, experimentation, or internal evaluation**.
|
|
7
|
+
>
|
|
8
|
+
> #### ❗ Risks of using this in production:
|
|
9
|
+
>
|
|
10
|
+
> - 🔄 Breaking changes may be introduced at any time
|
|
11
|
+
> - 🧪 Features are experimental and may be unstable
|
|
12
|
+
> - 🔐 Security has not been audited
|
|
13
|
+
> - 💥 Potential for data loss or critical errors
|
|
14
|
+
>
|
|
15
|
+
> **Please do not deploy this in production environments.**
|
|
16
|
+
|
|
17
|
+
# Megh CLI
|
|
18
|
+
|
|
19
|
+
Scaffold and run [Megh](https://github.com/meghjs/megh) applications.
|
|
20
|
+
|
|
21
|
+
## Create an Application
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npx @meghjs/cli new my_api
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
You are prompted for a package manager and for optional features (OpenAPI,
|
|
28
|
+
CORS, TypeORM, Knex, Scalar UI). The generated project uses `@meghjs/core`'s
|
|
29
|
+
`Megh.createApplication()` and is ready to run:
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
cd my_api
|
|
33
|
+
npm run start:dev
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Commands
|
|
37
|
+
|
|
38
|
+
| Command | Alias | Options | Description |
|
|
39
|
+
|---------|-------|---------|-------------|
|
|
40
|
+
| `new <name>` | | `-m`, `-f`, `-y`, `--pm <npm\|yarn\|pnpm>`, `--features <list>`, `--skip-install` | Create a new application |
|
|
41
|
+
| `serve` | | `--entry <file>`, `--no-type-check`, `--debounce <ms>` | Dev server: SWC compile, run, reload on change |
|
|
42
|
+
| `build` | | `-p <tsconfig>` | Compile with `tsconfig.build.json` and copy assets |
|
|
43
|
+
| `start` | | `--entry <file>` | Run the compiled app with `NODE_ENV=production` |
|
|
44
|
+
| `doctor` | | | Check Node.js, `@meghjs/core`, `reflect-metadata` and decorator settings |
|
|
45
|
+
| `route:list` | `routes` | `--json`, `--static` | List the app's routes |
|
|
46
|
+
| `make:model` | `m:m` | `-f`, `--orm`, `--root <dir>` | Create a model / TypeORM entity |
|
|
47
|
+
| `make:controller` | `m:c` | `-f`, `-r`, `-m <model>`, `--orm`, `--root <dir>`, `--base-path` | Create a controller |
|
|
48
|
+
| `make:service` | `m:s` | `-f`, `-r`, `-m <model>`, `--orm`, `--root <dir>` | Create a service |
|
|
49
|
+
| `make:config` | `m:cnf` | `-f`, `--root <dir>` | Create an `@AppConfig` class |
|
|
50
|
+
| `make:middleware` | `m:mlwr` | `-f`, `--root <dir>` | Create a middleware |
|
|
51
|
+
| `make:dto` | `m:dto` | `-f`, `--root <dir>` | Create a class-validator DTO |
|
|
52
|
+
|
|
53
|
+
`-f` / `--force` overwrites an existing file. `-r` / `--resource` generates a
|
|
54
|
+
full REST skeleton. `--root <dir>` (also `-rt`) writes under `src/<dir>`
|
|
55
|
+
instead of the default folder. Names may be nested, e.g. `admin/User`.
|
|
56
|
+
Run `megh <command> --help` for details.
|
|
57
|
+
|
|
58
|
+
Generated controllers must be registered in your app with
|
|
59
|
+
`app.useControllers([...])`.
|
|
60
|
+
|
|
61
|
+
### Controller
|
|
62
|
+
|
|
63
|
+
```sh
|
|
64
|
+
npx megh make:controller product
|
|
65
|
+
# shorthand
|
|
66
|
+
npx megh m:c product
|
|
67
|
+
|
|
68
|
+
# REST controller — also generates the matching service
|
|
69
|
+
npx megh m:c product -m product -r
|
|
70
|
+
|
|
71
|
+
# REST controller backed by a TypeORM repository
|
|
72
|
+
npx megh m:c product -m product -r --orm
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
With `-m`, the generated controller delegates to `src/services/<model>.service.ts`,
|
|
76
|
+
which is created alongside it.
|
|
77
|
+
|
|
78
|
+
### Model / Entity
|
|
79
|
+
|
|
80
|
+
```sh
|
|
81
|
+
# plain class
|
|
82
|
+
npx megh make:model product
|
|
83
|
+
|
|
84
|
+
# TypeORM entity
|
|
85
|
+
npx megh m:m product --orm
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Service
|
|
89
|
+
|
|
90
|
+
```sh
|
|
91
|
+
# basic service
|
|
92
|
+
npx megh make:service product
|
|
93
|
+
|
|
94
|
+
# resource service backed by an in-memory Collection
|
|
95
|
+
npx megh make:service product -m product -r
|
|
96
|
+
|
|
97
|
+
# resource service backed by a TypeORM repository
|
|
98
|
+
npx megh make:service product -m product -r --orm
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Middleware
|
|
102
|
+
|
|
103
|
+
```sh
|
|
104
|
+
npx megh make:middleware auth
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Generates a class extending `MeghMiddleware` with an `invoke(req, res)` method.
|
|
108
|
+
|
|
109
|
+
### Config
|
|
110
|
+
|
|
111
|
+
```sh
|
|
112
|
+
npx megh make:config jwt
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Generates a class extending `MeghConfig`, registered via `@AppConfig`.
|
|
116
|
+
|
|
117
|
+
## Development Server
|
|
118
|
+
|
|
119
|
+
```sh
|
|
120
|
+
npx megh serve
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Compiles `src/` with SWC into `dist/`, starts the entry point, and restarts on
|
|
124
|
+
change to sources, assets or `.env*` files. The entry file is taken from
|
|
125
|
+
`--entry`, `megh.entry` in `package.json`, `main`, then `dist/serve.js`,
|
|
126
|
+
`server.js`, `main.js` or `index.js`.
|
|
127
|
+
|
|
128
|
+
Type errors are reported by a parallel `tsc --noEmit --watch` (disable with
|
|
129
|
+
`--no-type-check`) and never block a reload. Syntax errors stop the app until
|
|
130
|
+
they are fixed, so stale code never keeps serving.
|
|
131
|
+
|
|
132
|
+
## Build and Run
|
|
133
|
+
|
|
134
|
+
```sh
|
|
135
|
+
npx megh build # tsc with tsconfig.build.json, then copy non-TS assets
|
|
136
|
+
npx megh start # node dist/serve.js with NODE_ENV=production
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Routes
|
|
140
|
+
|
|
141
|
+
```sh
|
|
142
|
+
npx megh route:list # compile and read Megh's route metadata
|
|
143
|
+
npx megh route:list --static # scan decorators in the source, no dependencies
|
|
144
|
+
npx megh routes --json
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Doctor
|
|
148
|
+
|
|
149
|
+
```sh
|
|
150
|
+
npx megh doctor
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Checks the Node.js version, that `@meghjs/core` matches the CLI's version,
|
|
154
|
+
that `reflect-metadata` is installed, and that `experimentalDecorators` and
|
|
155
|
+
`emitDecoratorMetadata` are enabled. Exits non-zero if a check fails.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ApiController, Body, Delete, Get, Param, Post, Put, Query } from '@meghjs/core';
|
|
2
|
+
import { Create{Model}Dto, List{Plural}Query, Update{Model}Dto } from '{dtoImport}';
|
|
3
|
+
import { {ServiceClass} } from '{serviceImport}';
|
|
4
|
+
|
|
5
|
+
@ApiController('/{route}')
|
|
6
|
+
export class {ClassName} {
|
|
7
|
+
constructor(private readonly {serviceVar}: {ServiceClass}) {}
|
|
8
|
+
|
|
9
|
+
@Get()
|
|
10
|
+
index(@Query() query: List{Plural}Query) {
|
|
11
|
+
return this.{serviceVar}.paginate(query);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
@Get('/:id')
|
|
15
|
+
show(@Param('id') id: {primaryKeyType}) {
|
|
16
|
+
return this.{serviceVar}.findOrFail({idArg});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@Post()
|
|
20
|
+
create(@Body() body: Create{Model}Dto) {
|
|
21
|
+
return this.{serviceVar}.create(body);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@Put('/:id')
|
|
25
|
+
update(@Param('id') id: {primaryKeyType}, @Body() body: Update{Model}Dto) {
|
|
26
|
+
return this.{serviceVar}.update({idArg}, body);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@Delete('/:id')
|
|
30
|
+
async delete(@Param('id') id: {primaryKeyType}) {
|
|
31
|
+
await this.{serviceVar}.delete({idArg});
|
|
32
|
+
return { message: '{Model} deleted.' };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ApiController, Get, Post, Put, Delete, Param, Query, Body, HttpExceptions } from '@meghjs/core';
|
|
2
|
+
import { {ServiceClass} } from '{serviceImport}';
|
|
3
|
+
|
|
4
|
+
@ApiController('/{route}')
|
|
5
|
+
export class {ClassName} {
|
|
6
|
+
constructor(private readonly {serviceVar}: {ServiceClass}) {}
|
|
7
|
+
|
|
8
|
+
@Get()
|
|
9
|
+
async index(@Query() query?: any) {
|
|
10
|
+
if (query && query.page) {
|
|
11
|
+
return this.{serviceVar}.paginate(Number(query.page));
|
|
12
|
+
}
|
|
13
|
+
return this.{serviceVar}.findAll();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@Get('/:id')
|
|
17
|
+
async findOne(@Param('id') id: number) {
|
|
18
|
+
const {modelVar} = await this.{serviceVar}.findByPk(Number(id));
|
|
19
|
+
if (!{modelVar}) throw HttpExceptions.notFound('{Model} not found.');
|
|
20
|
+
return {modelVar};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@Post()
|
|
24
|
+
async create(@Body() body: any) {
|
|
25
|
+
return this.{serviceVar}.create(body);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@Put('/:id')
|
|
29
|
+
async update(@Param('id') id: number, @Body() updateBody: any) {
|
|
30
|
+
const {modelVar} = await this.{serviceVar}.findByPk(Number(id));
|
|
31
|
+
if (!{modelVar}) throw HttpExceptions.notFound('{Model} not found.');
|
|
32
|
+
const isUpdated = await this.{serviceVar}.update(Number(id), updateBody);
|
|
33
|
+
if (!isUpdated) throw HttpExceptions.internalError("Can't update {Model}");
|
|
34
|
+
return { message: 'Updated successfully' };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@Delete('/:id')
|
|
38
|
+
async delete(@Param('id') id: number) {
|
|
39
|
+
const {modelVar} = await this.{serviceVar}.findByPk(Number(id));
|
|
40
|
+
if (!{modelVar}) throw HttpExceptions.notFound('{Model} not found.');
|
|
41
|
+
const isDeleted = await this.{serviceVar}.delete(Number(id));
|
|
42
|
+
if (!isDeleted) throw HttpExceptions.internalError("Can't delete {Model}");
|
|
43
|
+
return { message: 'Deleted successfully' };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ApiController, Get, Post, Put, Delete, Param, Query, Body } from '@meghjs/core';
|
|
2
|
+
|
|
3
|
+
@ApiController('/{route}')
|
|
4
|
+
export class {ClassName} {
|
|
5
|
+
@Get()
|
|
6
|
+
async index(@Query() query?: any) {
|
|
7
|
+
throw new Error('Method not implemented.');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
@Get('/:id')
|
|
11
|
+
async findOne(@Param('id') id: number) {
|
|
12
|
+
throw new Error('Method not implemented.');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@Post()
|
|
16
|
+
async create(@Body() body: any) {
|
|
17
|
+
throw new Error('Method not implemented.');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
@Put('/:id')
|
|
21
|
+
async update(@Param('id') id: number, @Body() updateBody: any) {
|
|
22
|
+
throw new Error('Method not implemented.');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@Delete('/:id')
|
|
26
|
+
async delete(@Param('id') id: number) {
|
|
27
|
+
throw new Error('Method not implemented.');
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Type } from 'class-transformer';
|
|
2
|
+
import { {validators} } from 'class-validator';
|
|
3
|
+
|
|
4
|
+
/** `POST /{route}` body. Columns the database fills in are optional. */
|
|
5
|
+
export class Create{Model}Dto {
|
|
6
|
+
{createFields}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** `PUT /{route}/:id` body: only the columns sent are changed. */
|
|
10
|
+
export class Update{Model}Dto {
|
|
11
|
+
{updateFields}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** `GET /{route}?page=1&perPage=10` */
|
|
15
|
+
export class List{Plural}Query {
|
|
16
|
+
@IsOptional()
|
|
17
|
+
@Type(() => Number)
|
|
18
|
+
@IsInt()
|
|
19
|
+
@Min(1)
|
|
20
|
+
page?: number;
|
|
21
|
+
|
|
22
|
+
@IsOptional()
|
|
23
|
+
@Type(() => Number)
|
|
24
|
+
@IsInt()
|
|
25
|
+
@Min(1)
|
|
26
|
+
@Max(100)
|
|
27
|
+
perPage?: number;
|
|
28
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineMigration } from "@meghjs/core";
|
|
2
|
+
|
|
3
|
+
export default defineMigration({
|
|
4
|
+
async up(schema) {
|
|
5
|
+
await schema.table("{table}", (table) => {
|
|
6
|
+
// table.string("column").nullable();
|
|
7
|
+
});
|
|
8
|
+
},
|
|
9
|
+
|
|
10
|
+
async down(schema) {
|
|
11
|
+
await schema.table("{table}", (table) => {
|
|
12
|
+
// table.dropColumn("column");
|
|
13
|
+
});
|
|
14
|
+
},
|
|
15
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineMigration } from "@meghjs/core";
|
|
2
|
+
|
|
3
|
+
export default defineMigration({
|
|
4
|
+
async up(schema) {
|
|
5
|
+
await schema.create("{table}", (table) => {
|
|
6
|
+
table.id();
|
|
7
|
+
// table.string("name");
|
|
8
|
+
table.timestamps();
|
|
9
|
+
});
|
|
10
|
+
},
|
|
11
|
+
|
|
12
|
+
async down(schema) {
|
|
13
|
+
await schema.drop("{table}");
|
|
14
|
+
},
|
|
15
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { defineMigration } from "@meghjs/core";
|
|
2
|
+
|
|
3
|
+
export default defineMigration({
|
|
4
|
+
async up(schema) {
|
|
5
|
+
// await schema.create(...), schema.table(...), schema.drop(...) or schema.raw(...)
|
|
6
|
+
},
|
|
7
|
+
|
|
8
|
+
async down(schema) {
|
|
9
|
+
// Undo the changes made in up().
|
|
10
|
+
},
|
|
11
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn } from 'typeorm';
|
|
2
|
+
|
|
3
|
+
@Entity({ name: '{table}' })
|
|
4
|
+
export class {Model} {
|
|
5
|
+
@PrimaryGeneratedColumn()
|
|
6
|
+
id!: number;
|
|
7
|
+
|
|
8
|
+
@Column()
|
|
9
|
+
name!: string;
|
|
10
|
+
|
|
11
|
+
@CreateDateColumn()
|
|
12
|
+
createdAt!: Date;
|
|
13
|
+
|
|
14
|
+
@UpdateDateColumn()
|
|
15
|
+
updatedAt!: Date;
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AutoIncrement, Column, Model, PrimaryKey, Table } from "@meghjs/core";
|
|
2
|
+
|
|
3
|
+
@Table({table})
|
|
4
|
+
export class {Model} extends Model {
|
|
5
|
+
@PrimaryKey() @AutoIncrement() id?: number;
|
|
6
|
+
|
|
7
|
+
// One decorated field per column, e.g.:
|
|
8
|
+
// @Column() email!: string;
|
|
9
|
+
// @Column({ nullable: true }) name!: string | null;
|
|
10
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { beforeAll, describe, expect, it } from 'vitest';
|
|
2
|
+
import { MeghTest } from '@meghjs/core';
|
|
3
|
+
import { HomeController } from './home.controller';
|
|
4
|
+
|
|
5
|
+
describe('HomeController', () => {
|
|
6
|
+
let controller!: HomeController;
|
|
7
|
+
|
|
8
|
+
beforeAll(() => {
|
|
9
|
+
controller = MeghTest.getController(HomeController);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('should be defined', () => {
|
|
13
|
+
expect(controller).toBeDefined();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('should return hello message', () => {
|
|
17
|
+
expect(controller.sayHello()).toEqual({ message: 'Hello World!' });
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ApiController, Get } from '@meghjs/core';
|
|
2
|
+
import { HomeService } from '../services/home.service';
|
|
3
|
+
|
|
4
|
+
@ApiController('/')
|
|
5
|
+
export class HomeController {
|
|
6
|
+
constructor(private readonly homeService: HomeService) {}
|
|
7
|
+
|
|
8
|
+
@Get()
|
|
9
|
+
sayHello() {
|
|
10
|
+
return this.homeService.sayHello();
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AppConfig, MeghConfig, Environment, OpenApiUiOptions } from '@meghjs/core';
|
|
2
|
+
|
|
3
|
+
@AppConfig
|
|
4
|
+
export class OpenApiConfig extends MeghConfig<OpenApiUiOptions> {
|
|
5
|
+
config(env: Environment): OpenApiUiOptions {
|
|
6
|
+
return {
|
|
7
|
+
info: {
|
|
8
|
+
title: '{AppTitle} API',
|
|
9
|
+
version: '0.0.1',
|
|
10
|
+
description: 'API documentation for {AppTitle}',
|
|
11
|
+
},{Provider}
|
|
12
|
+
routePrefix: '/docs',
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# {AppTitle}
|
|
2
|
+
|
|
3
|
+
Built with [Megh](https://github.com/meghjs/megh).
|
|
4
|
+
|
|
5
|
+
## Getting Started
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
{InstallCmd}
|
|
9
|
+
{DevCmd}
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Scripts
|
|
13
|
+
|
|
14
|
+
| Script | Description |
|
|
15
|
+
|--------|-------------|
|
|
16
|
+
| `start:dev` | Start the development server with reload (`megh serve`) |
|
|
17
|
+
| `build` | Compile TypeScript to `dist/` (`megh build`) |
|
|
18
|
+
| `start` | Run the compiled app in production (`megh start`) |
|
|
19
|
+
{TestRow}
|
|
20
|
+
## Project Structure
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
src/
|
|
24
|
+
{Tree}├── app.ts # App setup
|
|
25
|
+
└── serve.ts # Entry point
|
|
26
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "CommonJS",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"experimentalDecorators": true,
|
|
7
|
+
"emitDecoratorMetadata": true,
|
|
8
|
+
"outDir": "./dist",
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"allowSyntheticDefaultImports": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"sourceMap": true,
|
|
14
|
+
"strict": false
|
|
15
|
+
},
|
|
16
|
+
"include": ["megh-env.d.ts", "src/**/*"],
|
|
17
|
+
"exclude": ["node_modules", "dist"]
|
|
18
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import swc from 'unplugin-swc';
|
|
2
|
+
import { defineConfig } from 'vitest/config';
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
// SWC emits the decorator metadata that Megh's dependency injection
|
|
6
|
+
// and validation rely on; Vitest's default transform (esbuild) does not.
|
|
7
|
+
plugins: [swc.vite()],
|
|
8
|
+
test: {
|
|
9
|
+
environment: 'node',
|
|
10
|
+
include: ['src/**/*.spec.ts'],
|
|
11
|
+
coverage: {
|
|
12
|
+
include: ['src/**/*.ts'],
|
|
13
|
+
exclude: ['src/**/*.spec.ts'],
|
|
14
|
+
reportsDirectory: 'coverage',
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { AppService, type CreateInput, type PageRequest, type UpdateInput } from '@meghjs/core';
|
|
2
|
+
import { {Model} } from '{modelImport}';
|
|
3
|
+
|
|
4
|
+
@AppService
|
|
5
|
+
export class {ClassName} {
|
|
6
|
+
/** Every {modelVar}, ordered by {primaryKey}. */
|
|
7
|
+
findAll() {
|
|
8
|
+
return {Model}.query().orderBy('{primaryKey}').get();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** One page of {modelVar} rows: `{ data, meta }`. */
|
|
12
|
+
paginate(request: PageRequest = {}) {
|
|
13
|
+
return {Model}.query().orderBy('{primaryKey}').paginate(request);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The {modelVar} with this {primaryKey}, or `undefined`. */
|
|
17
|
+
find(id: {primaryKeyType}) {
|
|
18
|
+
return {Model}.find(id);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The {modelVar} with this {primaryKey}; throws a 404 when there is none. */
|
|
22
|
+
findOrFail(id: {primaryKeyType}) {
|
|
23
|
+
return {Model}.findOrFail(id);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
create(input: CreateInput<typeof {Model}>) {
|
|
27
|
+
return {Model}.create(input);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** `save()` writes only the columns that changed. */
|
|
31
|
+
async update(id: {primaryKeyType}, changes: UpdateInput<{Model}>) {
|
|
32
|
+
const {modelVar} = await {Model}.findOrFail(id);
|
|
33
|
+
// A DTO instance holds `undefined` for every field that was not sent.
|
|
34
|
+
const sent = Object.fromEntries(Object.entries(changes).filter(([, value]) => value !== undefined));
|
|
35
|
+
return {modelVar}.fill(sent).save();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async delete(id: {primaryKeyType}) {
|
|
39
|
+
const {modelVar} = await {Model}.findOrFail(id);
|
|
40
|
+
await {modelVar}.delete();
|
|
41
|
+
}
|
|
42
|
+
}
|