Skip to content

Commit

Permalink
♻️ chore(api): extract buildPostgresEnvironment function
Browse files Browse the repository at this point in the history
Co-Authored-By: GUL <guillaume.lagorce@pix.fr>
  • Loading branch information
yaf and HEYGUL committed Dec 26, 2024
1 parent 2370bc4 commit 1d0242a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 79 deletions.
48 changes: 9 additions & 39 deletions api/datamart/knexfile.js
Original file line number Diff line number Diff line change
@@ -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;
58 changes: 18 additions & 40 deletions api/db/knexfile.js
Original file line number Diff line number Diff line change
@@ -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;
20 changes: 20 additions & 0 deletions api/db/utils/build-postgres-environment.js
Original file line number Diff line number Diff line change
@@ -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',
};
}

0 comments on commit 1d0242a

Please sign in to comment.