Skip to content

Commit a0ad203

Browse files
committed
Containerized api with production data seeding script enabled
1 parent 578e803 commit a0ad203

8 files changed

Lines changed: 503 additions & 2 deletions

File tree

.dockerignore

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Dependencies
2+
node_modules
3+
bun.lockb
4+
5+
# Build outputs
6+
dist
7+
build
8+
.next
9+
10+
# Environment files
11+
.env
12+
.env.local
13+
.env.*.local
14+
15+
# Logs
16+
logs
17+
*.log
18+
npm-debug.log*
19+
yarn-debug.log*
20+
yarn-error.log*
21+
bun-debug.log*
22+
23+
# Runtime data
24+
pids
25+
*.pid
26+
*.seed
27+
*.pid.lock
28+
29+
# Coverage directory used by tools like istanbul
30+
coverage
31+
*.lcov
32+
33+
# IDE files
34+
.vscode
35+
.idea
36+
*.swp
37+
*.swo
38+
39+
# OS generated files
40+
.DS_Store
41+
.DS_Store?
42+
._*
43+
.Spotlight-V100
44+
.Trashes
45+
ehthumbs.db
46+
Thumbs.db
47+
48+
# Git
49+
.git
50+
.gitignore
51+
52+
# Docker
53+
Dockerfile*
54+
docker-compose*
55+
.dockerignore
56+
57+
# Documentation and README
58+
README.md
59+
*.md
60+
docs
61+
62+
# Test files
63+
tests
64+
__tests__
65+
*.test.ts
66+
*.test.js
67+
*.spec.ts
68+
*.spec.js
69+
70+
# Development tools
71+
.eslintrc*
72+
.prettierrc*
73+
jest.config*
74+
tsconfig*.json
75+
76+
# Prisma migrations
77+
# prisma/migrations
78+
79+
# Temporary files
80+
tmp
81+
temp

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ DIRECT_URL=
88
# Supabase URL
99
SUPABASE_URL=
1010

11+
# Session-mode pooler URL — used by setup-local.sh for pg_dump (IPv4-accessible).
12+
SESSION_POOLER=
13+
1114
# Supabase service role key
1215
SUPABASE_SERVICE_ROLE_KEY=
1316

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
1212
# Runtime data
1313
pids
1414
*.pid
15-
*.seed
15+
seed
1616
*.pid.lock
1717

1818
# Directory for instrumented libs generated by jscoverage/JSCover

Dockerfile

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
FROM node:20-alpine AS base
2+
3+
RUN apk add --no-cache curl bash ca-certificates
4+
5+
# Install Bun
6+
RUN curl -fsSL https://bun.sh/install | bash
7+
ENV PATH="/root/.bun/bin:$PATH"
8+
9+
RUN if [ -f /root/.bun/bin/bun ]; then \
10+
/root/.bun/bin/bun --version && \
11+
if [ ! -f /root/.bun/bin/bunx ]; then \
12+
ln -s /root/.bun/bin/bun /root/.bun/bin/bunx; \
13+
fi; \
14+
else \
15+
echo "ERROR: Bun not installed properly" && exit 1; \
16+
fi
17+
18+
WORKDIR /app
19+
20+
21+
# -----------------------------
22+
# deps stage - cache dependencies
23+
# -----------------------------
24+
25+
26+
FROM base AS deps
27+
28+
COPY package.json bun.lock* ./
29+
COPY prisma ./prisma
30+
31+
RUN bun install --frozen-lockfile
32+
RUN bunx prisma generate
33+
34+
35+
# -----------------------------
36+
# development stage
37+
# -----------------------------
38+
39+
40+
FROM deps AS development
41+
42+
COPY --from=deps /app/node_modules ./node_modules
43+
COPY --from=deps /app/src/generated ./src/generated
44+
45+
COPY . .
46+
47+
EXPOSE 3000
48+
49+
CMD ["bun", "src/server.ts"]
50+
51+
52+
# -----------------------------
53+
# production stage
54+
# -----------------------------
55+
56+
57+
FROM deps AS production
58+
59+
RUN addgroup -g 1001 -S nodejs && adduser -S bunjs -u 1001
60+
RUN cp -r /root/.bun /usr/local/bun && chown -R bunjs:nodejs /usr/local/bun
61+
62+
COPY --from=deps --chown=bunjs:nodejs /app/node_modules ./node_modules
63+
COPY --from=deps --chown=bunjs:nodejs /app/src/generated ./src/generated
64+
65+
COPY --chown=bunjs:nodejs src ./src
66+
COPY --chown=bunjs:nodejs package.json ./
67+
COPY --chown=bunjs:nodejs prisma ./prisma
68+
69+
USER bunjs
70+
71+
ENV NODE_ENV=production
72+
ENV PORT=3000
73+
ENV PATH="/usr/local/bun/bin:$PATH"
74+
75+
EXPOSE 3000
76+
77+
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
78+
CMD wget -qO- http://localhost:3000/health || exit 1
79+
80+
CMD ["sh", "-c", "bun src/server.ts"]

LOCAL_DEVELOPMENT.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Local Development Environment Setup
2+
3+
This document explains how to set up and manage the local development environment for the COC-API project.
4+
5+
## Prerequisites
6+
7+
- **Docker & Docker Compose**: Ensure you have Docker installed and running.
8+
- **PostgreSQL Client (`pg_dump`)**: The setup script uses `pg_dump` to pull data from the remote database.
9+
10+
## Setup Instructions
11+
12+
### 1. Configure Environment Variables
13+
14+
Copy the `.env.example` file to `.env` and fill in the required values:
15+
16+
```bash
17+
cp .env.example .env
18+
```
19+
20+
Key variables for the setup script:
21+
- `SESSION_POOLER`: The direct connection string to your Supabase project (Session Pooler / IPv4).
22+
- **Format**: `postgresql://postgres.PROJECT_REF:PASSWORD@aws-0-us-east-1.pooler.supabase.com:5432/postgres`
23+
24+
### 2. Run the Setup Script
25+
26+
The `scripts/setup-local.sh` script automates the entire process:
27+
28+
```bash
29+
bun run local
30+
```
31+
32+
#### What the script does:
33+
1. **Starts Postgres**: Launches the `db` container.
34+
2. **Installs Extensions**: Pre-installs `pgcrypto`, `uuid-ossp`, and `pg_stat_statements` into the `public` schema.
35+
3. **Dumps Remote DB**: Uses `pg_dump` to create a snapshot of the production database.
36+
4. **Cleans the Dump**: Strips Supabase-specific extensions and patches schema references to work locally.
37+
5. **Seeds the Database**: Loads the cleaned dump into your local Postgres container.
38+
6. **Starts the API**: Launches the `api` container and waits for it to be healthy.
39+
40+
### 3. Useful Commands
41+
42+
- **Start environment**: `bash scripts/setup-local.sh`
43+
- **Skip dump (reuse existing `seed/dump.sql`)**: `bash scripts/setup-local.sh --skip-dump`
44+
- **Skip seeding (just start containers)**: `bash scripts/setup-local.sh --skip-seed`
45+
- **View logs**: `docker compose logs -f`
46+
- **Stop containers**: `docker compose down`
47+
- **Wipe local data (force re-seed)**: `docker compose down -v`
48+
49+
## Troubleshooting
50+
51+
### "Tenant or user not found" during dump
52+
This usually means your `SESSION_POOLER` username is just `postgres`. Supabase requires the format `postgres.PROJECT_REF`.
53+
54+
### Tables not in `public` schema
55+
The script automatically patches the dump to ensure tables are placed in the `public` schema. If you manually imported a dump, ensure you've installed the required extensions first.
56+
57+
### Re-seeding
58+
The script skips seeding if it detects existing tables in the `public` schema. To force a re-seed, run `docker compose down -v` before running the setup script.

docker-compose.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
services:
2+
api:
3+
image: coc-api
4+
build:
5+
context: .
6+
dockerfile: Dockerfile
7+
ports:
8+
- "3000:3000"
9+
environment:
10+
NODE_ENV: production
11+
PORT: 3000
12+
DATABASE_URL: "postgresql://postgres:example@db:5432/coc?sslmode=disable"
13+
volumes:
14+
- ./:/app:cached
15+
- /app/node_modules
16+
command: ["bun", "src/server.ts"]
17+
depends_on:
18+
- db
19+
healthcheck:
20+
test: ["CMD", "wget", "-qO-", "http://localhost:3000/health"]
21+
interval: 30s
22+
timeout: 3s
23+
retries: 3
24+
25+
db:
26+
image: postgres:17
27+
environment:
28+
POSTGRES_USER: postgres
29+
POSTGRES_PASSWORD: example
30+
POSTGRES_DB: coc
31+
volumes:
32+
- pgdata:/var/lib/postgresql/data
33+
ports:
34+
- "5432:5432"
35+
healthcheck:
36+
test: ["CMD", "pg_isready", "-U", "postgres", "-d", "coc", "-h", "127.0.0.1", "-p", "5432"]
37+
interval: 5s
38+
timeout: 5s
39+
retries: 5
40+
41+
volumes:
42+
pgdata:
43+
seed-data:

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"precommit": "lint-staged",
1717
"migrate:first": "bunx prisma migrate dev --name init",
1818
"migrate": "bunx prisma migrate dev",
19-
"generate": "bunx prisma generate"
19+
"generate": "bunx prisma generate",
20+
"local": "bash scripts/setup-local.sh"
2021
},
2122
"repository": {
2223
"type": "git",

0 commit comments

Comments
 (0)