shawon1fb / movie_house

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Contributors Forks Stargazers Issues MIT License

NestJs NodeJs Typescript MongoDB JWT Jest Yarn Docker

ACK NestJs Boilerplate Mongoose πŸ”₯ πŸš€

ack-nestjs-mongoose is a NestJs Boilerplate with Mongoose and MongoDB as Database.

Made with following

You can Request Feature or Report Bug with following this link

Important

ack-nestjs-mongoose still on trial and error phase and the test will base on real projects or cases. So there will be (always) have new update and new features.

If you change env value of APP_MODE to secure that will trigger more Middleware and Guard.

  1. TimestampMiddleware, tolerant 5 minutes of request.
  2. UserAgentMiddleware, whitelist of user agent.
  3. ApiKeyGuard, check api key based on database.

If you change env value of APP_ENV to production that will

  1. Do not write into console Database debug even when DATABASE_DEBUG is true
  2. Do not write into console for DebuggerService even when APP_DEBUG is true
  3. CorsMiddleware on.

You can see our e2e testing file or read the documentation on [section environment][ack-doc-env].

Build with

Describes which version .

Name Version
NestJs v9.x
NodeJs v18.x
Typescript v4.x
Mongoose v6.x
MongoDB v6.x
Yarn v1.x
NPM v8.x
Docker v20.x
Docker Compose v2.x

Objective

ack-nestjs-mongoose have some objective.

  • Simple, scalable and secure
  • Avoid spaghetti code
  • Component based
  • Reusable component
  • Easy to maintenance
  • Support for all microservice patterns

Features

  • NestJs v9.x πŸ₯³
  • Production Ready πŸ”₯
  • Typescript πŸš€
  • Authentication and Authorization (JWT, OAuth2, API Key, Basic Auth, Role Management) πŸ’ͺ
  • User Agent Check
  • MongoDB Integrate by Using Mongoose Package πŸŽ‰
  • Database Migration (NestJs-Command)
  • Storage with AWS (S3)
  • Server Side Pagination (3 Types)
  • Url Versioning
  • Request Validation Pipe with Custom Message πŸ›‘
  • Custom Error Status Code 🀫
  • Logger (Morgan) and Debugger (Winston) πŸ“
  • Centralize Configuration πŸ€–
  • Centralize Exception Filter, and Custom Error Structure
  • Multi-language (i18n) πŸ—£
  • Timezone Awareness, and Custom Timezone
  • Request Timeout, and Request Custom Timeout (Override) βŒ›οΈ
  • Dynamic Setting from Database πŸ—Ώ
  • Maintenance Mode on / off 🐀
  • Server Side Pagination
  • Cache Manager Implementation
  • Support Docker Installation
  • Support CI/CD with Github Action or Jenkins
  • Husky GitHook For Check Source Code, and Run Test Before Commit 🐢
  • Linter with EsLint for Typescript

Prerequisites

We assume that everyone who comes here is programmer with intermediate knowledge and we also need to understand more before we begin in order to reduce the knowledge gap.

  1. Understand NestJs Fundamental, Main Framework. NodeJs Framework with support fully TypeScript.
  2. UnderstandTypescript Fundamental, Programming Language. It will help us to write and read the code.
  3. Understand ExpressJs Fundamental, NodeJs Base Framework. It will help us in understanding how the NestJs Framework works.
  4. Understand what NoSql is and how it works as a database, especially MongoDB.
  5. Optional, Understand Microservice Architecture and the design pattern.
  6. Optional,The Twelve Factor Apps
  7. Optional, Understand Docker that can help you to run the project

Todo

Next development

  • Import data form excel
  • Version 2. New folder structure, new file upload decorator
  • Reduce mixin usage
  • Upload file multiple update
  • File large upload into s3 for video and audio. using array of chunk
  • Rename repo from ack-nestjs-boilerplate-mongoose to ack-nestjs-mongoose
  • Message en,id
  • Optimize code, remove unnecessary code
  • Update Unit test
  • Update E2E test
  • Response Excel, convert response from controller into excel res.send()
  • Implement Repository Design Pattern
  • Swagger for API Documentation
  • Basic Token as ApiKey
  • Update Documentation

Documentation

Before we start, we need to install some packages and tools. Recommend version is LTS Version for every tool and package.

Make sure check that tools has been installed successfully.

Getting Started

Clone Repo

Clone ack-nestjs-mongoose with git.

git clone https://github.com/andrechristikan/ack-nestjs-mongoose

Install Dependencies

This project need some dependencies. Let's go install it.

# yarn
yarn install

Create environment

Make your own environment with copy from .env.example and edit some value.

cp .env.example .env

Jump to details

Database Migration

If you want to to implement transaction, you must to install Mongodb Replication Set. You need to run mongodb. There are have so many options, you can do by your self with search on google.

Database migration ack-nestjs-mongoose used NestJs-Command

For migrate

yarn migrate

For rollback

yarn rollback

Test

ack-nestjs-mongoose provide 3 automation testing unit testing, integration testing, and e2e testing.

yarn test

Specific test

For unit testing

yarn test:unit

For integration testing

yarn test:integration

For E2E testing

yarn test:e2e

Run Project

If mongodb version < 5, Read this section for adjust mongoose setting.

Finally, Cheers 🍻🍻 !!! we passed all steps.

Now we can run ack-nestjs-mongoose and use all of features.

yarn start:dev

Run Project with Docker

docker-compose up -d

Environment

Detail information about the environment

APP Environment

Key Type Description
APP_NAME string Application name and will be subject for jwt
APP_ENV string
  • production
  • development
APP_MODE string
  • secure
  • simple
APP_LANGUAGE string Enum languages, separator ,
APP_TZ string Override timezone
APP_HOST string Application serve
APP_PORT number Application serve
APP_DEBUG boolean All logs will write into console
APP_VERSIONING boolean Application url versioning
APP_VERSION `number string`
APP_HTTP_ON boolean Application Http turn on
APP_JOB_ON boolean Application Job turn on

Database Environment

Key Type Description
DATABASE_HOST string Mongodb URL. Support standard url replication, and srv
DATABASE_NAME string Database name
DATABASE_USER string Database user
DATABASE_PASSWORD string Database user password
DATABASE_DEBUG boolean Trigger database mongoose DEBUG
DATABASE_OPTIONS string Mongodb connect options

Middleware Environment

Key Type Description
MIDDLEWARE_TOLERANCE_TIMESTAMP string Tolerance timestamp ApiKey. ms package value
MIDDLEWARE_TIMEOUT string Request timeout. ms package value

Auth Environment

Key Type Description
AUTH_JWT_AUDIENCE string Jwt audience
AUTH_JWT_ISSUER string JWT issuer
AUTH_JWT_ACCESS_TOKEN_SECRET_KEY string Secret access token, free text.
AUTH_JWT_ACCESS_TOKEN_EXPIRED string Expiration time for access token. ms package value
AUTH_JWT_REFRESH_TOKEN_SECRET_KEY string Secret refresh token, free text.
AUTH_JWT_REFRESH_TOKEN_EXPIRED string Expiration time for refresh token. ms package value
AUTH_JWT_REFRESH_TOKEN_REMEMBER_ME_EXPIRED string Expiration time for refresh token when remember me is checked. ms package value
AUTH_JWT_REFRESH_TOKEN_NOT_BEFORE_EXPIRATION string Token active for refresh token before x time. ms package value

Basic Environment

Will implement as ApiKey

Key Type Description
AUTH_BASIC_TOKEN_CLIENT_ID string Free text
AUTH_BASIC_TOKEN_CLIENT_SECRET string Free tex

AWS Environment

Key Type Description
AWS_CREDENTIAL_KEY string AWS account credential key
AWS_CREDENTIAL_SECRET string AWS account credential secret
AWS_S3_REGION string AWS S3 Region
AWS_S3_BUCKET string AWS S3 Name of Bucket

Adjust Mongoose Setting

Just is case, if your mongodb version is < 5

Go to file src/common/database/services/database.options.service.ts and remove comment useMongoClient then set value to true.

const mongooseOptions: MongooseModuleOptions = {
    uri,
    useNewUrlParser: true,
    useUnifiedTopology: true,
    serverSelectionTimeoutMS: 5000,
    useMongoClient: true
};

License

Distributed under MIT licensed.

Contact

Andre Christi kan

Github LinkedIn Instagram

About

License:MIT License


Languages

Language:TypeScript 99.9%Language:Shell 0.1%