From 1d0242aa6f7ab7738f967fbcf3b8342394f1fc7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yannick=20Fran=C3=A7ois?= Date: Thu, 26 Dec 2024 11:05:31 +0100 Subject: [PATCH] :recycle: chore(api): extract buildPostgresEnvironment function Co-Authored-By: GUL --- api/datamart/knexfile.js | 48 ++++-------------- api/db/knexfile.js | 58 +++++++--------------- api/db/utils/build-postgres-environment.js | 20 ++++++++ 3 files changed, 47 insertions(+), 79 deletions(-) create mode 100644 api/db/utils/build-postgres-environment.js diff --git a/api/datamart/knexfile.js b/api/datamart/knexfile.js index 7b5dcd50463..d33d8a3cc12 100644 --- a/api/datamart/knexfile.js +++ b/api/datamart/knexfile.js @@ -1,51 +1,21 @@ import * as url from 'node:url'; const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); import * as dotenv from 'dotenv'; +import { buildPostgresEnvironment } from '../db/utils/build-postgres-environment.js'; dotenv.config({ path: `${__dirname}/../.env` }); -function localPostgresEnv(databaseUrl, knexAsyncStacktraceEnabled) { - return { - client: 'postgresql', - connection: databaseUrl, - pool: { - min: 1, - max: 4, - }, - migrations: { - tableName: 'knex_migrations', - directory: './migrations', - loadExtensions: ['.js'], - }, - seeds: { - directory: './seeds', - loadExtensions: ['.js'], - asyncStackTraces: knexAsyncStacktraceEnabled !== 'false', - }, - }; +const baseConfiguration = { + migrationsDirectory: './migrations/', + seedsDirectory: './seeds/', + databaseUrl: process.env.DATAMART_DATABASE_URL, } + const environments = { - development: localPostgresEnv(process.env.DATAMART_DATABASE_URL, process.env.KNEX_ASYNC_STACKTRACE_ENABLED), + development: buildPostgresEnvironment(baseConfiguration), - test: localPostgresEnv(process.env.TEST_DATAMART_DATABASE_URL, process.env.KNEX_ASYNC_STACKTRACE_ENABLED), + test: buildPostgresEnvironment({ ...baseConfiguration, databaseUrl: process.env.TEST_DATAMART_DATABASE_URL }), - production: { - client: 'postgresql', - connection: process.env.DATAMART_DATABASE_URL, - pool: { - min: 1, - max: 4, - }, - migrations: { - tableName: 'knex_migrations', - directory: './migrations', - loadExtensions: ['.js'], - }, - seeds: { - directory: './seeds', - loadExtensions: ['.js'], - }, - asyncStackTraces: process.env.KNEX_ASYNC_STACKTRACE_ENABLED !== 'false', - }, + production: buildPostgresEnvironment(baseConfiguration) }; export default environments; diff --git a/api/db/knexfile.js b/api/db/knexfile.js index 80eba977ff4..e362f0005c3 100644 --- a/api/db/knexfile.js +++ b/api/db/knexfile.js @@ -1,51 +1,29 @@ import * as url from 'node:url'; const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); import * as dotenv from 'dotenv'; + +import { buildPostgresEnvironment } from './utils/build-postgres-environment.js'; dotenv.config({ path: `${__dirname}/../.env` }); -function localPostgresEnv(databaseUrl, knexAsyncStacktraceEnabled) { - return { - client: 'postgresql', - connection: databaseUrl, - pool: { - min: 1, - max: 4, - }, - migrations: { - tableName: 'knex_migrations', - directory: './migrations', - loadExtensions: ['.js'], - }, - seeds: { - directory: './seeds', - loadExtensions: ['.js'], - }, - asyncStackTraces: knexAsyncStacktraceEnabled !== 'false', - }; +const baseConfiguration = { + migrationsDirectory: './migrations/', + seedsDirectory: './seeds/', + databaseUrl: process.env.DATABASE_URL, } -const environments = { - development: localPostgresEnv(process.env.DATABASE_URL, process.env.KNEX_ASYNC_STACKTRACE_ENABLED), - test: localPostgresEnv(process.env.TEST_DATABASE_URL, process.env.KNEX_ASYNC_STACKTRACE_ENABLED), +export default { + development: buildPostgresEnvironment(baseConfiguration), - production: { - client: 'postgresql', - connection: process.env.DATABASE_URL, + test: buildPostgresEnvironment({ + ...baseConfiguration, + databaseUrl: process.env.TEST_DATABASE_URL, + }), + + production: buildPostgresEnvironment({ + ...baseConfiguration, pool: { - min: parseInt(process.env.DATABASE_CONNECTION_POOL_MIN_SIZE, 10) || 1, - max: parseInt(process.env.DATABASE_CONNECTION_POOL_MAX_SIZE, 10) || 4, - }, - migrations: { - tableName: 'knex_migrations', - directory: './migrations', - loadExtensions: ['.js'], + min: parseInt(process.env.DATABASE_CONNECTION_POOL_MIN_SIZE, 10), + max: parseInt(process.env.DATABASE_CONNECTION_POOL_MAX_SIZE, 10), }, - seeds: { - directory: './seeds', - loadExtensions: ['.js'], - }, - asyncStackTraces: process.env.KNEX_ASYNC_STACKTRACE_ENABLED !== 'false', - }, + }), }; - -export default environments; diff --git a/api/db/utils/build-postgres-environment.js b/api/db/utils/build-postgres-environment.js new file mode 100644 index 00000000000..0c17a0df0da --- /dev/null +++ b/api/db/utils/build-postgres-environment.js @@ -0,0 +1,20 @@ +export function buildPostgresEnvironment({ databaseUrl, pool, migrationsDirectory, seedsDirectory }) { + return { + client: 'postgresql', + connection: databaseUrl, + pool: { + min: pool?.min || 1, + max: pool?.max || 4, + }, + migrations: { + tableName: 'knex_migrations', + directory: migrationsDirectory, + loadExtensions: ['.js'], + }, + seeds: { + directory: seedsDirectory, + loadExtensions: ['.js'], + }, + asyncStackTraces: process.env.KNEX_ASYNC_STACKTRACE_ENABLED !== 'false', + }; +}