NestJS materials

Write an awesome doc for NestJS tips, tricks, notes, things which are not in the doc (or are but not really obvious) and experimented to figure them out and use them.

View on GitHub

Protobuf

[!NOTE]

We can use something like JSON as our data exchange format, but .proto files have several advantages which make them a better candidate for the job.

How It Works

  1. Define the structure for the data you wanna serialize in a proto file.

    message Person {
      int32 id = 1;
      string name = 2;
      bool is_active = 3;
    }
    

    message: a small logical record of information containing a series of name-value pairs called fields

  2. Use protoc to generate data access classes in your preferred language from your .proto definition.

    [!TIP]

    Though they are supporting JS, but if you need TS interfaces, etc you need to turn to some other libraries such as ts-proto.

    ts-protoc --ts_proto_out=./output -I=./protos ./protoc/*.proto --ts_proto_opt=addGrpcMetadata=true --ts_proto_opt=addNestjsRestParameter=true --ts_proto_opt=nestJs=true --ts_proto_opt=exportCommonSymbols=false
    

    Learn more here.