nicot 1.0.7 → 1.0.8
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 +84 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,87 @@
|
|
|
1
1
|
# nicot
|
|
2
2
|
|
|
3
3
|
Nest.js interacting with class-validator + OpenAPI + TypeORM for Nest.js Restful API development.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
In your Nest.js project, run the following command:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @nestjs/swagger typeorm @nestjs/typeorm class-validator class-transformer nicot
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Entity
|
|
14
|
+
|
|
15
|
+
Those decorators would all decorate the following, with the SAME settings.
|
|
16
|
+
|
|
17
|
+
- TypeORM `@Entity()` settings.
|
|
18
|
+
- class-validator validation settings.
|
|
19
|
+
- `@nestjs/swagger` `@ApiProperty()` settings.
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
@Entity()
|
|
23
|
+
export class User extends IdBase() {
|
|
24
|
+
@Index()
|
|
25
|
+
@StringColumn(5, {
|
|
26
|
+
required: true,
|
|
27
|
+
description: 'User name',
|
|
28
|
+
})
|
|
29
|
+
name: string;
|
|
30
|
+
|
|
31
|
+
@IntColumn('int', { unsigned: true, description: 'User age', default: 20 })
|
|
32
|
+
age: number;
|
|
33
|
+
|
|
34
|
+
@EnumColumn(Gender, { description: 'User gender' })
|
|
35
|
+
gender: Gender;
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## CrudService
|
|
40
|
+
|
|
41
|
+
Creates a service for database operation in one word.
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
@Injectable()
|
|
45
|
+
export class UserService extends CrudService(User) {
|
|
46
|
+
constructor(@InjectDataSource() db: DataSource) {
|
|
47
|
+
super(db.getRepository(User));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Controller decorators
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
const dec = new RestfulFactory(User);
|
|
56
|
+
class UpdateUserDto extends dec.updateDto {} // to extract type and class
|
|
57
|
+
|
|
58
|
+
@Controller('user')
|
|
59
|
+
export class UserController {
|
|
60
|
+
constructor(private userService: UserService) {}
|
|
61
|
+
|
|
62
|
+
@dec.create() // POST /
|
|
63
|
+
create(@dec.createParam() user: User) {
|
|
64
|
+
return this.userService.create(user);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@dec.findOne() // GET /:id
|
|
68
|
+
findOne(@dec.idParam() id: number) {
|
|
69
|
+
return this.userService.findOne(id);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@dec.findAll() // GET /
|
|
73
|
+
findAll(@dec.findAllParam() user: UpdateUserDto) {
|
|
74
|
+
return this.userService.findAll(user);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
@dec.update() // PATH /:id
|
|
78
|
+
update(@dec.idParam() id: number, @dec.updateParam() user: UpdateUserDto) {
|
|
79
|
+
return this.userService.update(id, user);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
@dec.delete() // DELETE /:id
|
|
83
|
+
delete(@dec.idParam() id: number) {
|
|
84
|
+
return this.userService.delete(id);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
package/package.json
CHANGED