Home

Awesome

<p align="center"> <a href="http://nestjs.com/" target="blank"><img src="https://i.imgur.com/4xiI9Hu.png" width="620" alt="Nest Logo" /></a> </p>

Blog made using Nestjs + Mikro-orm codebase(backend) containing real world examples (CRUD, auth (password based and oauth), advanced patterns, etc) and batteries included and ever-evolving

<p align="center"> <img alt="GitHub package.json version" src="https://img.shields.io/github/package-json/v/rubiin/ultimate-nest"> <img alt="Workflow test" src="https://github.com/rubiin/ultimate-nest/actions/workflows/github-ci.yml/badge.svg"> <img alt="GitHub" src="https://img.shields.io/github/license/rubiin/ultimate-nest"> <img alt="GitHub" src="https://img.shields.io/github/commit-activity/w/rubiin/ultimate-nest"> </p> <p align="center"> <a href="https://www.buymeacoffee.com/XbgWxt567" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-green.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a> </p> <br/>

NOTE: Starting April 18,2022 , the repo has ditched most promises for observables. You can check the latest promised version code at commit

More on why observables are better than promises can be read here

<br/>

Table of Contents

Whats included

Prerequisites

NodeJS https://nodejs.org/en/

Typescript https://www.typescriptlang.org/

PostgresQL https://www.postgresql.org/

Redis https://redis.io/

RabbitMQ https://www.rabbitmq.com

Getting started


# 1. Clone the repository or click on "Use this template" button.
npx degit rubiin/ultimate-nest my-nest-app

# 2. Enter your newly-cloned folder.
cd ultimate-nest

# 3. Create Environment variables file.
cp env/.env.sample env/.env.dev

# 4. Install dependencies (preferred: pnpm)
 pnpm install
 npm install
 yarn install

[!NOTE] If you face any issues with dependencies when using yarn or npm, stick to pnpm for now

[!NOTE] Env files are kept in env folder. The config validation allows 4 environment ['dev', 'prod', 'test','stage']. The env file name should be of format .env.[environment] Ex. (.env.dev). The env to use should be provided while running any script as NODE_ENV=dev npm run dev

Available Scripts

All the scripts require NODE_ENV flag

Additionally, you can also see the scripts in justfile which is a cross platform task runner. You can use it by installing just and then running just <script>. Ex. just build


Setup

Migration and seeding(optional)

Migrations are used to update the database schema. The migration files are stored in migrations directory.

NODE_ENV=dev npm run orm migration:up # applies migration for dev env

Seeding is used to insert data into the database. The seeding files are stored in common/database/seeders directory.

USER_PASSWORD=Test@1234 NODE_ENV=dev npm run orm seeder:run   # seeds data for dev env with all user password set as Test@1234

Seeding is optional

Start application

File structure

ultimate-nest
โ”œโ”€โ”€ env                                           * Contains all configuration files
โ”‚   โ””โ”€โ”€ .env.example                              * Sample configuration file.
โ”‚   โ””โ”€โ”€ .env.dev                                  * Configuration file for development environment.
โ”‚   โ””โ”€โ”€ .env.prod                                 * Configuration file for production environment.
โ”‚   โ””โ”€โ”€ .env.test                                 * Configuration file for test environment.
โ”œโ”€โ”€ coverage                                      * Coverage reports after running `npm run test:cov` command.
โ”œโ”€โ”€ dist                                          * Optimized code for production after `npm run build` is run.
โ”œโ”€โ”€ src
    โ””โ”€โ”€ modules                                   * Folder where specific modules all files are stored
          โ””โ”€โ”€ <module>
      โ”‚       โ””โ”€โ”€ dto                             * Data Transfer Objects.
      โ”‚       โ””โ”€โ”€ <module>.controller.ts          * Controller file.
      โ”‚       โ””โ”€โ”€ <module>.module.ts              * root module file for module.
      โ”‚       โ””โ”€โ”€ <module>.service.ts             * Service file for <module>.
      โ”‚       โ””โ”€โ”€ <module>.service.spec.ts        * Test file for service.
      โ”‚       โ””โ”€โ”€ <module>.repository.ts          * Repository file for <module>.
      โ”‚       โ””โ”€โ”€ <module>.repository.spec.ts     * Test file for repository.
โ”‚   โ””โ”€โ”€ common                                    * Common helpers function, dto, entity,guards, custom validators,types, exception, decorators etc.
โ”‚   โ””โ”€โ”€ __mocks__                                 * Fixtures for unit tests.
โ”‚   โ””โ”€โ”€ libs                                      * Resusable pre configured libraries
โ”‚   โ””โ”€โ”€ resources                                 * Contains all static resources like ssl, i18n,email templates etc.
โ”‚   โ””โ”€โ”€ app.module.ts                             * Root module of the application.
โ”‚   โ””โ”€โ”€ main.ts                                   * The entry file of the application which uses the core function NestFactory to create a Nest application instance.
โ”œโ”€โ”€ test                                          * End to end test files for the application.

Authentication

This applications uses JSON Web Token (JWT) to handle authentication. The token is passed with each request using the Authorization header with Token scheme. The JWT authentication middleware handles the validation and authentication of the token.

Deployment

You need to have docker and docker-compose installed. The image environment variable values can be found at the compose file

ENV=dev sh ./scripts/deploy.sh   # deploys dev environment (.env.dev used)
ENV=prod sh ./scripts/deploy.sh   # deploys prod environment (.env.prod used)

More docs found at docs folder

<h2 align="center">Do you use this template?<br/>Don't be shy to give it a star! โ˜…</h2>

Support

<a href="https://www.buymeacoffee.com/XbgWxt567"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=XbgWxt567&button_colour=5F7FFF&font_colour=ffffff&font_family=Cookie&outline_colour=000000&coffee_colour=FFDD00" /></a>

Also if you are into NestJS ecosystem you may be interested in one of my other libs:

helper-fns

GitHub stars npm

A collection of helper functions for typescrip development. It includes functions for array,object,string,etc

nestjs-easyconfig

GitHub stars npm

Platform config manager for nestjs. It supports multiple config files and environment variables.


nestjs-minio

GitHub stars npm

This is a minio module for Nest.


nestjs-cloudinary

GitHub stars npm

This is a cloudinary module for Nest.


nestjs-pgpromise

GitHub stars npm

A Module for Utilizing Pg-promise with NestJS


Star History

<a href="https://star-history.com/#rubiin/ultimate-nest&Timeline"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=rubiin/ultimate-nest&type=Timeline&theme=dark" /> <source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=rubiin/ultimate-nest&type=Timeline" /> <img alt="Star History Chart" src="https://api.star-history.com/svg?repos=rubiin/ultimate-nest&type=Timeline" /> </picture> </a>

Made with โค๏ธ with opensource.