美文网首页
基于nest的grpc crud解决方案

基于nest的grpc crud解决方案

作者: 张涛_fd87 | 来源:发表于2020-10-09 20:04 被阅读0次

nest-grpc-crud

基于nest的grpc crud解决方案

English | 简体中文

目录

功能

  • 快速构建基于nest的grpc微服务模板
  • 批量生成数据库crud操作
  • 自动生成proto3契约文件

安装

使用git:

$ git clone https://github.com/zhangtao25/nest-grpc-crud.git

例子

请关注generate文件夹

这个文件夹存放了生成代码的核心文件

//修改data.json文件
{
    "columns": [
        {
            "name": "student",
            "Name": "Student",
            "column": [
                {
                    "name": "id",
                    "type": "int32"
                },
                {
                    "name": "name",
                    "type": "string"
                }
            ]
        }
    ]
}

终端运行该命令,生成目标文件

$ node generate/test.js

生成的代码

// app.controller.ts
@Controller()
export class AppController {
    constructor(
        private readonly studentCrudService: StudentCrudService,
    
    ) {
    }
    
    // student
    @GrpcMethod(StudentCrudService.service, 'Create' + StudentCrudService.method)
    async createStudent(req): Promise<any> {
    return await this.studentCrudService.create(req);
    }

    @GrpcMethod(StudentCrudService.service, 'Delete' + StudentCrudService.method)
    async deleteStudent(req): Promise<any> {
    const id = req.id;
    return await this.studentCrudService.delete(id);
    }

    @GrpcMethod(StudentCrudService.service, 'Update' + StudentCrudService.method)
    async updateStudent(req): Promise<any> {
    const id = req.id;
    delete req.id
    return await this.studentCrudService.update(id,req);
    }

    @GrpcMethod(StudentCrudService.service, 'Find' + StudentCrudService.method)
    async findStudent(req): Promise<any> {
    const commonField = req.commonField;
    const entity = req.entity;
    return await this.studentCrudService.find(entity, commonField);
    }
}
//proto3契约文件
syntax = "proto3";
package grpcService; // 包名


message CommonField {
    string pageIndex = 1;
    string pageSize = 2;
}

service StudentService {
    rpc CreateStudent (CreateStudentRequestType) returns (CreateStudentResponseType) {}
    rpc DeleteStudent (DeleteStudentRequestType) returns (DeleteStudentResponseType) {}
    rpc UpdateStudent (UpdateStudentRequestType) returns (UpdateStudentResponseType) {}
    rpc FindStudent (FindStudentRequestType) returns (FindStudentResponseType) {}
}

//Student
message Student {
    
        int32 id = 1;
        string name = 2;
}

message CreateStudentRequestType{
    
        int32 id = 1;
        string name = 2;
}

message CreateStudentResponseType{
    int32 responseCode = 2;
    string responseDesc =  3;
}

message DeleteStudentRequestType{
    string id = 1;
}

message DeleteStudentResponseType{
    int32 responseCode = 2;
    string responseDesc =  3;
}

message UpdateStudentRequestType{
    
        int32 id = 1;
        string name = 2;
}

message UpdateStudentResponseType{
    int32 responseCode = 2;
    string responseDesc =  3;
}

message FindStudentRequestType{
    Student entity = 1;
    CommonField commonField = 2;
}

message FindStudentResponseType{
    repeated Student data = 1;
    int32 responseCode = 2;
    string responseDesc =  3;
}
    
// apps/student/student.crud.service.ts

@Injectable()
export class StudentCrudService extends CrudService<Student>{
    constructor(
        @InjectRepository(Student, 'regConnection') repo
    ) {
        super(repo)
    }

    public static method = 'Student';
    public static service = 'StudentService';

    public static request = {};
    public static response = {};
}
// apps/student/student.entity.ts

@Entity({
    name: 'student'
})
export class Student {

    @PrimaryGeneratedColumn({ name: 'Id' })
    id: number;

    @Column({ name: 'Name' })
    name: string;
}
// app.module.ts
// 在该文件中添加student实体类和crud service
@Module({
  imports: [
      TypeOrmModule.forRoot({
          name: '***',
          type: 'mysql',
          host: '***',
          port: '***',
          username: '***',
          password: '***',
          database: '***',
          entities:[Student],
          autoLoadEntities: true,
          synchronize: true
      }),
      TypeOrmModule.forFeature([Student], 'regConnection')
  ],
  controllers: [AppController],
  providers: [StudentCrudService],
})
export class AppModule {}

grpc客户端调用方式(配套grpc客户端地址 nest-grpc-crud-client

@Controller('')
export class AppController implements OnModuleInit {
    private studentService;

    constructor(@Inject(ClentServe) private readonly clentServe: ClentServe) {
    }

    onModuleInit() {
        this.studentService = this.clentServe.client.getService('StudentService');
    }

    // Student
    @Post('createStudent')
    createStudent(@Body() body) {
        return this.studentService.createStudent(body);
    }

    @Post('deleteStudent')
    deleteStudent(@Body() body) {
        return this.studentService.deleteStudent(body);
    }

    @Post('updateStudent')
    updateStudent(@Body() body) {
        return this.studentService.updateStudent(body);
    }

    @Post('findStudent')
    findStudent(@Body() body) {
        return this.studentService.findStudent(body);
    }
}

反馈

QQ群: 1042755904

License

MIT

Copyright (c) 2020-present, zhangtao25

相关文章

  • 基于nest的grpc crud解决方案

    nest-grpc-crud 基于nest的grpc crud解决方案 English[./README.md] ...

  • golang grpc

    golang grpc rpc种类 grpc含义 gRPC是Google的RPC框架,开源、高性能、跨语言,基于H...

  • grpc协议解析及新语言支持

    (一)grpc 协议分析。gRPC 是基于 HTTP/2 协议的,要深刻理解 gRPC,理解下 HTTP/2 是必...

  • Mybatis使用与配置

    本测试项目涵盖了Mybatis框架的入门快速构建、基于xml的CRUD操作、配置信息说明、基于注解的CRUD操作、...

  • 分享一下 gRPC- HTTP网关 I

    [TOC] gRPC- HTTP网关 I gRPC是什么?我们再来回顾一下 gRPC是 Google开发的基于HT...

  • gRPC 学习笔记

     gRPC 学习笔记,记录gprc一些基本概念.  gRPC正如其他 RPC 系统,gRPC 基于如下思想:定义一...

  • gRPC-transport包源码分析

    gRPC是基于HTTP/2标准和proto协议开发的,gRPC的很多特性都依赖于HTTP/2标准提供。gRPC设计...

  • gRPC之IOSHelloworld

    gRPC是google开源的一个跨平台网络层解决方案,旨在提高移动端网络层的性能,提升移动端网络层的开发效率,基于...

  • google grpc 简介及官网的快速入门

    GRPC是基于protocol buffers3.0协议的. 本文将向您介绍gRPC和protocol buffe...

  • gRPC

    gRPC是Googel基于HTTP/2以及protobuf的。gRPC通常有四种模式,unary,client s...

网友评论

      本文标题:基于nest的grpc crud解决方案

      本文链接:https://www.haomeiwen.com/subject/pyxzuqtx.html