ack-nestjs-boilerplate
ack-nestjs-boilerplate copied to clipboard
NestJs v10.x Boilerplate. Repository Design Pattern. MongoDB (Mongoose). Containerization.
ACK NestJs Boilerplate Mongoose 🔥 🚀
Standard architecture for nestjs
ack-nestjs-mongoose is a NestJs Boilerplate with Mongoose and MongoDB as Database.
Made with following
- nodejs-best-practice
- The Twelve-Factor App
- NestJs Habit.
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.
TimestampMiddleware, tolerant 5 minutes of request.UserAgentMiddleware, whitelist of user agent.ApiKeyGuard, check api key based on database.CorsMiddleware, check cors based on configs.
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.
- Understand NestJs Fundamental, Main Framework. NodeJs Framework with support fully TypeScript.
- UnderstandTypescript Fundamental, Programming Language. It will help us to write and read the code.
- Understand ExpressJs Fundamental, NodeJs Base Framework. It will help us in understanding how the NestJs Framework works.
- Understand what NoSql is and how it works as a database, especially MongoDB.
- Optional, Understand Microservice Architecture and the design pattern.
- Optional,The Twelve Factor Apps
- Optional, Understand Docker that can help you to run the project
Todo
Next development
- [x] Import data form excel
- [x] Version 2. New folder structure, new file upload decorator
- [x] Reduce mixin usage
- [x] Upload file multiple update
- [x] File large upload into s3 for video and audio. using array of chunk
- [x] Rename repo from
ack-nestjs-boilerplate-mongoosetoack-nestjs-mongoose - [x] Message en,id
- [x] Optimize code, remove unnecessary code
- [x] Update Unit test
- [x] Update E2E test
- [x] Response Excel, convert response from controller into excel
res.send() - [ ] 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 installMongodb Replication Set. You need to runmongodb. There are have so many options, you can do by your self with search on
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 |
| APP_ENV | string |
|
| APP_MODE | string |
|
| 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_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_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