Skip to content

Commit 47892fc

Browse files
committed
add some examples & update nestcloud version
1 parent 36910e6 commit 47892fc

File tree

10 files changed

+94
-283
lines changed

10 files changed

+94
-283
lines changed

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@
2222
"@godaddy/terminus": "^4.1.0",
2323
"@nestcloud/boot": "^0.1.4",
2424
"@nestcloud/common": "^0.1.3",
25-
"@nestcloud/consul": "^0.1.4",
25+
"@nestcloud/consul": "^0.1.5",
2626
"@nestcloud/consul-config": "^0.1.4",
2727
"@nestcloud/consul-loadbalance": "^0.1.4",
28-
"@nestcloud/consul-service": "^0.1.2",
28+
"@nestcloud/consul-service": "^0.1.3",
2929
"@nestcloud/feign": "^0.1.2",
3030
"@nestcloud/gateway": "^0.1.1",
3131
"@nestcloud/logger": "^0.2.0",
3232
"@nestcloud/schedule": "^0.1.0",
3333
"@nestcloud/validations": "^0.1.0",
34-
"@nestjs/common": "^6.0.1",
35-
"@nestjs/core": "^6.0.1",
36-
"@nestjs/microservices": "^6.0.1",
37-
"@nestjs/platform-express": "^6.0.1",
34+
"@nestjs/common": "^6.0.2",
35+
"@nestjs/core": "^6.0.2",
36+
"@nestjs/microservices": "^6.0.2",
37+
"@nestjs/platform-express": "^6.0.2",
3838
"@nestjs/swagger": "^3.0.1",
3939
"@nestjs/terminus": "^6.0.0",
40-
"@nestjs/testing": "^6.0.1",
40+
"@nestjs/testing": "^6.0.2",
4141
"@nestjs/typeorm": "^6.0.0",
42-
"@nestjs/websockets": "^5.7.4",
42+
"@nestjs/websockets": "^6.0.2",
4343
"class-validator": "^0.8.5",
4444
"consul": "^0.34.1",
4545
"mysql": "^2.16.0",

src/app.module.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ import { ConsulConfigModule } from '@nestcloud/consul-config';
55
import { ConsulServiceModule } from '@nestcloud/consul-service';
66
import { LoadbalanceModule } from '@nestcloud/consul-loadbalance';
77
import { FeignModule } from '@nestcloud/feign';
8-
import { NEST_BOOT, NEST_CONSUL_LOADBALANCE, NEST_BOOT_PROVIDER, NEST_TYPEORM_LOGGER_PROVIDER } from '@nestcloud/common';
8+
import {
9+
NEST_BOOT,
10+
NEST_CONSUL_LOADBALANCE,
11+
NEST_BOOT_PROVIDER,
12+
NEST_TYPEORM_LOGGER_PROVIDER,
13+
components,
14+
repositories
15+
} from '@nestcloud/common';
916
import { TypeOrmHealthIndicator, TerminusModule, TerminusModuleOptions } from "@nestjs/terminus";
1017
import { TypeOrmModule } from '@nestjs/typeorm';
1118
import { GatewayModule } from '@nestcloud/gateway';
1219

13-
import { components, repos } from "./utils/ProviderUtils";
1420
import * as controllers from './controllers';
15-
import * as repositories from './repositories';
21+
import * as repos from './repositories';
1622
import * as services from './services';
1723
import * as clients from './clients';
1824
import { LoggerModule, TypeormLogger } from '@nestcloud/logger';
@@ -60,7 +66,7 @@ const getTerminusOptions = (db: TypeOrmHealthIndicator): TerminusModuleOptions =
6066
})
6167
],
6268
controllers: components(controllers),
63-
providers: components(repos(repositories), services, clients)
69+
providers: components(repositories(repos), services, clients)
6470
})
6571
export class AppModule {
6672
}

src/bootstrap-development.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ logger:
5454
# 200M
5555
maxsize: 209715200
5656
maxFiles: 10
57+
custom:
58+
data: test
5759

src/controllers/ArticleController.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export class ArticleController {
1010

1111
@Get()
1212
async getArticles() {
13-
console.log('test')
1413
return await this.articleClient.getArticles();
1514
}
1615
}

src/controllers/UserController.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Body, Controller, Get, Post, Query } from "@nestjs/common";
1+
import { Body, Controller, Get, Param, Post, Put, Query } from "@nestjs/common";
2+
import { IsValid, IsNotEmpty } from '@nestcloud/validations';
23
import { User } from "../entities";
34
import { UserRepository } from "../repositories";
45
import { UserService } from "../services";
@@ -20,7 +21,12 @@ export class UserController {
2021
}
2122

2223
@Post()
23-
async createUser(@Body('user') user: User) {
24+
async createUser(@Body('user', new IsValid()) user: User) {
2425
await this.userRepo.save(user);
2526
}
27+
28+
@Put(':userId')
29+
async updateUser(@Param('userId') userId: string, @Body('name', new IsNotEmpty()) name: string) {
30+
await this.userRepo.update(userId, { name });
31+
}
2632
}

src/entities/User.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
2+
import { IsNotEmpty } from "class-validator";
23

34
@Entity('user')
45
export class User {
56
@PrimaryGeneratedColumn('uuid')
67
id: string;
78

89
@Column('varchar')
10+
@IsNotEmpty()
911
name: string;
1012
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
1212
const logger = defaults.logger = new NestLogger({ path: context, filename });
1313

1414
async function bootstrap() {
15-
const boot = new Boot(__dirname);
15+
const boot = new Boot(__dirname, filename);
1616
const app = await NestFactory.create(AppModule, { logger });
1717

1818
process.on('SIGINT', async () => {

src/services/ScheduleService.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,44 @@ import { Injectable } from "@nestjs/common";
22
import { NestSchedule, Interval } from "@nestcloud/schedule";
33
import { InjectLogger } from '@nestcloud/logger';
44
import { LoggerInstance } from 'winston';
5+
import { Bootstrap, BootValue } from '@nestcloud/boot';
6+
import { Configuration, ConfigValue } from '@nestcloud/consul-config';
7+
import { WatchKV } from '@nestcloud/consul';
58

69
@Injectable()
10+
@Bootstrap()
11+
@Configuration()
712
export class ScheduleService extends NestSchedule {
13+
@BootValue('custom.data', 'default custom data')
14+
private readonly customData: string;
15+
16+
// In this project, the consul key is 'config__nestcloud-starter-service__development'
17+
// Please insert data in it.
18+
@ConfigValue('custom.data', 'default custom data')
19+
private readonly consulCustomData: string;
20+
21+
// Please insert text into consul kv 'test-key'
22+
@WatchKV('test-key', 'text', 'default consul data')
23+
private readonly consulData: string;
24+
825
constructor(
926
@InjectLogger() private readonly logger: LoggerInstance,
1027
) {
1128
super();
1229
}
1330

1431
@Interval(2000)
15-
intervalJob() {
16-
this.logger.info('executing interval job')
32+
intervalBootJob() {
33+
this.logger.info('interval get custom data from boot: ', this.customData);
34+
}
35+
36+
@Interval(2000)
37+
intervalConsulConfigJob() {
38+
this.logger.info('interval get custom from consul config: ', this.consulCustomData);
39+
}
40+
41+
@Interval(2000)
42+
intervalConsulJob() {
43+
this.logger.info('interval get custom from consul: ', this.consulData);
1744
}
1845
}

src/utils/ProviderUtils.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)