README.md
May 23, 2026 · View on GitHub
NestJS Boilerplate
Description
NestJS REST API boilerplate with TypeORM, MariaDB, JWT authentication, Swagger, Docker Compose, validation, and migrations.
Requirements
- Node.js compatible with the versions in
package.json - Yarn
- Docker + Docker Compose for the containerized workflow
Quick start with Docker
./init
The init script will:
- copy
.env.exampleto.envwhen.envis missing; - generate a strong
JWT_SECRET_KEYwhen it is empty or still set tochangeme; - build and start the Docker Compose stack;
- wait for MariaDB to become ready;
- run TypeORM migrations.
After startup:
- API through nginx: http://localhost:8080
- Swagger docs: http://localhost:8080/api/docs
- MariaDB:
localhost:3306
For IDE autocompletion on the host machine, run:
yarn
Local development without Docker
-
Install dependencies:
yarn -
Create and edit environment config:
cp .env.example .env openssl rand -base64 32 # use this for JWT_SECRET_KEY -
Make sure MariaDB is running and that
DB_HOST,DB_PORT,DB_USERNAME,DB_PASSWORD, andDB_DATABASEmatch your local database. -
Run migrations:
yarn migration:run -
Start the app:
yarn start:dev
Local app URL: http://localhost:3000
Environment variables
| Variable | Description |
|---|---|
APP_ENV | Application environment. Use dev for local development. |
APP_URL | Value returned by the authenticated root endpoint. |
JWT_SECRET_KEY | Required JWT signing secret. Must be at least 32 characters. |
JWT_EXPIRATION_TIME | Optional JWT expiration time in seconds. |
CORS_ALLOW_ORIGIN | Comma-separated allowed origins. Required outside dev. |
DB_TYPE | Database type. This project is configured for mariadb/mysql. |
DB_HOST | Database host. In Docker Compose this is db. |
DB_PORT | Database port. |
DB_USERNAME | Database user. |
DB_PASSWORD | Database password. |
DB_DATABASE | Database name. |
DB_SYNC | TypeORM synchronize flag. Keep false outside throwaway development. |
API
Swagger is available at /api/docs.
Main routes:
POST /api/auth/register— create a userPOST /api/auth/login— receive a JWT access tokenGET /api/auth/me— return the authenticated userGET /— authenticated root endpoint
Use the returned JWT as a bearer token for protected routes.
TypeORM and migrations
TypeORM configuration lives in src/database/data-source.ts. Entities are loaded from src/**/*.entity.ts in development and dist/**/*.entity.js after build.
Create a migration:
yarn migration:create src/migrations/MigrationName
Run migrations:
yarn migration:run
Revert the last migration:
yarn migration:revert
Inside Docker, prefix commands with docker exec -it nest, for example:
docker exec -it nest yarn migration:run
DB_SYNC=truecan be useful for throwaway local experiments, but do not use it in production because schema synchronization can cause data loss.
Scripts
yarn start # run src/main.ts with ts-node
yarn start:dev # run with nodemon
yarn start:debug # run with nodemon debug config
yarn start:prod # run compiled dist/main.js
yarn test # unit tests
yarn test:watch # unit tests in watch mode
yarn test:cov # coverage
yarn test:e2e # e2e tests
yarn format # prettier over src/**/*.ts
Docker files
docker-compose.yml— local development stack: NestJS app, nginx, MariaDB.docker-compose-swarm.yml— swarm-oriented deployment template.Dockerfile— development app image.Dockerfile-prod— production app image.Dockerfile-nginx— nginx reverse proxy image.
Notes
- Passwords are hashed with
bcryptthrough a TypeORM value transformer. - Validation is enabled globally with Nest
ValidationPipeand a custom trim pipe. - CORS is open only in
APP_ENV=devwhenCORS_ALLOW_ORIGINis empty.