-
Notifications
You must be signed in to change notification settings - Fork 0
feat: store lexicons in db #4
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
Open
caidanw
wants to merge
14
commits into
main
Choose a base branch
from
caidanw/atm-199-store-lexicons-in-db
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
50785f2
feat: add postgres to docker compose
caidanw 234797e
refactor: nexus service to use persistent volume
caidanw 3d5b28a
feat: add .dockerignore for better lexhub image builds
caidanw 3a7cd0e
feat: add drizzle and postgres dependencies
caidanw 3fd70f4
feat: setup drizzle
caidanw 57e1e21
feat: add initial db schema
caidanw 5372d99
feat: drizzle generate init migration
caidanw 361e169
feat: add trigger to update timestamp on the lexicons.updated_at column
caidanw 77b30ff
feat: simplify the Dockerfile and add an entrypoint script
caidanw eeeeede
refactor: Dockerfile to use multi-stage build for smaller image size
caidanw ff76848
feat(api): filter and type Nexus events, only process 'record' events…
caidanw 7cd0bb3
feat(api): expand RecordEvent type and add ack/retry helpers to inges…
caidanw a79bbb3
feat(api): ingest route upserts lexicon records to db and handles id/…
caidanw 96f3a61
chore: add .env.example and update .gitignore to allow example env file
caidanw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # Dependencies | ||
| node_modules | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
| # Next.js | ||
| .next | ||
| out | ||
| build | ||
| dist | ||
|
|
||
| # Testing | ||
| coverage | ||
| .nyc_output | ||
|
|
||
| # Misc | ||
| .DS_Store | ||
| *.pem | ||
| .env | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
|
|
||
| # Debug | ||
| *.log | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
| # Docker | ||
| Dockerfile | ||
| *.Dockerfile | ||
| .dockerignore | ||
| docker-compose.yml | ||
| compose.yaml | ||
| !docker-entrypoint.sh | ||
|
|
||
| # Git | ||
| .git | ||
| .gitignore | ||
| .gitattributes | ||
|
|
||
| # IDE | ||
| .vscode | ||
| .idea | ||
| *.swp | ||
| *.swo | ||
| *~ | ||
|
|
||
| # Documentation | ||
| README.md | ||
| CHANGELOG.md | ||
| LICENSE | ||
|
|
||
| # CI/CD | ||
| .github | ||
| .gitlab-ci.yml | ||
|
|
||
| # Database | ||
| *.db | ||
| *.sqlite | ||
| data/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Database Configuration | ||
| DATABASE_URL=postgresql://lexhub:lexhub_password@localhost:5432/lexhub | ||
|
|
||
| # Next.js Configuration | ||
| NODE_ENV=development | ||
| PORT=10000 | ||
|
|
||
| # Nexus Configuration (if needed for local development) | ||
| NEXUS_URL=http://localhost:8080 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,61 +1,58 @@ | ||
| # Build stage | ||
| FROM node:20-alpine AS builder | ||
|
|
||
| # Install dependencies needed for build | ||
| RUN apk add --no-cache libc6-compat | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Copy package files | ||
| COPY package.json package-lock.json* ./ | ||
|
|
||
| # Install dependencies | ||
| RUN apk add --no-cache libc6-compat | ||
| COPY package.json package-lock.json* ./ | ||
| RUN npm ci | ||
|
|
||
| # Copy source code | ||
| # Copy source and build | ||
| COPY . . | ||
|
|
||
| # Set environment to production | ||
| ENV NODE_ENV=production | ||
| ENV NEXT_TELEMETRY_DISABLED=1 | ||
|
|
||
| # Build the application | ||
| RUN npm run build | ||
|
|
||
| # Production stage | ||
| # Runtime stage | ||
| FROM node:20-alpine AS runner | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Install runtime dependencies | ||
| RUN apk add --no-cache curl | ||
| RUN apk add --no-cache curl postgresql-client | ||
|
|
||
| # Create a non-root user | ||
| # Create non-root user | ||
| RUN addgroup --system --gid 1001 nodejs && \ | ||
| adduser --system --uid 1001 nextjs | ||
|
|
||
| # Copy necessary files from builder | ||
| COPY --from=builder /app/public ./public | ||
| # Copy standalone output and static files | ||
| COPY --from=builder /app/.next/standalone ./ | ||
| COPY --from=builder /app/.next/static ./.next/static | ||
| COPY --from=builder /app/public ./public | ||
|
|
||
| # Copy migration files and dependencies for drizzle-kit | ||
| COPY --from=builder /app/drizzle ./drizzle | ||
| COPY --from=builder /app/drizzle.config.ts ./drizzle.config.ts | ||
| COPY --from=builder /app/src/db ./src/db | ||
| COPY --from=builder /app/node_modules ./node_modules | ||
| COPY --from=builder /app/package.json ./package.json | ||
|
|
||
| # Copy entrypoint script | ||
| COPY --from=builder /app/docker-entrypoint.sh ./docker-entrypoint.sh | ||
| RUN chmod +x ./docker-entrypoint.sh | ||
|
|
||
| # Set ownership to nextjs user | ||
| # Set ownership | ||
| RUN chown -R nextjs:nodejs /app | ||
|
|
||
| # Switch to non-root user | ||
| USER nextjs | ||
|
|
||
| # Expose the port the app runs on | ||
| EXPOSE 10000 | ||
|
|
||
| # Set environment variables | ||
| ENV NODE_ENV=production | ||
| ENV PORT=10000 | ||
| ENV HOSTNAME="0.0.0.0" | ||
|
|
||
| # Health check | ||
| HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ | ||
| CMD curl -f http://localhost:10000/ || exit 1 | ||
|
|
||
| # Start the application | ||
| CMD ["node", "server.js"] | ||
| ENTRYPOINT ["/app/docker-entrypoint.sh"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #!/usr/bin/env sh | ||
| set -e | ||
|
|
||
| echo "Starting LexHub application..." | ||
|
|
||
| # Wait for PostgreSQL to be ready | ||
| echo "Waiting for PostgreSQL to be ready..." | ||
| until pg_isready -h "$POSTGRES_HOST" -p "$POSTGRES_PORT" -U "$POSTGRES_USER"; do | ||
| echo "PostgreSQL is unavailable - sleeping" | ||
| sleep 2 | ||
| done | ||
| echo "PostgreSQL is ready!" | ||
|
|
||
| # Run database migrations | ||
| echo "Running database migrations..." | ||
| npm run db:migrate | ||
|
|
||
| # Start the Next.js application | ||
| exec node server.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { defineConfig } from "drizzle-kit"; | ||
|
|
||
| export default defineConfig({ | ||
| dialect: "postgresql", | ||
| schema: "./src/db/schema.ts", | ||
| out: "./drizzle", | ||
| dbCredentials: { | ||
| url: | ||
| process.env.DATABASE_URL || | ||
| "postgresql://lexhub:lexhub_password@localhost:5432/lexhub", | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| CREATE TABLE "lexicons" ( | ||
| "id" varchar(317) NOT NULL, | ||
| "cid" varchar(100) NOT NULL, | ||
| "data" jsonb NOT NULL, | ||
| "created_at" timestamp with time zone DEFAULT now() NOT NULL, | ||
| "updated_at" timestamp with time zone DEFAULT now() NOT NULL, | ||
| CONSTRAINT "lexicons_id_cid_pk" PRIMARY KEY("id","cid") | ||
| ); | ||
| --> statement-breakpoint | ||
| CREATE INDEX "lexicons_id_idx" ON "lexicons" USING btree ("id");--> statement-breakpoint | ||
| CREATE INDEX "lexicons_created_at_idx" ON "lexicons" USING btree ("created_at");--> statement-breakpoint | ||
| CREATE INDEX "lexicons_data_gin_idx" ON "lexicons" USING gin ("data");--> statement-breakpoint | ||
|
|
||
|
|
||
| CREATE OR REPLACE FUNCTION update_updated_at_column() | ||
| RETURNS TRIGGER AS $$ | ||
| BEGIN | ||
| NEW.updated_at = NOW(); | ||
| RETURN NEW; | ||
| END; | ||
| $$ language 'plpgsql'; | ||
|
|
||
| CREATE TRIGGER update_lexicons_updated_at | ||
| BEFORE UPDATE ON "lexicons" | ||
| FOR EACH ROW | ||
| EXECUTE FUNCTION update_updated_at_column(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Do we want to use 18 which is the current version and has some performance improvement?
In subsequent versions of postgres the mount directory is changed to:
/var/lib/postgresql, https://github.com/docker-library/docs/blob/master/postgres/README.md#pgdata, potentially save some debugging down the line should people upgrade the db version.