Install Nestjs CLI to start and generate CRUD resources
# pnpm
pnpm i -g @nestjs/cli
# yarn
yarn add -g @nestjs/cli
# npm
npm add -g @nestjs/cli
Install the dependencies for the Nest application:
# pnpm
pnpm install
# npm
npm install
# yarn
yarn install
support different .env files .env.development
and .env.production
Don't use prisma client command directly. Because prisma doesn't support different .env files, so we use
./scripts/prisma.js
to loaded different .env files.
using prisma client command like this below
npm run prisma -> node ./scripts/prisma.js
prisma migrate dev -> npm run prisma dev migrate dev
prisma migrate deploy -> npm run prisma prod migrate deploy
dev
-> development
prod
-> production
Setup a development PostgreSQL with Docker. Copy .env.example and rename to .env
- cp .env.example .env.development
and cp .env.example .env.production
- which sets the required environments for PostgreSQL such as POSTGRES_USER
, POSTGRES_PASSWORD
and POSTGRES_DB
. Update the variables as you wish and select a strong password.
Start the PostgreSQL database using docker (recomand)
docker-compose -f docker-compose.db.yml up -d
Prisma Migrate is used to manage the schema and migration of the database. Prisma datasource requires an environment variable DATABASE_URL
for the connection to the PostgreSQL database. Prisma reads the DATABASE_URL
from the root .env file.
Use Prisma Migrate in your development environment to
- Creates
migration.sql
file - Updates Database Schema
- Generates Prisma Client
npm run migrate:dev
If you are happy with your database changes you want to deploy those changes to your production database. Use prisma migrate deploy
to apply all pending migrations, can also be used in CI/CD pipelines as it works without prompts.
npm run migrate:deploy
Prisma Client JS is a type-safe database client auto-generated based on the data model.
Generate Prisma Client JS by running
Note: Every time you update schema.prisma re-generate Prisma Client JS
npx prisma generate
# or
npm run prisma:generate
Run Nest Server in Development mode:
npm run start
# watch mode
npm run start:dev
Run Nest Server in Production mode:
npm run start:prod
- pino-http to pretty log and logging http logs
- rotating-file-stream saves log files to ./logs directory. it will divide logs files to seperate files day by day.
RESTful API (localhost:3000/api) documentation available with Swagger.
Nest server is a Node.js application and it is easily dockerized.
See the Dockerfile on how to build a Docker image of your Nest server.
Now to build a Docker image of your own Nest server simply run:
# give your docker image a name
docker build -t <your username>/nest-prisma-server .
# for example
docker build -t nest-prisma-server .
After Docker build your docker image you are ready to start up a docker container running the nest server:
docker run -d -t -p 3000:3000 -v /path/to/logs:/app/logs --env-file .env.production nest-prisma-server
Now open up localhost:3000 to verify that your nest server is running.
When you run your NestJS application in a Docker container update your .env file
You can also setup a the database and Nest application with the docker-compose
# building new NestJS docker image
docker-compose build
# or
npm run docker:build
# start docker-compose
docker-compose up -d
# or
npm run docker