Giter Site home page Giter Site logo

darko-super-nest's Introduction

Nest Logo

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

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](https://github.com/rubiin/ultimate-nest/tree/fb06b34f7d36f36195880e600f8f1b5b86f71213)

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

Clone this repo. Edit the env file and pass in your credentials

Installation

Install dependencies (preferred: pnpm)

 npm install --legacy-peer-deps
 yarn install
 pnpm install --shamefully-hoist=true

Database

Mikro Orm

The example codebase uses MikroORM with a Postgres database.

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 npm run dev

Start local Postgres server and run NODE_ENV=dev make migrate to apply migrations

Now you can start the application witt NODE_ENV=dev npm run start.

Note: If you are using windows, SET NODE_ENV=dev npm run start


Features covered:

  • ๐ŸŒ I18n - Internationalization
  • ๐Ÿงต Stats - swagger stats for common server metrics
  • ๐Ÿงต Poolifier - threads for cpu extensive tasks
  • ๐Ÿ’ฌ Twillio - 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

NPM scripts

  • npm run start - Start application
  • npm run start:dev - Start application in watch mode
  • npm run test - run Jest test runner
  • npm run start:prod - Build application

Additionally, you can also see the scripts in makefile


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.

  env=dev make migrate # applies migration for dev env

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

  env=dev make seed   # seeds data for dev env with user password set as Test@1234

Start application

  • NODE_ENV=[env name] npm run start (without ssl)
  • NODE_ENV=[env name] SSL=true npm run start (with ssl)
  • Test api by browsing to http://localhost:[port]/v1/user
  • 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 make deploy    # deploys dev environment (.env.dev used)
  env=prod make deploy   # 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

sample env

The sample env is generated using sample-env

More docs found at docs folder

darko-super-nest's People

Contributors

kolevdarko avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.