Skip to content

Project of example to expose resources using typescript and grpc framework.

Notifications You must be signed in to change notification settings

stivenson/example-typescript-grpc-api

Repository files navigation

Example Typescript Grpc API

Build Status

Project of example to expose resources using typescript and grpc framework.

Technologies used

--
- 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.)

Requirements

  • nodejs v12.16+
  • npm 6.13.x+
  • postgresql v11.5+ running
  • git

Installing the Protobuf Compiler

  • To macOs: instructions
  • To Ubuntu: install package protobuf-compiler

Commands to check requirements

  • node --version
  • npm --version
  • postgres --version (or similar depending Operating System, O.S.)
  • git --version
  • protoc --version (Protobuf Compiler)

Folder descriptions

  • 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.

Getting Started

1. Install dependencies

  • Run: npm i

2. Generate interfaces (sdks) that you will use to send and receive resources

  • 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)

  • And after, check if these files were generated into ./src/protos/v1 folder:
    Image of folder with sdk proto files
Important: This second step is necessary every time you change any file with a proto extension (contracts). 

3. Config Database connection and database called example_grpc

> Create the database using the preferred editor or executing this line by command line:

  • createdb -h localhost -p 5432 -U <some postgres user> example_grpc (replace <some postgres user>)

> Replace connection values in .envrc file (clone .envrc_example file to create this file)

> And run source .envrc to load this new environment values.

4. Run project

  • source .envrc && npm start

5. Run tests in other console

  • source .envrc && npm run test

Considerations

  1. This example was create using macOs Catalina O.S. (but the steps should not vary much on other systems)
  2. The installation steps were also tested in lubuntu on a virtual machine.

External links to more info

About

Project of example to expose resources using typescript and grpc framework.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published