Skip to content

Commit fc4aa9a

Browse files
Merge pull request #69 from call-0f-code/local-setup
Containerized api with production data seeding script enabled
2 parents 578e803 + 780a51a commit fc4aa9a

File tree

9 files changed

+1269
-2
lines changed

9 files changed

+1269
-2
lines changed

.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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
1212
# Runtime data
1313
pids
1414
*.pid
15-
*.seed
1615
*.pid.lock
1716

1817
# Directory for instrumented libs generated by jscoverage/JSCover

Dockerfile

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
FROM oven/bun:1.3 AS base
2+
WORKDIR /var/www/api
3+
4+
# -----------------------------
5+
# deps stage - cache dependencies
6+
# -----------------------------
7+
FROM base AS deps
8+
9+
COPY package.json bun.lock* ./
10+
COPY prisma ./prisma
11+
RUN bun install --frozen-lockfile
12+
RUN bunx prisma generate
13+
14+
# -----------------------------
15+
# build stage - compile TypeScript to JavaScript
16+
# -----------------------------
17+
FROM deps AS build
18+
19+
COPY . .
20+
RUN bun build src/server.ts --target=bun --production --outdir dist
21+
22+
# -----------------------------
23+
# development stage
24+
# -----------------------------
25+
FROM deps AS development
26+
COPY . .
27+
EXPOSE 3000
28+
CMD ["bun", "src/server.ts"]
29+
30+
# -----------------------------
31+
# production stage
32+
# -----------------------------
33+
34+
35+
FROM oven/bun:1-slim AS production
36+
37+
WORKDIR /var/www/api
38+
RUN groupadd -g 1001 nodejs && useradd -u 1001 -g nodejs -m bunjs
39+
40+
COPY --from=build --chown=bunjs:nodejs /var/www/api/dist ./dist
41+
COPY --from=deps --chown=bunjs:nodejs /var/www/api/src/generated ./dist/generated
42+
43+
44+
RUN chown -R bunjs:nodejs /var/www/api
45+
USER bunjs
46+
47+
ENV NODE_ENV=production
48+
ENV PORT=3000
49+
50+
EXPOSE 3000
51+
52+
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
53+
CMD wget -qO- http://localhost:3000/health || exit 1
54+
55+
CMD ["bun", "./dist/server.js"]

LOCAL_DEVELOPMENT.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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`)**: Optional. The current local setup prefers loading a local `seed/dump.sql` file. `pg_dump` is only required if you want to create a fresh dump from a remote database manually.
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 local environment bring-up. By default it will load the local `seed/dump.sql` into the local Postgres instance.
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. **Loads Local Seed**: The script will attempt to load `seed/dump.sql` into the DB only when there are no existing user tables present in the database. If the database already contains tables (non-system tables), the seed step will be skipped to avoid accidentally overwriting existing data. This is the default and recommended local flow.
36+
4. **Starts the API**: Launches the `api` container and waits for it to be healthy.
37+
38+
Flags:
39+
- `--skip-dump`: reuse existing `seed/dump.sql` (no remote dump step)
40+
- `--skip-seed`: skip loading the seed and just start containers
41+
42+
### 3. Useful Commands
43+
44+
- **Start environment**: `bash scripts/setup-local.sh`
45+
- **Skip dump (reuse existing `seed/dump.sql`)**: `bash scripts/setup-local.sh --skip-dump`
46+
- **Skip seeding (just start containers)**: `bash scripts/setup-local.sh --skip-seed`
47+
- **View logs**: `docker compose logs -f`
48+
- **Stop containers**: `docker compose down`
49+
- **Wipe local data (force re-seed)**: `docker compose down -v`
50+
51+
## Troubleshooting
52+
53+
### Tables not in `public` schema
54+
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.

docker-compose.yml

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