Commit 3fce2344 authored by nanahira's avatar nanahira

add readme

parent bb7b7d18
# nicot
Nest.js interacting with class-validator + OpenAPI + TypeORM for Nest.js Restful API development.
## Install
In your Nest.js project, run the following command:
```bash
npm install @nestjs/swagger typeorm @nestjs/typeorm class-validator class-transformer nicot
```
## Entity
Those decorators would all decorate the following, with the SAME settings.
- TypeORM `@Entity()` settings.
- class-validator validation settings.
- `@nestjs/swagger` `@ApiProperty()` settings.
```ts
@Entity()
export class User extends IdBase() {
@Index()
@StringColumn(5, {
required: true,
description: 'User name',
})
name: string;
@IntColumn('int', { unsigned: true, description: 'User age', default: 20 })
age: number;
@EnumColumn(Gender, { description: 'User gender' })
gender: Gender;
}
```
## CrudService
Creates a service for database operation in one word.
```ts
@Injectable()
export class UserService extends CrudService(User) {
constructor(@InjectDataSource() db: DataSource) {
super(db.getRepository(User));
}
}
```
## Controller decorators
```ts
const dec = new RestfulFactory(User);
class UpdateUserDto extends dec.updateDto {} // to extract type and class
@Controller('user')
export class UserController {
constructor(private userService: UserService) {}
@dec.create() // POST /
create(@dec.createParam() user: User) {
return this.userService.create(user);
}
@dec.findOne() // GET /:id
findOne(@dec.idParam() id: number) {
return this.userService.findOne(id);
}
@dec.findAll() // GET /
findAll(@dec.findAllParam() user: UpdateUserDto) {
return this.userService.findAll(user);
}
@dec.update() // PATH /:id
update(@dec.idParam() id: number, @dec.updateParam() user: UpdateUserDto) {
return this.userService.update(id, user);
}
@dec.delete() // DELETE /:id
delete(@dec.idParam() id: number) {
return this.userService.delete(id);
}
}
```
......@@ -4,13 +4,13 @@ import { CrudService } from '../src/crud-base';
import { Gender, User } from './utility/user';
import { RestfulFactory } from '../src/decorators';
import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import { InjectDataSource, TypeOrmModule } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
import request from 'supertest';
@Injectable()
class UserService extends CrudService(User) {
constructor(db: DataSource) {
constructor(@InjectDataSource() db: DataSource) {
super(db.getRepository(User));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment