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 schematicarrow-up-right by Marc Stammerjohann, which allows for exactly that, and then add it to our project.

yarn add nestjs-prisma
nest add nestjs-prisma
circle-info

Choose the following options:

  • Datasource Provider - Postgresql

  • Dockerize - Yes

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

circle-info

Remember that the TypeORM database is using the port 5432, and the test database uses port 5433. You may then wish to use another port such as 5434.

  • Add .env to .gitignore

  • In package.json, in the prisma scripts, replace npx with yarn

  • 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

circle-info

If necessary, change the names of containers/volumes in Docker Desktop to avoid duplicity

  • Turn on the database container

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 to migrate:run

  • Change migrate:dev:create to migrate:create and add a -n at the end

circle-info

Remember to have the Prisma extension installed.

Commit - Prisma and docker setup

Last updated