Vivify-Ideas/nestjs-boilerplate
NestJS Boilerplate π»(Authentication, TypeORM, Configuration, Swagger)
NestJS REST API boilerplate with TypeORM, MariaDB, JWT authentication, Swagger, Docker Compose, validation, and migrations.
package.json./init
The init script will:
.env.example to .env when .env is missing;JWT_SECRET_KEY when it is empty or still set to changeme;After startup:
localhost:3306For IDE autocompletion on the host machine, run:
yarn
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, and DB_DATABASE match your local database.
Run migrations:
yarn migration:run
Start the app:
yarn start:dev
Local app URL: http://localhost:3000
No download data available
No tracked packages depend on this.
| 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. |
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 endpointUse the returned JWT as a bearer token for protected routes.
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.
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-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.bcrypt through a TypeORM value transformer.ValidationPipe and a custom trim pipe.APP_ENV=dev when CORS_ALLOW_ORIGIN is empty.