Full NodeJS boilerplate of a NestJS GraphQL monolithic backend API using PostgreSQL as the database.
In terms of languages this template takes a NestJS GraphQL Code First Approach, so it's fully written in TypeScript.
In terms of frameworks it uses:
- NestJS as the main NodeJS framework;
- Fastify as the HTTP and WS adapter;
- Mercurius as the GraphQL adapter for Fastify;
- MikroORM as the ORM for interacting with the database;
- Sharp for image manipulation and optimization.
Configuration (adds most used config classes):
- Cache with Redis;
- GraphQL with subscriptions and GraphQL through Websockets;
- MikroORM with SQLite in development and PostgreSQL in production.
Authentication:
- JWT Authentication (local OAuth) for HTTP;
- Custom Session Authentication for Websockets (based on Facebook Messenger Design);
- Two-Factor authentication with email.
Uploader:
- Basic image only uploader with Sharp optimizations for a generic S3 Bucket.
Pagination:
- Has the generics for Edges and Paginated types;
- Relay cursor pagination function.
In terms of folders each module is divided as follows:
NOTE: this is only a recommendation you can always use your own approach as long as they work with NestJS.
C:/home/user/Home/IdeProjects/{project-folder}/src/{module-name}:.
├─── entities
│ │ {something}.entity.ts
│ ├─── gql:
│ │ {something}.type.ts
├─── interfaces
│ {something}.interface.ts
├─── enums
│ {something}.enum.ts
├─── dtos
│ {something}.dto.ts
├─── inputs
│ {something}.input.ts
├─── tests
│ {something}.spec.ts
│ {module-name}.module.ts
│ {module-name}.service.ts
│ {module-name}.resolver.ts
- Where you save all your entities;
- The entities are the classes that represent the database tables and the Graphql Object Types;
- These are normally decorated with the @Entity decorator and the @ObjectType decorator;
- File Extension: {something}.entity.ts
- Gql (gql):
- Where you save the Generic Graphql Object Types (ex: PaginatedUsers, etc.);
- These are normally decorated with the @ObjectType decorator;
- File Extension: {something}.type.ts
- Where you save all the interfaces and TypeScript types;
- File Extension: {something}.interface.ts
- Where you save all general enums and Enum Type;
- File Extension: {something}.enum.ts
- Where you save all the DTOs (Data Transfer Objects) mainly for your Queries;
- These are normally decorated with the ArgsType decorator;
- File Extension: {something}.dto.ts
- Where you save all the GraphQL Input Objects mainly for your Mutations;
- These are normally decorated with the @InputType decorator;
- File Extension: {something}.input.ts
- Where you save all the unit tests for your module's services, resolvers and controllers;
- These are normally the spec files generated by the cli, but you still need to move them to this folder;
- File Extension: {something}.spec.ts
- Embeddables (embeddables):
- Where you save all your JSON Fields for your database tables;
- These are normally decorated with the @Embeddable decorator;
- File Extension: {something}.embeddable.ts
- Guards (guards):
- Where you save all your NestJS Guards related to your module;
- File Extension: {something}.guard.ts
$ yarn install
# creation
$ yarn migrate:create
# update
$ yarn migrate:update
# production mode
$ yarn start
# watch mode
$ yarn start:dev
# debug mode
$ yarn start:debug
- Create a repo using this template;
- Install the dependencies:
$ yarn install
- Create a .env file with all the fields equal to the example.
- Run the app in development mode:
$ yarn start:dev
- Check if NODE_ENV is not production;
- Remove the current test.db (if exits);
- Create a new test.db.
# remove test.db
$ rm test.db
# create a new test.db
$ yarn migrate:create
# unit tests
$ yarn run test
# unit tests
$ yarn run test service-name.service.spec.ts
- Go to DigitalOcean, Linode or Hetzner;
- Create a server running Ubuntu LTS;
- Install dokku;
- Run the following commands on your server for dokku initial set-up:
$ cat ~/.ssh/authorized_keys | dokku ssh-keys:add admin
$ dokku domains:set-global your-global-domain.com
- Create a new app and connect git:
$ dokku apps:create app-name
- Add the Postgres plugin to dokku, create a new PG instance and link it to the app:
$ sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git postgres
$ dokku postgres:create app-name-db
$ dokku postgres:link app-name-db app-name
- Add the Redis plugin to dokku, and create a new Redis instance and link it to the app:
$ sudo dokku plugin:install https://github.com/dokku/dokku-redis.git redis
$ dokku redis:create app-name-redis
$ dokku redis:link app-name-redis app-name
- Add all the other configurations as in the example file:
$ dokku config:set app-name URL=https://your-domain.com ...
- On the project folder on your local computer run the following commands:
$ git remote add dokku dokku@server-public-ip-address:app-name
$ git push dokku main:master
- Finally set up SSL and a domain for your app:
$ sudo dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git
$ dokku config:set --global DOKKU_LETSENCRYPT_EMAIL=your-email@your.domain.com
$ dokku domains:set app-name your-domain.com
$ dokku letsencrypt:enable app-name
$ dokku letsencrypt:cron-job --add
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
Mikro-ORM is a TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. If you like MikroORM, give it a star on GitHub and consider sponsoring its development!
Sharp is a high performance Node.js image processor. If you want to support them.
This template is MIT licensed.