Skip to content

Commit

Permalink
fix: get db url from DATABASE_URL env var in worker (#294)
Browse files Browse the repository at this point in the history
# What ❔

Get DB URL from DATABASE_URL env var in the Worker service.

## Why ❔

To make it consistent with the API service and simplify infrastructure
automation.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [X] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
  • Loading branch information
vasyl-ivanchuk authored Oct 14, 2024
1 parent a838825 commit 305401c
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/worker/src/typeorm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import { config } from "dotenv";

config();

let dbUrl = process.env.DATABASE_URL;
if (!dbUrl) {
const host = process.env.DATABASE_HOST || "localhost";
const port = parseInt(process.env.DATABASE_PORT) || 5432;
const username = process.env.DATABASE_USER || "postgres";
const password = process.env.DATABASE_PASSWORD || "postgres";
const database = process.env.DATABASE_NAME || "block-explorer";
dbUrl = `postgres://${username}:${password}@${host}:${port}/${database}`;
}

export const typeOrmModuleOptions: DataSourceOptions = {
type: "postgres",
host: process.env.DATABASE_HOST || "localhost",
port: parseInt(process.env.DATABASE_PORT) || 5432,
username: process.env.DATABASE_USER || "postgres",
password: process.env.DATABASE_PASSWORD || "postgres",
database: process.env.DATABASE_NAME || "block-explorer",
url: dbUrl,
poolSize: parseInt(process.env.DATABASE_CONNECTION_POOL_SIZE, 10) || 100,
extra: {
idleTimeoutMillis: parseInt(process.env.DATABASE_CONNECTION_IDLE_TIMEOUT_MS, 10) || 12000,
Expand Down

0 comments on commit 305401c

Please sign in to comment.