Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.
/ nestjs-boilerplate Public archive

A Dockerized NestJS project boilerplate with PostgreSQL, Prisma, migrations, seeder, and a convenient development environment with watch mode

Notifications You must be signed in to change notification settings

ewwmy/nestjs-boilerplate

Repository files navigation

NestJS Boilerplate

🚀 Features:

  • Dockerized
  • PostgreSQL database
    • Test database included
    • Simple NestJS controller which communicates with the database
    • Prisma ORM
  • Convenient development environment with watch mode through the Docker
  • dev / prod Docker build modes
  • Migrations
  • Seeder

Run

cp .env.example .env.development
cp .env.example .env.production
cp ./backend/prisma/.env.example ./backend/prisma/.env

Edit env-files for your purposes.

Install dependencies:

cd backend
npm i # or `npm ci` if you need exact versions of the packages locked in `package-lock.json`
cd ..

Make sure the current dir is set to the root of the project before working with docker compose.

Development

docker compose --env-file .env.development -f docker-compose.yml -f docker-compose.dev.yml up --build

Production

docker compose --env-file .env.production up --build -d

Shutdown

docker compose down

Database connection

Development

psql -h localhost -p 5432 -U nestjs-boilerplate -d nestjs-boilerplate

Migrations

Example of how to make migrations

Using the predefined npm scripts:

npm run migrate:dev -- --name migration_name

Inside the docker container:

docker compose exec nestjs-boilerplate-backend npx prisma migrate dev --name migration_name

Outside the docker container (make sure you have set the DATABASE_URL variable in your environment):

DATABASE_URL=postgresql://user:password@localhost:5432/nestjs-boilerplate npx prisma migrate dev --name migration_name

or (prisma/.env file with the DATABASE_URL variable must exist):

npx prisma migrate dev --name migration_name

Seeding

Inside the Docker container
docker compose exec nestjs-boilerplate-backend npx prisma db seed
Outside Docker
DATABASE_URL=postgresql://user:password@localhost:5432/nestjs-boilerplate npx prisma db seed

or just (if the DATABASE_URL environment variable is set properly):

npx prisma db seed

Production

In this mode the database container is not accessible from outside, so we need to connect to the backend container first to access the database service from it:

docker exec -it nestjs-boilerplate-backend /bin/sh
apk update
apk add postgresql-client
psql -h nestjs-boilerplate -U nestjs-boilerplate -d nestjs-boilerplate # hostname (-h) can be either the service name or the container name from the docker compose config

About

A Dockerized NestJS project boilerplate with PostgreSQL, Prisma, migrations, seeder, and a convenient development environment with watch mode

Resources

Stars

Watchers

Forks

Packages

No packages published