Dockerで作ったNest.js×Nxst.jsにprismaとPostgreSQLをいれる

https://qiita.com/yasdtech/items/31c9ea7553bd4ddbd9bc
これの続きです！
今回はPostgreSQLとORMにPrismaを使用することにします(業務で使うので！！)
# Docker修正

```docker-compose.yml
version: "3.8"

services:
  api:
    build:
      context: ./api
      dockerfile: Dockerfile
    volumes:
      - ./api:/app
##############追加##################
    depends_on:
      - db
###################################
    ports:
      - "3000:3000"
##############追加##################
      - "5555:5555"
###################################
    networks:
      - app-network

  front:
    build:
      context: ./front
      dockerfile: Dockerfile
    volumes:
      - ./front:/app
    ports:
      - "4000:3000"
    networks:
      - app-network

##############追加##################
  db:
    image: postgres:15.3
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: test_user
      POSTGRES_PASSWORD: test_pw
      POSTGRES_DB: test_db
    restart: always
    networks:
      - app-network
###################################

networks:
  app-network:
    driver: bridge
```

```Dockerfile
FROM node:20.10.0

WORKDIR /app

COPY package*.json ./

RUN npm i -g @nestjs/cli
##############追加##################
RUN yarn add -D prisma
RUN yarn add @prisma/client
###################################

RUN yarn install


COPY . .

CMD ["npm", "run", "start"]
```

ファイルの修正はこれで完了です！
ファイルの作成していきます！

```sh
$ docker compose build
$ cd api
$ docker compose run --rm api npx prisma init

Need to install the following packages:
prisma@5.7.0
Ok to proceed? (y) y

✔ Your Prisma schema was created at prisma/schema.prisma
  You can now open it in your favorite editor.

warn You already have a .gitignore file. Don't forget to add `.env` in it to not commit any private information.

Next steps:
1. Set the DATABASE_URL in the .env file to point to your existing database. If your database has no tables yet, read https://pris.ly/d/getting-started
2. Set the provider of the datasource block in schema.prisma to match your database: postgresql, mysql, sqlite, sqlserver, mongodb or cockroachdb.
3. Run npx prisma db pull to turn your database schema into a Prisma schema.
4. Run npx prisma generate to generate the Prisma Client. You can then start querying your database.

More information in our documentation:
https://pris.ly/d/getting-started
```
こんな感じで出れば完了です！
todo-apiの中にprismaディレクトリが確認できればOK！
みんな大好きモダンな開発環境の完成です！


試しにUserモデルを作成してみます！
```schema.prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

// 追加
model User {
  id Int @id @default(autoincrement())
  createAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  email String @unique
  hashedPassword String
}
// 

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}
```


```.env
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="postgresql://test_user:test_pw@db:5432/test_db?schema=public"
# docker-compose.ymlの値に合わせます
```
ここまでできれば、この内容を反映させていきます！
```sh
$ docker compose run --rm api npx prisma migrate dev

✔ Enter a name for the new migration: … user_v0
#こんな感じでマイグレーションファイルの名前決めてくれと言ってくるので適当に決めます。命名規則はそのうち学ぶ

Applying migration `20231207152451_user_v0`

The following migration(s) have been created and applied from new schema changes:

migrations/
  └─ 20231207152451_user_v0/
    └─ migration.sql

Your database is now in sync with your schema.

Running generate... (Use --skip-generate to skip the generators)
yarn add v1.22.19
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
warning " > ts-loader@9.5.1" has unmet peer dependency "webpack@^5.0.0".
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 3 new dependencies.
info Direct dependencies
└─ prisma@5.7.0
info All dependencies
├─ @prisma/engines@5.7.0
├─ @prisma/fetch-engine@5.7.0
└─ prisma@5.7.0
Done in 253.36s.
yarn add v1.22.19
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
warning " > ts-loader@9.5.1" has unmet peer dependency "webpack@^5.0.0".
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ @prisma/client@5.7.0
info All dependencies
└─ @prisma/client@5.7.0
Done in 16.51s.

✔ Generated Prisma Client (v5.7.0) to ./node_modules/@prisma/client in 1.99s
```

完了です！


prisma studioで起動確認します！
```sh
$ docker compose up -d
$ docker compose exec api yarn prisma studio
```
http://localhost:5555/
ここにいい感じにアクセスできればOKです！

# まとめ
・.envの
```
DATABASE_URL="postgresql://test_user:test_pw@db:5432/test_db?schema=public"
```
これDockerだとlocalhostじゃなくてコンテナ名にするのちょっとつまった

・Dockerfile何書くのが正解かがびみょい

・prisma studioが
```
docker compose run --rm api yarn prisma studio
```
これでうまくアクセスできないのなんでやろ、、、
有識者の方いたら教えてください🥺