Skip to content
CodeSook
CodeSook

06. Setup database


Spin up Postgres

third-party/compose.yaml
third-party/compose.yaml
services:
# ╭─ postgres ─────────────────────────────────────────────────────────╮
postgres:
image: postgres
container_name: db
restart: always
user: postgres
volumes:
- ./db-data:/var/lib/postgresql/data
environment:
- POSTGRES_DB=hono
- POSTGRES_PASSWORD=mysecretpassword
- POSTGRES_USER=codesook
ports:
- 5432:5432
healthcheck:
test: ['CMD', 'pg_isready', -U, <postgres user>]
interval: 10s
timeout: 5s
retries: 5
# ╰─ postgres ─────────────────────────────────────────────────────────╯
# ╭─ postgres admin ───────────────────────────────────────────────────╮
pgadmin:
image: dpage/pgadmin4
container_name: pgadmin
restart: always
ports:
- '8888:80'
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: qLTrQEWZsNuBMsQGFnzKEVipU
volumes:
- ./pgadmin:/var/lib/pgadmin
# ╰─ postgres admin ───────────────────────────────────────────────────╯
Terminal window
docker compose -p hono-day1 up -d

Setup Prisma

install prisma and related stuff
pnpm add prisma @types/node -D
initializing prisma
pnpm exec prisma init

แก้ user, pass, db ใน .env

.env
.env
DATABASE_URL="postgresql://codesook:mysecretpassword@localhost:5432/hono?schema=public"

package.json script

package.json scripts
pnpm pkg set \
'scripts.prisma:generate=prisma generate' \
'scripts.prisma:push=prisma db push' \
'scripts.prisma:pull=prisma db pull' \
'scripts.prisma:migrate=prisma migrate dev' \
'scripts.prisma:migrate:deploy=prisma migrate deploy' \
'scripts.prisma:studio=prisma studio' \
'scripts.prisma:reset=prisma migrate reset'
package.json
"prisma:generate": "prisma generate",
"prisma:push": "prisma db push",
"prisma:pull": "prisma db pull",
"prisma:migrate": "prisma migrate dev",
"prisma:migrate:deploy": "prisma migrate deploy",
"prisma:studio": "prisma studio",
"prisma:reset": "prisma migrate reset"