• Stars
    star
    229
  • Rank 169,049 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Starter template with blog clone as project

Nest Logo

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

GitHub package.json version Workflow test GitHub Lines of code

Buy Me A Coffee


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


Table of Contents

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.example env/.env.dev

# 4. Install dependencies (preferred: pnpm)

 npm install
 pnpm install
 yarn install

Database

Mikro Orm

The example codebase uses MikroORM with a Postgres database. Why Mikroorm? It is a modern ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. It is fully compatible with TypeScript and provides additional features like support for enums, custom types, MongoDB, transactions, caching, migrations, change tracking, advanced queries, lazy/eager relations and much more.

Copy sample env file and adjust the connection settings and other settings(jwt,redis,mail,etc) respectively on sample env file

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 yarn dev

Start local Postgres server and run npx cross-env NODE_ENV=dev make migrate to apply migrations

Now you can start the application witt npx cross-env NODE_ENV=dev yarn start.


Features covered:

  • 🌐 I18n - Internationalization
  • 🧡 Stats - swagger stats for common server metrics
  • 🧡 Poolifier - threads for cpu extensive tasks
  • πŸ’¬ Twilio - sms support
  • πŸ“± NestJS β€” latest version
  • πŸŽ‰ TypeScript - Type checking
  • βš™οΈ Dotenv - Supports environment variables
  • πŸ— Authentication - JWT, RSA256, oauth
  • 🏬 Authorization - RBAC with casl
  • πŸͺ MikroORM - Database ORM
  • πŸͺ PostgreSQL - Open-Source Relational Database
  • 🧠 Configuration - Single config for all
  • πŸ“ƒ Swagger - API Documentation
  • 🐳 Docker Compose - Container Orchestration
  • πŸ” Helmet - secure HTTP headers
  • 😴 Insomnia - Insomnia config for endpoints
  • πŸ“ ESLint β€” Pluggable JavaScript linter
  • πŸ’– Prettier - Opinionated Code Formatter

Available Scripts

  • yarn start - Start application
  • yarn start:dev - Start application in watch mode
  • yarn start:prod - Start built application
  • yarn start:hmr - Start application with hot module replacement
  • yarn format - Formats all the files inside src using prettier with config provided in .prettierrc
  • yarn lint - Uses eslint to lint all the files inside src with config provided in .eslintrc.cjs
  • yarn orm migration:create - Uses Mikroorm to create a migration file
  • yarn orm migration:up - This command is used to run availablexisting migration files.
  • yarn orm migration:down - This command is used to rollback migration.
  • yarn orm seeder:run - This command is used to run existing seeders in src/common/database.

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>


Setup

  • First if you don't want to use any libs from like redis, mailer etc. replace them from the app.module.tasks
    • You will also need to remove the config from validate.config.ts from line load: []
    • Also remove the unwanted config variables from the env file
  • Make sure you create a env file under env directory with name like .env.something.The portion after .env is the NODE_ENV value which will be required while running the app
  • Also make sure you have ssl files inside src/resources/ssl if you tend to use ssl. Replace the sample files with your ssl files but keep the name same. Additionally

Migration and seeding

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

  npx cross-env NODE_ENV=dev yarn 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.

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

Start application

  • npx cross-env NODE_ENV=[env name] yarn start
  • View automatically generated swagger api docs by browsing to http://localhost:[port]/docs
  • View automatically generated swagger stats dashboard by browsing to http://localhost:[port]/stats. The username and password is the values set in the env file under SWAGGER_USERNAME and SWAGGER_PASS respectively

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 `yarn test:cov` command.
β”œβ”€β”€ dist                                          * Optimized code for production after `yarn 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 (not the compose plugin) installed. Also since we are using makefiles for deployment, you need to have make installed.

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

The password for redis and rabbitmq is Test@1234 can be changed in the make file under deploy script

More docs found at docs folder

Do you use this template?
Don't be shy to give it a star! β˜…

Support

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

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


Made with ❀️ with opensource.

More Repositories

1

nestjs-easyconfig

Manage nestjs configs on the go πŸ”₯
TypeScript
183
star
2

nestjs-minio

Your favorite object storage with nestjs
TypeScript
58
star
3

jenkins

Custom jenkins images with docker-compose,docker and all the plugins pre-installed
Dockerfile
27
star
4

nestjs-pgpromise

A Module for Utilizing Pg-promise with NestJS
TypeScript
25
star
5

gitignorer

Cli app to quicky add gitignore files to your current environment
Go
11
star
6

url-minify

Js library to shorten urls
TypeScript
8
star
7

nestjs-cloudinary

Cloudinary module with sharp package for transformations
TypeScript
7
star
8

rubiin

Personal Readme
6
star
9

sample-env

Generate sample file from .env for your apps
JavaScript
5
star
10

vscode-nestjs-snippets

Snippets for Nestjs 😻
5
star
11

projecto

Opens your projects in your specified editors
Go
4
star
12

helper-fns

Handy js utilities
TypeScript
4
star
13

vitely

Vue 3 template with vite
Vue
4
star
14

react

vite with react
TypeScript
3
star
15

starter-ts

Library boilerplate for making new JS LIbraries
TypeScript
2
star
16

nest-swagger-bug

TypeScript
2
star
17

azlyrics-extractor

A simple app purely written in java that extracts lyrics of a song from azlyrics.com .
Java
2
star
18

tinynode

This node image contains an additional tool inside
Dockerfile
2
star
19

memoize

A simple and easy to use memoize lib for deno
TypeScript
1
star
20

infras

Kong + postgres <3
JavaScript
1
star
21

jazz-music-player

free music player
Java
1
star
22

deno-env

DotEnv but for deno
TypeScript
1
star
23

Ar-SpaceShip

An Ar implementation of Spaceeship game.
C#
1
star
24

Lappy-Ball

Arcade game where you have to complete laps to score.
C#
1
star
25

nfdl

This script automatically install your favorite nerd fonts right from the terminal
Shell
1
star
26

unprofane

Profanity filter library
TypeScript
1
star
27

netflix-clone-react

Netflix clone with plain react and tailwind
TypeScript
1
star
28

MarkIT

A simple mark down web app powered by js
JavaScript
1
star
29

Dev.to-URL-Shortener

Dev.to tutorial code
JavaScript
1
star
30

MarkIT-App

A simple markdown app made on electron
HTML
1
star
31

validator

Port of validatorjs to deno
TypeScript
1
star
32

jenkins-with-docker

JavaScript
1
star
33

wallpapers

Wallpapers that I collected from various sources
1
star
34

lyricsgrab

Cli to grab your favorite song lyrics from azlyrics
Go
1
star
35

SmartPad

SmartPad is a simple yet full featured texteditor written in java.
Java
1
star
36

kube-express

Kubernetes with express
Dockerfile
1
star
37

vimwordlist.nvim

Lua
1
star