diff --git a/api/datamart/knexfile.js b/api/datamart/knexfile.js index 7b5dcd50463..d186f59b294 100644 --- a/api/datamart/knexfile.js +++ b/api/datamart/knexfile.js @@ -1,51 +1,22 @@ 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..7a6eb650ef3 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 environments = { - development: localPostgresEnv(process.env.DATABASE_URL, process.env.KNEX_ASYNC_STACKTRACE_ENABLED), +const baseConfiguration = { + migrationsDirectory: './migrations/', + seedsDirectory: './seeds/', + databaseUrl: process.env.DATABASE_URL, +}; + +export default { + development: buildPostgresEnvironment(baseConfiguration), - test: localPostgresEnv(process.env.TEST_DATABASE_URL, process.env.KNEX_ASYNC_STACKTRACE_ENABLED), + test: buildPostgresEnvironment({ + ...baseConfiguration, + databaseUrl: process.env.TEST_DATABASE_URL, + }), - production: { - client: 'postgresql', - connection: process.env.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', + }; +}