Starter template for 😻 NestJS and Prisma.
simple run
make all
Postman Nest-backend Websocket
Install Nestjs CLI to start and generate CRUD resources
Install the dependencies for the Nest application:
# npm
npm install
# yarn
yarn install
# pnpm
pnpm install
Setup a development PostgreSQL with Docker. Copy .env.example and rename to .env
- cp .env.example .env
- 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
make postgres
make redis
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
make prisma-int
npx prisma studio
# or npm/yarn/pnpm
make prisma-studio
Execute the script with this command:
# npm/yarn/pnpm
pnpm run seed
Run Nest Server in Development mode:
# npm/yarn/pnpm
pnpm run start
# watch mode
pnpm run start:dev
Run Nest Server in Production mode:
pnpm run start:prod
Prisma Studio for the NestJS Server is available here: http://localhost:5555/
RESTful 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 --env-file .env 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
- DB_HOST=localhost
# replace with name of the database container
+ DB_HOST=postgres
# Prisma database connection
+ DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DB_HOST}:${DB_PORT}/${POSTGRES_DB}?schema=${DB_SCHEMA}&sslmode=prefer
If DATABASE_URL
is missing in the root .env
file, which is loaded into the Docker container, the NestJS application will exit with the following error:
(node:19) UnhandledPromiseRejectionWarning: Error: error: Environment variable not found: DATABASE_URL.
--> schema.prisma:3
|
2 | provider = "postgresql"
3 | url = env("DATABASE_URL")
You can also setup a the database and Nest application with the docker-compose
# building new NestJS docker image
docker-compose build
# or
pnpm run docker:build
# start docker-compose
docker-compose up -d
# or
pnpm run docker
Update the Prisma schema prisma/schema.prisma
and after that run the following two commands:
npx prisma generate
# or in watch mode
npx prisma generate --watch
# or
pnpm run prisma:generate
pnpm run prisma:generate:watch