Project of example to expose resources using typescript and grpc framework.
-- |
---|
- Typescript (Typed superset of JavaScript that compiles to plain JavaScript.) |
- Grpc (A high-performance, open source universal RPC framework.) |
- Decorators and sequelize (v5) for an orm implementation to postgres database. |
- Generic typescript for an implementation of the repository pattern in logic. |
- Definition of contracts in a .proto file: GRPC provides protocol buffer compiler plugins that generate client- and server-side code (similar to sdks). gRPC users typically call these APIs on the client side and implement the corresponding API on the server side. |
- Jest (Implementation of tests.) |
- nodejs v12.16+
- npm 6.13.x+
- postgresql v11.5+ running
- git
- To macOs: instructions
- To Ubuntu: install package
protobuf-compiler
node --version
npm --version
postgres --version
(or similar depending Operating System, O.S.)git --version
protoc --version
(Protobuf Compiler)
- src/models: Models definitions (interaction with persistence through ORM).
- src/postgresql: Postgres config.
- src/protos/v1: This folder content the Contracts (using Protocol Buffers as IDL) and SDK files generated.
- src/repositories: Layern with implementation of repository pattern.
- src/implementations: Layer with union between repository, utils and implementation for sending and receiving resources using framework grpc.
- Run:
npm i
- Run:
cd ./src/protos/v1
- Run:
protoc --plugin="protoc-gen-ts=../../../node_modules/.bin/protoc-gen-ts" --plugin=protoc-gen-grpc=../../../node_modules/.bin/protoc-gen-ts --js_out="import_style=commonjs,binary:." --ts_out=service=grpc-node:"." examplegrpcApi.proto && cd ../../../
This process to generate sdks is necessary per proto file (.proto) into src/protos/v1 folder)
Important: This second step is necessary every time you change any file with a proto extension (contracts).
createdb -h localhost -p 5432 -U <some postgres user> example_grpc
(replace<some postgres user>
)
source .envrc && npm start
source .envrc && npm run test
- This example was create using macOs Catalina O.S. (but the steps should not vary much on other systems)
- The installation steps were also tested in lubuntu on a virtual machine.