This project aims to provide an API project boilerplate based on nestjs, which already includes or will soon include the following basic functions:
- Database
- Migrations
- API
- Authentication
- Passport
- Local strategy
- LDAP strategy
- JWT strategy
- Passport
- Authorization
- Basic RBAC (Role-based access control)
- Claims-based authorization (TBD)
- Pagination and filtering
- Health check
- orm
- memory
- disk
- http
- Rate limiting
- Authentication
- Task Scheduling
- Testing
- Unit testing framework
- E2E testing framework
$ npm install
There are three environment variable files pre-set in this project template, and their priority is as follows:
.env.local > .env.development > .env
If you want to modify the default environment variable priority or define a new environment variable priority, you can modify the src/config/env.ts
file. To create an environment variable file, you can copy it from the .env.example
file:
$ cp .env.example .env
You can modify the configuration as needed.
In order to quickly start working in the development environment, here are some services that the development environment needs to use, which will run in Docker containers.
- postgres
- pgweb
- integresql
Run the following command to start all the basic services mentioned above:
$ docker-compose -f docker-compose-dev-infra.yml --env-file .env up
Create a development environment database:
$ npm run db:create
Due to the fact that the development database in the newly built development environment is brand new and has no schema, it is necessary to run the accumulated migration scripts to update the database schema:
$ npm run migration:run
This step is optional. If some data seeds have already been defined in the project, running the following script can quickly synchronize some initial data to the database:
$ npm run seed:run
Data seeds are usually placed in the modules/*/testing/*.seeder.ts
file.
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
Unit test files are usually placed in the same directory as functional code. In this project template, there are all files in the src
directory that end with .spec.ts
. Run unit tests:
$ npm run test:unit
E2E testing files are generally not placed together with functional code. In this project template, they are all files in the test
directory that end with .e2e-spec.ts
. Run end-to-end testing:
$ npm run test:e2e
Run all tests at once without distinguishing test types:
$ npm run test
When you modify the code of any database entity, specifically the classes decorated with the @Entity()
provided by typeorm, you can run the following script to have typeorm automatically generate a database migration script:
$ npm run migration:generate
The generated database migration script will be placed in the src/database/migrations/
directory.
If you want to revert the latest database migration script, you can run the following command:
$ npm run migration:revert
It should be noted that each time this command is run, only the most recent database migration script will be revoked. If you need to revoke multiple times, you need to run the script continuously for the corresponding number of times.
This template uses nestjs page to support data pagination.
REPL (Read Eval Print Loop) is an interactive environment that accepts single user inputs, executes them, and returns the results to the user. The REPL function allows you to directly check the dependency graph from the terminal and call methods on the provider (and controller):
$ npm run start:repl