-
Notifications
You must be signed in to change notification settings - Fork 6
Containerized api with production data seeding script enabled #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
a0ad203
0e71d9d
56e3d95
2556b39
780a51a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # Dependencies | ||
| node_modules | ||
| bun.lockb | ||
|
|
||
| # Build outputs | ||
| dist | ||
| build | ||
| .next | ||
|
|
||
| # Environment files | ||
| .env | ||
| .env.local | ||
| .env.*.local | ||
|
|
||
| # Logs | ||
| logs | ||
| *.log | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| bun-debug.log* | ||
|
|
||
| # Runtime data | ||
| pids | ||
| *.pid | ||
| *.seed | ||
| *.pid.lock | ||
|
|
||
| # Coverage directory used by tools like istanbul | ||
| coverage | ||
| *.lcov | ||
|
|
||
| # IDE files | ||
| .vscode | ||
| .idea | ||
| *.swp | ||
| *.swo | ||
|
|
||
| # OS generated files | ||
| .DS_Store | ||
| .DS_Store? | ||
| ._* | ||
| .Spotlight-V100 | ||
| .Trashes | ||
| ehthumbs.db | ||
| Thumbs.db | ||
|
|
||
| # Git | ||
| .git | ||
| .gitignore | ||
|
|
||
| # Docker | ||
| Dockerfile* | ||
| docker-compose* | ||
| .dockerignore | ||
|
|
||
| # Documentation and README | ||
| README.md | ||
| *.md | ||
| docs | ||
|
|
||
| # Test files | ||
| tests | ||
| __tests__ | ||
| *.test.ts | ||
| *.test.js | ||
| *.spec.ts | ||
| *.spec.js | ||
|
|
||
| # Development tools | ||
| .eslintrc* | ||
| .prettierrc* | ||
| jest.config* | ||
| tsconfig*.json | ||
|
|
||
| # Prisma migrations | ||
| # prisma/migrations | ||
|
|
||
| # Temporary files | ||
| tmp | ||
| temp |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| FROM node:20-alpine AS base | ||
|
|
||
| RUN apk add --no-cache curl bash ca-certificates | ||
|
|
||
| # Install Bun | ||
| RUN curl -fsSL https://bun.sh/install | bash | ||
| ENV PATH="/root/.bun/bin:$PATH" | ||
|
|
||
| RUN if [ -f /root/.bun/bin/bun ]; then \ | ||
| /root/.bun/bin/bun --version && \ | ||
| if [ ! -f /root/.bun/bin/bunx ]; then \ | ||
| ln -s /root/.bun/bin/bun /root/.bun/bin/bunx; \ | ||
| fi; \ | ||
| else \ | ||
| echo "ERROR: Bun not installed properly" && exit 1; \ | ||
| fi | ||
|
|
||
| WORKDIR /app | ||
|
|
||
|
|
||
| # ----------------------------- | ||
| # deps stage - cache dependencies | ||
| # ----------------------------- | ||
|
|
||
|
|
||
| FROM base AS deps | ||
|
|
||
| COPY package.json bun.lock* ./ | ||
| COPY prisma ./prisma | ||
|
|
||
| RUN bun install --frozen-lockfile | ||
| RUN bunx prisma generate | ||
|
|
||
|
|
||
| # ----------------------------- | ||
| # development stage | ||
| # ----------------------------- | ||
|
|
||
|
|
||
| FROM deps AS development | ||
|
|
||
| COPY --from=deps /app/node_modules ./node_modules | ||
| COPY --from=deps /app/src/generated ./src/generated | ||
|
|
||
| COPY . . | ||
|
|
||
| EXPOSE 3000 | ||
|
|
||
| CMD ["bun", "src/server.ts"] | ||
|
|
||
|
|
||
| # ----------------------------- | ||
| # production stage | ||
| # ----------------------------- | ||
|
|
||
|
|
||
| FROM deps AS production | ||
|
|
||
| RUN addgroup -g 1001 -S nodejs && adduser -S bunjs -u 1001 | ||
| RUN cp -r /root/.bun /usr/local/bun && chown -R bunjs:nodejs /usr/local/bun | ||
|
|
||
| COPY --from=deps --chown=bunjs:nodejs /app/node_modules ./node_modules | ||
| COPY --from=deps --chown=bunjs:nodejs /app/src/generated ./src/generated | ||
|
|
||
| COPY --chown=bunjs:nodejs src ./src | ||
| COPY --chown=bunjs:nodejs package.json ./ | ||
| COPY --chown=bunjs:nodejs prisma ./prisma | ||
|
|
||
| USER bunjs | ||
|
|
||
| ENV NODE_ENV=production | ||
| ENV PORT=3000 | ||
| ENV PATH="/usr/local/bun/bin:$PATH" | ||
|
|
||
| EXPOSE 3000 | ||
|
|
||
| HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ | ||
| CMD curl -sf http://localhost:3000/health || exit 1 | ||
|
|
||
| CMD ["sh", "-c", "bun src/server.ts"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Local Development Environment Setup | ||
|
|
||
| This document explains how to set up and manage the local development environment for the COC-API project. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - **Docker & Docker Compose**: Ensure you have Docker installed and running. | ||
| - **PostgreSQL Client (`pg_dump`)**: The setup script uses `pg_dump` to pull data from the remote database. | ||
|
|
||
| ## Setup Instructions | ||
|
|
||
| ### 1. Configure Environment Variables | ||
|
|
||
| Copy the `.env.example` file to `.env` and fill in the required values: | ||
|
|
||
| ```bash | ||
| cp .env.example .env | ||
| ``` | ||
|
|
||
| Key variables for the setup script: | ||
| - `SESSION_POOLER`: The direct connection string to your Supabase project (Session Pooler / IPv4). | ||
| - **Format**: `postgresql://postgres.PROJECT_REF:PASSWORD@aws-0-us-east-1.pooler.supabase.com:5432/postgres` | ||
|
|
||
| ### 2. Run the Setup Script | ||
|
|
||
| The `scripts/setup-local.sh` script automates the entire process: | ||
|
|
||
| ```bash | ||
| bun run local | ||
| ``` | ||
|
|
||
| #### What the script does: | ||
| 1. **Starts Postgres**: Launches the `db` container. | ||
| 2. **Installs Extensions**: Pre-installs `pgcrypto`, `uuid-ossp`, and `pg_stat_statements` into the `public` schema. | ||
| 3. **Dumps Remote DB**: Uses `pg_dump` to create a snapshot of the production database. | ||
| 4. **Cleans the Dump**: Strips Supabase-specific extensions and patches schema references to work locally. | ||
| 5. **Seeds the Database**: Loads the cleaned dump into your local Postgres container. | ||
| 6. **Starts the API**: Launches the `api` container and waits for it to be healthy. | ||
|
|
||
| ### 3. Useful Commands | ||
|
|
||
| - **Start environment**: `bash scripts/setup-local.sh` | ||
| - **Skip dump (reuse existing `seed/dump.sql`)**: `bash scripts/setup-local.sh --skip-dump` | ||
| - **Skip seeding (just start containers)**: `bash scripts/setup-local.sh --skip-seed` | ||
| - **View logs**: `docker compose logs -f` | ||
| - **Stop containers**: `docker compose down` | ||
| - **Wipe local data (force re-seed)**: `docker compose down -v` | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### "Tenant or user not found" during dump | ||
| This usually means your `SESSION_POOLER` username is just `postgres`. Supabase requires the format `postgres.PROJECT_REF`. | ||
|
|
||
| ### Tables not in `public` schema | ||
| 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. | ||
|
|
||
| ### Re-seeding | ||
| 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. |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,43 @@ | ||||||||||||
| services: | ||||||||||||
| api: | ||||||||||||
| image: coc-api | ||||||||||||
| build: | ||||||||||||
| context: . | ||||||||||||
| dockerfile: Dockerfile | ||||||||||||
| ports: | ||||||||||||
| - "3000:3000" | ||||||||||||
| environment: | ||||||||||||
| NODE_ENV: development | ||||||||||||
| PORT: 3000 | ||||||||||||
| DATABASE_URL: "postgresql://postgres:example@db:5432/coc?sslmode=disable" | ||||||||||||
| volumes: | ||||||||||||
| - ./:/app:cached | ||||||||||||
| - /app/node_modules | ||||||||||||
| command: ["bun", "src/server.ts"] | ||||||||||||
| depends_on: | ||||||||||||
| - db | ||||||||||||
| healthcheck: | ||||||||||||
| test: ["CMD", "curl", "-sf", "http://localhost:3000/health"] | ||||||||||||
| interval: 30s | ||||||||||||
| timeout: 3s | ||||||||||||
| retries: 3 | ||||||||||||
|
shrutiiiyet marked this conversation as resolved.
|
||||||||||||
|
|
||||||||||||
| db: | ||||||||||||
| image: postgres:17 | ||||||||||||
| environment: | ||||||||||||
| POSTGRES_USER: postgres | ||||||||||||
| POSTGRES_PASSWORD: example | ||||||||||||
| POSTGRES_DB: coc | ||||||||||||
| volumes: | ||||||||||||
| - pgdata:/var/lib/postgresql/data | ||||||||||||
| ports: | ||||||||||||
| - "5432:5432" | ||||||||||||
| healthcheck: | ||||||||||||
| test: ["CMD", "pg_isready", "-U", "postgres", "-d", "coc", "-h", "127.0.0.1", "-p", "5432"] | ||||||||||||
| interval: 5s | ||||||||||||
| timeout: 5s | ||||||||||||
| retries: 5 | ||||||||||||
|
|
||||||||||||
| volumes: | ||||||||||||
| pgdata: | ||||||||||||
| seed-data: | ||||||||||||
|
Comment on lines
+43
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unused The 🧹 Remove unused volume volumes:
pgdata:
- seed-data:📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||
Uh oh!
There was an error while loading. Please reload this page.