Introduction
If you're familiar with NestJS and gRPC, you may have already referred to the official documentation on implementing gRPC in NestJS. While Protoc is a standard tool, as you delve deeper into customization, its complexity increases.
This is where Buf comes in. Buf offers a simpler alternative to Protoc for generating SDKs, documentation, and more. Despite its simplicity, I noticed a lack of sample projects or showcases integrating Buf with NestJS.
Therefore, I took it upon myself to create one.
Overview of repository
This repository (https://github.com/Ko1103/nestjs-buf-example) is a NestJS and Buf usage example for protobuf server implementation.
It will help you to understand about
- directory structure
- explains requirements
- installation process
- how to run the application
Project Structure
.
├── Dockerfile
├── README.md
├── docker-compose.yml
├── nest-cli.json
├── proto // proto files
├── src
│ ├── posts // User posts resource
│ ├── proto // TypeScript SDK generated from proto file
│ ├── users // Users resource
│ │ ├── users.controller.ts
│ │ ├── users.module.ts
│ │ ├── users.service.ts
│ ├── app.module.ts
│ ├── main.ts
├── tsconfig.build.json
├── tsconfig.json
Installation and Setup
Requirements
- docker & docker compose cli
- Node.js v18
Please note that I developed this project using an M1 Mac.
To get started, clone repository.
git clone git@github.com:Ko1103/nestjs-buf-example.git
Next, install the node_modules
npm ci
Generate TypeScript SDK from protobuf files
npm run proto:gen
Finally bring up docker container.
docker compose app
Try accessing gRPC via http:localhost/5000
.
Conclusion
With this repository, you now have the ability to create new gRPC projects with NestJS using Buf. However, I'm concerned that the TypeScript SDK generated into src/proto may not be optimal. Please feel free to share any alternative options or ideas you may have regarding this.
Thank you for reading until the end.
Top comments (0)