11.2.4 • Published 5 months ago

nest-crud-server v11.2.4

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

Ví dụ

Sử dụng Crud và TypeormCrudService để tạo RestfulAPI

File .controller

import { Controller } from "@nestjs/common";
import { Crud } from "nest-crud-server";
import { PostService } from "./post.service";

@Crud({
 params: {
  primaryKey: "postId",
 },
 routes: {
  exclude: ["getCountBase"],
  updateOneBase: {
   // allowParamsOverride: true,
  },
 },
})
@Controller("post")
export class PostController {
 constructor(private service: PostService) {}
}

File .service

import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { TypeOrmCrudService } from "nest-crud-server";
import { Repository } from "typeorm";
import { PostEntity } from "./post.entity";

@Injectable()
export class PostService extends TypeOrmCrudService<PostEntity> {
 constructor(@InjectRepository(PostEntity) repo: Repository<PostEntity>) {
  super(repo);
 }
}

File .entity

import {
 Column,
 CreateDateColumn,
 Entity,
 PrimaryColumn,
 UpdateDateColumn,
} from "typeorm";

@Entity({ name: "Post" })
export class PostEntity {
 @PrimaryColumn({ generated: "increment" })
 postId: number;
 @Column()
 title: string;
 avatarSrc: string;
 @Column({ default: "" })
 description: string;
 @Column({ default: "" })
 shortDescription: string;

 @CreateDateColumn() createDate: Date;
 @UpdateDateColumn() updateDate: Date;
}

Mô tả

Cách sử dụng Crud

Tham số truyền vào

  • params.primaryKey: kiểu string là khóa chính của Entity. Ví dụ PostEntity có khóa chính là postId
  • routes gồm nhiều tham số khác nhau

    • only: kiểu string[] bao gồm các giá trị: "getManyBase" | "getOneBase" | "createOneBase" | "createManyBase" | "updateOneBase" | "replaceOneBase" | "deleteOneBase" | "recoverOneBase" | "getCountBase" | "getSumBase"

    Ví dụ Controller chỉ muốn cung cấp API để getManycreateOne thì truyền

    routes: {
     only: ["getManyBase", "createOneBase"];
    }

    Với ví dụ trên thì chỉ request đến Controller với 2 phương thức là GET (để lấy danh sách) và POST (để tạo)

    • exclude: kiểu string[] tương tự only nhưng ngược lại

    Ví dụ Controller có tất cả phương thức: getMany (lấy danh sách), getOne (lấy một đối tượng), createOne (tạo đối tượng), updateOne (cập nhật đối tượng), deleteOne (xóa đối tượng)... và bạn muốn bỏ phương thức createMany (tạo nhiều đối tượng) thì thực hiện như sau

    routes: {
     exclude: ["createManyBase"];
    }
11.2.4

5 months ago

11.2.3

6 months ago

11.2.2

6 months ago

11.2.1

6 months ago

11.2.0

6 months ago

11.1.2

6 months ago

11.1.1

6 months ago

11.1.0

6 months ago

11.0.3

6 months ago

11.0.2

6 months ago

11.0.1

6 months ago

11.0.0

6 months ago

10.1.4

6 months ago

10.1.3

6 months ago

10.1.2

6 months ago

10.1.1

6 months ago

10.1.0

6 months ago

10.0.0

6 months ago

9.1.9

6 months ago

9.1.8

7 months ago

9.1.7

7 months ago

9.1.6

7 months ago

9.1.5

7 months ago

9.1.4

7 months ago

9.1.3

7 months ago

9.1.2

7 months ago

9.1.1

7 months ago

9.1.0

7 months ago

9.0.2

7 months ago

9.0.1

7 months ago

9.0.0

7 months ago