Integration with Prisma/Docker
An easier way to integrate these tools into our project.
Let's begin by automatically integrating Prisma and Docker with Nest. We shall first install this schematic by Marc Stammerjohann, which allows for exactly that, and then add it to our project.
yarn add nestjs-prisma
nest add nestjs-prisma
And then create the DatabaseModule
, to configure Prisma...
nest g mo database
...and add the following to its imports
so that Prisma is available globally.
PrismaModule.forRoot({ isGlobal: true })
And finally, some more steps...
Adjust .env file and create .env.example
Add .env to .gitignore
In package.json, in the prisma scripts, replace
npx
withyarn
In the Dockerfile, adjust to use yarn
FROM node:16 AS builder
WORKDIR /app
COPY package.json ./
COPY yarn.lock ./
COPY prisma ./prisma/
RUN yarn
COPY . .
RUN yarn build
FROM node:16
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./
COPY --from=builder /app/yarn.lock ./
COPY --from=builder /app/dist ./dist
EXPOSE 3000
CMD [ "yarn", "start:prod" ]
In docker-compose.yml, use the environment variables and clean up the file a bit
version: '3.8'
services:
backend:
build: .
restart: always
ports:
- 3000:3000
depends_on:
- database
database:
image: postgres
restart: always
ports:
- ${DATASOURCE_PORT}:5432
environment:
POSTGRES_PASSWORD: ${DATASOURCE_PASSWORD}
Turn on the database container
docker-compose up -d database
We'll also change the migration scripts, so that their usage becomes simpler. So, in package.json, let's alter the following scripts:
Change
migrate:dev
tomigrate:run
Change
migrate:dev:create
tomigrate:create
and add a-n
at the end
Commit - Prisma and docker setup
Last updated