npx generate-nest-boilerplate {your-app}
- Install dependencies by running
yarn install
- Create .env file by running
cp .env.example .env
and replace existing env variables
You can create .env.development or .env.staging or .env.production file depend on your environment
You have to install Mysql
, Redis
and replace respective env variables in env file
yarn db:setup:local
For development environment
yarn migration:run
yarn start:dev
For production environment
yarn migration:run
yarn start:prod
By default, the app will be run port 8000
📦{your-app}
┣ 📂src
┃ ┣ 📂base
┃ ┃ ┣ 📂dtos
┃ ┃ ┗ 📂entities
┃ ┣ 📂common
┃ ┃ ┣ 📂constants
┃ ┃ ┣ 📂decorators
┃ ┃ ┣ 📂dtos
┃ ┃ ┣ 📂entities
┃ ┃ ┗ 📂exceptions
┃ ┣ 📂configs
┃ ┃ ┣ 📂app
┃ ┃ ┣ 📂database
┃ ┃ ┣ 📂queue
┃ ┃ ┗ 📜config.module.ts
┃ ┣ 📂databases
┃ ┃ ┣ 📂factories
┃ ┃ ┣ 📂migrations
┃ ┃ ┗ 📂seeds
┃ ┣ 📂guards
┃ ┣ 📂jobs
┃ ┣ 📂mail
┃ ┣ 📂modules
┃ ┃ ┗ 📂{module-name}
┃ ┃ ┃ ┣ 📂commands
┃ ┃ ┃ ┃ ┣ 📜{command-name}.admin.command.ts
┃ ┃ ┃ ┃ ┣ 📜{command-name}.command.ts
┃ ┃ ┃ ┃ ┗ 📜{command-name}.local.command.ts
┃ ┃ ┃ ┣ 📂controllers
┃ ┃ ┃ ┃ ┣ 📜{module-name}.admin.controller.ts
┃ ┃ ┃ ┃ ┗ 📜{module-name}.controller.ts
┃ ┃ ┃ ┣ 📂dtos
┃ ┃ ┃ ┃ ┣ 📜{dto-name}.admin.dto.ts
┃ ┃ ┃ ┃ ┣ 📜{dto-name}.dto.ts
┃ ┃ ┃ ┃ ┗ 📜{dto-name}.local.dto.ts
┃ ┃ ┃ ┣ 📂entities
┃ ┃ ┃ ┣ 📂interfaces
┃ ┃ ┃ ┣ 📂queries
┃ ┃ ┃ ┃ ┣ 📜{command-name}.admin.query.ts
┃ ┃ ┃ ┃ ┣ 📜{command-name}.query.ts
┃ ┃ ┃ ┃ ┗ 📜{command-name}.local.query.ts
┃ ┃ ┃ ┣ 📂repositories
┃ ┃ ┃ ┣ 📂services
┃ ┃ ┃ ┃ ┣ 📜{module-name}.admin.service.ts
┃ ┃ ┃ ┃ ┗ 📜{module-name}.service.ts
┃ ┃ ┃ ┗ 📜{module-name}.module.ts
┃ ┣ 📂utils
┃ ┣ 📂views
┃ ┣ 📜app.controller.ts
┃ ┣ 📜app.module.ts
┃ ┣ 📜app.service.ts
┃ ┗ 📜main.ts
┣ 📜.env.example
┣ 📜docker-compose.yml
┣ 📜ormconfig.js
┣ 📜package.json
┗ 📜tsconfig.json
Naming rules:
{file-name}.{file-type}.ts
: file only foruser
permission or lower{file-name}.admin.{file-type}.ts
: file only foradmin
permission or higher{file-name}.local.{file-type}.ts
: file only use in logic code, not use inservice
orcontroller
folder
DTO files is divided into 2 types: input dto
and output dto
- Input dto file example:
create-user.dto.ts
- Output dto file example:
user.dto.ts
In most cases, structure model --> repository --> service --> controller
is sufficient. However, when our requirements become more complex, the CQRS
pattern may be more appropriate and scalable.
You can defined commands and queries in commands
and queries
folder in each module.
The boilerplate has been installed passport
and jwt
.
jwt token
is installed in cookies
that auto be sent with each request.
It can be enabled by adding JwtAuthGuard
to necessary routes in controller files.
@UseGuards(JwtAuthGuard)
The JwtAuthGuard
uses combination of Redis
and Mysql
database to optimize the speed of the app
To enable the permission guard, add PermissionGuard
to necessary routes in controller files.
@UseGuards(PermissionGuard)
Some permissions have been installed. Visit file src/common/constants/permission.const.ts
to view detail.
Role {
SUPER_ADMIN = 'super-admin',
ADMIN = 'admin',
USER = 'user',
}
User {
CREATE = 'user:create',
READ = 'user:read',
UPDATE = 'user:update',
DELETE = 'user:delete',
}
To enabled this guard, add this code @UseGuards(UserStatusGuard)
User account has 3 status:
PENDING
: User register new account and account has not been activated by emailACTIVE
: Account has been activated by activation emailBLOCKED
: Account has been blocked by admin
Only ACTIVE
account can login to the app.
-
- Login:
/auth/login
- Signup:
/auth/signup
- Logout:
/auth/logout
- Login:
-
API prefix:
/auth/devices
- Get all device information which is logined
- Get current device infomation
- Logout all device
- Logout one device
-
API prefix:
/users
and/admin/users
- CRUD
user
byadmin
,super_admin
- CRUD
admin
bysuper_admin
- CRUD
-
API prefix:
/auth/reset-password
- By current password
- By email verification
- By google authenticator (incoming)
-
- Send mail to activate account
- Send mail to reset password
Visit
src/mail/commands
for details
Create new migration by running
yarn migration:generate {name-of-migration}
The new migration will be created in src/databases/migrations
.
yarn migration:run
yarn migration:revert
- Convert
entity
todto
to remove unnecessary properties in returned data such aspassword
. The methodtoDto
is installed for each entity. It can be used like that
user.toDto()
- Convert
dto
toresponse
to format the response that return to client. All the response will be format like that
{
data: ...
status: 200,
message: "Successfully"
}
The method toResponse
is installed for each entity. It can be used like that
user.toResponse(HttpStatus.CREATED, 'Create user successfully')
With the response has not return data. You can use method generateEmptyRes
generateEmptyRes(HttpStatus.OK, "Update user successfully");
All exceptions will be catched and returned with format
{
status: 403,
timestamp: "Sun, 01 Aug 2021 04:35:40 GMT",
message: "Forbidden resource",
path:"/users",
}
Rate limiting is configured by @nestjs/throttler
.
By default, limit 100 requests per 60s
Visit app.module.ts
to change this.
All APIs are described in Swagger. To see all available endpoints visit http://localhost:8000/api/static/index.html
yarn compodoc
By default, you can see the compodoc on http://localhost:8080
eslint
+ prettier
= ❤️
The MIT License. Please see License File for more information. Copyright © 2021 Tran Duc Minh.
Made with ❤️ by Tran Duc Minh