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
- Rate limiting
- Caching
- Authentication
- Task Scheduling
- Testing
- Unit testing framework
- E2E testing framework
$ npm installApp will load .env.${APP_ENV} file in project root as environment variable file when starting up. If APP_ENV is not be set, app will load .env file in project root as environment variable file. For example, if APP_ENV=production, app will load .env.production as environment variable file.
To create an environment variable file, you can copy it from the .env.example file:
$ cp .env.example .envYou 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 upCreate a development environment database:
$ npm run db:createDue 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:runThis 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:runData 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:prodUnit 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:unitE2E 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:e2eRun all tests at once without distinguishing test types:
$ npm run testE2E tests require PostgreSQL and IntegreSQL. For local development, start the development infrastructure first:
$ docker compose -f docker-compose-dev-infra.yml --env-file .env upThis template is JWT-first and does not mount server-side session middleware by
default. Tokens are accepted from the Authorization: Bearer header only.
Before using this template in production:
- Set
NODE_ENV=production. - Use a random
APP_SECRETwith at least 32 characters. - Set explicit
ALLOWED_ORIGINS; wildcard CORS is not allowed by validation in production. - Keep
API_DOCS_ENABLED=falseunless the deployment is private. - Configure database pool variables:
DATABASE_POOL_MAXDATABASE_POOL_IDLE_TIMEOUT_MSDATABASE_POOL_CONNECTION_TIMEOUT_MS
- Configure database SSL with
DATABASE_SSL_MODE; useverify-caorverify-fullwithDATABASE_SSL_CAwhen your platform provides a CA bundle. - Run migrations before serving traffic.
- Use
/health/livefor liveness probes and/health/readyfor readiness probes.
GitHub Actions are defined in .github/workflows/ci.yml. Reusable CI commands
live in the ci/ directory:
$ bash ci/validate.sh
$ bash ci/test-e2e.shThe CI workflow starts PostgreSQL and IntegreSQL services, then runs build, unit tests, and E2E tests.
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:generateThe 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:revertIt 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