# Integration with Prisma/Docker

Let's begin by automatically integrating **Prisma** and **Docker** with Nest. We shall first install [this schematic](https://github.com/notiz-dev/nestjs-prisma) by Marc Stammerjohann, which allows for exactly that, and then add it to our project.

```sh
yarn add nestjs-prisma
nest add nestjs-prisma
```

{% hint style="info" %}
Choose the following options:

* Datasource Provider - **Postgresql**
* Dockerize - **Yes**
  {% endhint %}

And then create the <mark style="color:blue;">`DatabaseModule`</mark>, to configure Prisma...

```sh
nest g mo database
```

...and add the following to its <mark style="color:blue;">`imports`</mark> so that Prisma is available globally.

```typescript
PrismaModule.forRoot({ isGlobal: true })
```

And finally, some more steps...

* Adjust <mark style="color:purple;">.env</mark> file and create <mark style="color:purple;">.env.example</mark>

{% hint style="info" %}
Remember that the **TypeORM database** is using the port <mark style="color:blue;">5432</mark>, and the **test database** uses port <mark style="color:blue;">5433</mark>. You may then wish to use another port such as <mark style="color:blue;">5434</mark>.
{% endhint %}

* Add <mark style="color:purple;">.env</mark> to <mark style="color:purple;">.gitignore</mark>
* In <mark style="color:purple;">package.json</mark>, in the **prisma scripts**, replace <mark style="color:blue;">`npx`</mark> with <mark style="color:blue;">`yarn`</mark>
* In the <mark style="color:purple;">Dockerfile</mark>, adjust to use **yarn**

```docker
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 <mark style="color:purple;">docker-compose.yml</mark>, use the **environment variables** and clean up the file a bit

```yaml
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}
```

{% hint style="info" %}
If necessary, change the names of containers/volumes in **Docker Desktop** to avoid duplicity
{% endhint %}

* Turn on the database container

```sh
docker-compose up -d database
```

We'll also change the **migration scripts**, so that their usage becomes simpler. So, in <mark style="color:purple;">package.json</mark>, let's alter the following scripts:

* Change <mark style="color:blue;">`migrate:dev`</mark> to <mark style="color:blue;">`migrate:run`</mark>
* Change <mark style="color:blue;">`migrate:dev:create`</mark> to <mark style="color:blue;">`migrate:create`</mark> and add a <mark style="color:blue;">`-n`</mark> at the end

{% hint style="info" %}
Remember to have the **Prisma extension** installed.
{% endhint %}

<mark style="color:green;">**Commit**</mark> - Prisma and docker setup
