1.0.59 • Published 8 months ago

nicot v1.0.59

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

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:

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.
@Entity()
export class User extends IdBase() {
  @Index()
  @QueryLike() // queries as 'where name LIKE :name%'
  @StringColumn(5, {
    required: true,
    description: 'User name',
  })
  name: string;

  @QueryEqual() // queries as 'where age = :age'
  @IntColumn('int', { unsigned: true, description: 'User age', default: 20 })
  age: number;

  @EnumColumn(Gender, { description: 'User gender' })
  gender: Gender;

  @NotColumn()
  somethingElse: any; // Would not come from client input, and would not go into OpenAPI document.

  // possible optional override operations

  override isValidInCreate() { // Custom before-create check.
    if (!this.name.length) {
      return 'Name cannot be empty!';
    }
  }

  override isValidInUpdate() { // Custom before-update check.
    if (this.name && !this.name.length) {
      return 'Name cannot be empty!';
    }
  }

  override async beforeCreate() {
    this.name = this.name.toLowerCase(); // Do something before create.
  }

  override async afterCreate() {
    this.name = this.name.toUpperCase(); // Do something after create before sending to user.
  }

  override async beforeGet() {
  }

  override async afterGet() {
  }

  override async beforeUpdate() {
  }
}

There are also other following decorators to control accessibility:

  • @NotWritable() Can only come from GET requests.
  • @NotChangeable() Cannot be changed by PATCH requests.

CrudService

Creates a service for database operation in one word.

@Injectable()
export class UserService extends CrudService(User) {
  constructor(@InjectDataSource() db: DataSource) {
    super(db.getRepository(User));
  }
}

Controller decorators

Would also register proper OpenAPI documentation for the controller.

const dec = new RestfulFactory(User);
class FindAllUsersDto extends dec.findAllDto {} // to extract type and class
class UpdateUserDto extends dec.updateDto {}

@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: FindAllUsersDto) {
    return this.userService.findAll(user);
  }

  @dec.update() // PATCH /: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);
  }
}

Return message

Return data of all APIs are in the following format, with proper OpenAPI documentation:

export interface ReturnMessage<T> {
  statusCode: number;
  message: string;
  success: boolean;
  data: T;
}

You may also create a Dto class like this by the following way:

export class UserReturnMessage extends ReturnMessageDto(User) {}

With result into the following class, also with proper OpenAPI documentation:

export class UserReturnMessage {
  statusCode: number;
  message: string;
  success: boolean;
  data: User;
}
1.0.59

8 months ago

1.0.58

11 months ago

1.0.57

12 months ago

1.0.55

1 year ago

1.0.54

1 year ago

1.0.56

1 year ago

1.0.53

2 years ago

1.0.52

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.40

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.49

2 years ago

1.0.37

2 years ago

1.0.29

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.28

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.27

2 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago