1.0.56 • Published 22 days ago

nicot v1.0.56

Weekly downloads
-
License
MIT
Repository
-
Last release
22 days 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.55

22 days ago

1.0.54

23 days ago

1.0.56

22 days ago

1.0.53

5 months ago

1.0.52

5 months ago

1.0.51

5 months ago

1.0.50

5 months ago

1.0.39

7 months ago

1.0.38

7 months ago

1.0.40

7 months ago

1.0.44

6 months ago

1.0.43

7 months ago

1.0.42

7 months ago

1.0.41

7 months ago

1.0.48

5 months ago

1.0.47

5 months ago

1.0.46

6 months ago

1.0.45

6 months ago

1.0.49

5 months ago

1.0.37

8 months ago

1.0.29

12 months ago

1.0.33

11 months ago

1.0.32

11 months ago

1.0.31

11 months ago

1.0.30

11 months ago

1.0.36

9 months ago

1.0.35

9 months ago

1.0.34

11 months ago

1.0.28

12 months ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.27

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago