This repository presents a backend boilerplate based on NestJS and Hasura with some features. Of course there are many templates you can use, but this boilerplate allows developers to construct a backend system with light and intuitive skills. Therefore, it may not have the highest level of skills, but it may be an easier way to experience backend systems.
In this boilerplate, a backend system will consist of NestJS, Hasura, and Postgres. You can find out descriptions about them in overview.
You can start, implement, and deploy your backend system easily with this boilerplate.
This boilerplate aims to:
- Productivity : Start new projects quickly and configure your environments
- Education : Adaption of NestJS and Hasura usages
- Utilization : Testing for new code or library easily
Among them, we are most focused on productivity. For productivity, you will construct the following backend system:
-
Postgres, NestJS, and Hasura are running on Docker container that they are started by simple shell scripts with some commands.
-
All client requests are made through Hasura.
-
GraphQL queries are mainly used in client data requests that are automatically generated by Hasura.
-
All permissions for data requests are set in Hasura.
-
Business logic functions are implemented in NestJS server, and registered as Hasura Actions.
-
Permissions for business logic functions are double set in Hasura and NestJS server.
Moreover, this NestJS boilerplate has monolithic architecture to aim for productivity.
In various backend architectures, DDD (Domain-driven design) or micro-service nature may be more useful. However, we aimed to incorporate the advantages of Hasura into NestJS. Therefore, we tried to increase the productivity of the backend by focusing on implementing Action Handler (Hasura feature) in NestJS.
This summary is like a kind of technical stack covered by this boilerplate.
Key Point | Use / Implementation / Connection |
---|---|
Framework | NestJS |
Language | TypeScript |
Package Manager | yarn |
Architecture | Monolith, CQRS |
Documentation | Swagger |
ORM | TypeORM |
Database | Postgres |
GraphQL Engine | Hasura |
Interaction | Hasura Query & Actions |
Deployment | Dockerlized |
- Architecture from CQRS Pattern (focusing on Hasura Action Handler)
- Focusing on Code Sharing for Collaboration
- Swagger Documentation
- Health Checker & Throttler
- Cache on Database
- Custom Logging System
- JWT Authentication
- User/Auth Examples
- Docker Versioning and Deployment
If you already have Docker engine and
docker-compose
command, skip this step.
We need docker
and docker-compose
commands to run the servers. The servers are running on docker containers by our shell scripts with the commands.
You can install docker as desktop-app or engine. We share some commands to install docker in prerequisites document. After docker installation, docker-compose
command may be installed together. If not, use the command in the document to install.
Clone this repository:
$ git clone https://github.com/hubts/nestjs-easyfull-boilerplate.git
You should prepare settings to start Postgres, Hasura, and NestJS server. The settings are set in .env
environment variable files. You can find an example of those files in each directory of them.
In this boilerplate, JWT authentication is applied to control user accesses. NestJS server will issue access tokens, and Hasura server will verify the tokens at the forefront of client requests. Therefore, the public key of JWT must be registered in Hasura. Use this command to generate a new JWT key pair:
$ ./script/util/jwt-key-generation.sh
A new RSA key pair will be printed in your console. Copy them to set in Hasura and NestJS servers.
Follow steps of document in Postgres directory. Since the run of Postgres does not have much difficulty, it simply needs to be completed to run a database.
Follow steps of document in Hasura directory. Note that JWT public key generated in settings must be set in Hasura setting.
See .env.example
file in root directory, and copy it as .env
file to set environment variables.
ENV
: Server environment to handle services (e.g. logging).PORT
: Server listening port.EXTERNAL_ENDPOINT
: Server external endpoint used in swagger OpenAPI.DB_*
: Database settings to connect with the running database.THROTTLER_*
: Throttler options.JWT_*
: JWT options.
$ yarn # or 'yarn install'
# Start
$ yarn start
# Start with watch mode (to debug)
$ yarn start:dev
$ yarn deploy