From 305401cbfca04aadca62b147d556a1060f333ab3 Mon Sep 17 00:00:00 2001 From: Vasyl Ivanchuk Date: Mon, 14 Oct 2024 14:25:12 +0300 Subject: [PATCH] fix: get db url from DATABASE_URL env var in worker (#294) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 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 - [X] PR title corresponds to the body of PR (we generate changelog entries from PRs). --- packages/worker/src/typeorm.config.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/worker/src/typeorm.config.ts b/packages/worker/src/typeorm.config.ts index d08f3f9e76..7439bde751 100644 --- a/packages/worker/src/typeorm.config.ts +++ b/packages/worker/src/typeorm.config.ts @@ -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,