diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-12-31 19:35:17 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-12-31 19:35:17 +0900 |
| commit | f211ebcfac0a21e264b67c1226509896a11ed5ca (patch) | |
| tree | 502c10bbebe041c439a54b3fd08a5b90b5be4429 | |
| parent | 594482a280279149cbf5cb8b8d086961e65b0fb0 (diff) | |
| download | kioku-f211ebcfac0a21e264b67c1226509896a11ed5ca.tar.gz kioku-f211ebcfac0a21e264b67c1226509896a11ed5ca.tar.zst kioku-f211ebcfac0a21e264b67c1226509896a11ed5ca.zip | |
refactor(db): replace DATABASE_URL with individual POSTGRES_* env vars
Eliminates duplicate configuration by building the connection URL
from POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB, POSTGRES_HOST,
and POSTGRES_PORT instead of requiring a separate DATABASE_URL.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
| -rw-r--r-- | .env.example | 3 | ||||
| -rw-r--r-- | .github/workflows/ci.yaml | 2 | ||||
| -rw-r--r-- | README.md | 5 | ||||
| -rw-r--r-- | compose.yaml | 6 | ||||
| -rw-r--r-- | drizzle.config.ts | 7 | ||||
| -rw-r--r-- | src/server/db/index.ts | 13 |
6 files changed, 17 insertions, 19 deletions
diff --git a/.env.example b/.env.example index 90ff45a..6ba9628 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,6 @@ POSTGRES_USER=kioku POSTGRES_PASSWORD=kioku POSTGRES_DB=kioku -DATABASE_URL=postgresql://kioku:kioku@kioku-db:5432/kioku +POSTGRES_HOST=localhost +POSTGRES_PORT=5432 JWT_SECRET=your-secret-key-here diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f83116e..86db887 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -34,7 +34,7 @@ jobs: run: pnpm typecheck - name: Test - run: DATABASE_URL="fake" pnpm test + run: POSTGRES_USER=test POSTGRES_PASSWORD=test POSTGRES_DB=test POSTGRES_HOST=localhost POSTGRES_PORT=5432 pnpm test - name: Build run: pnpm build @@ -29,8 +29,6 @@ pnpm install # Copy environment variables cp .env.example .env -# Edit .env for local development: -# DATABASE_URL=postgresql://kioku:kioku@localhost:5432/kioku # Start PostgreSQL docker compose up db -d @@ -53,7 +51,8 @@ pnpm dev:client # Frontend dev server (port 5173) | `POSTGRES_USER` | PostgreSQL username | `kioku` | | `POSTGRES_PASSWORD` | PostgreSQL password | `kioku` | | `POSTGRES_DB` | PostgreSQL database name | `kioku` | -| `DATABASE_URL` | Full PostgreSQL connection string | `postgresql://kioku:kioku@localhost:5432/kioku` | +| `POSTGRES_HOST` | PostgreSQL host | `kioku-db` | +| `POSTGRES_PORT` | PostgreSQL port | `5432` | | `JWT_SECRET` | Secret key for JWT tokens (use a secure random string in production) | `your-secret-key` | ## Scripts diff --git a/compose.yaml b/compose.yaml index 6308342..8c89cd3 100644 --- a/compose.yaml +++ b/compose.yaml @@ -24,7 +24,11 @@ services: container_name: kioku-server restart: unless-stopped environment: - DATABASE_URL: ${DATABASE_URL} + POSTGRES_USER: ${POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB} + POSTGRES_HOST: kioku-db + POSTGRES_PORT: 5432 JWT_SECRET: ${JWT_SECRET} NODE_ENV: production expose: diff --git a/drizzle.config.ts b/drizzle.config.ts index 6fe73ec..0520eb6 100644 --- a/drizzle.config.ts +++ b/drizzle.config.ts @@ -1,15 +1,12 @@ import { defineConfig } from "drizzle-kit"; -const databaseUrl = process.env.DATABASE_URL; -if (!databaseUrl) { - throw new Error("DATABASE_URL environment variable is not set"); -} +const { POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB, POSTGRES_HOST, POSTGRES_PORT } = process.env; export default defineConfig({ out: "./drizzle", schema: "./src/server/db/schema.ts", dialect: "postgresql", dbCredentials: { - url: databaseUrl, + url: `postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}`, }, }); diff --git a/src/server/db/index.ts b/src/server/db/index.ts index 0e69fad..4826710 100644 --- a/src/server/db/index.ts +++ b/src/server/db/index.ts @@ -2,15 +2,12 @@ import { drizzle } from "drizzle-orm/node-postgres"; import * as schema from "./schema.js"; import * as schemaCrdt from "./schema-crdt.js"; -const databaseUrl = process.env.DATABASE_URL; +const { POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB, POSTGRES_HOST, POSTGRES_PORT } = process.env; -if (!databaseUrl) { - throw new Error("DATABASE_URL environment variable is not set"); -} - -export const db = drizzle(databaseUrl, { - schema: { ...schema, ...schemaCrdt }, -}); +export const db = drizzle( + `postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}`, + { schema: { ...schema, ...schemaCrdt } }, +); export * from "./schema.js"; export * from "./schema-crdt.js"; |
