Skip to content

Commit

Permalink
Add postgresGCP config and toggle to allow switching (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
varney authored Mar 2, 2023
1 parent 728c497 commit 5e49bb1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
13 changes: 13 additions & 0 deletions config/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,18 @@
"database": "entity_test",
"max": 10,
"statement_timeout": 10000
},
"postgresGCP": {
"user": "et",
"password": "ET",
"host": "localhost",
"port": "5435",
"database": "entity_test",
"max": 10,
"statement_timeout": 10000,
"ssl": false
},
"toggle": {
"useGCPPostgres": false
}
}
21 changes: 14 additions & 7 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

const config = require("exp-config");
const pg = require("pg");
const toggle = require("./toggle");

const {user, password, database, host, port, statementTimeout, ssl, max = 10} = toggle("useGCPPostgres")
? config.postgresGCP
: config.postgres;

const poolConfig = {
user: config.postgres.user,
password: config.postgres.password,
database: config.postgres.database,
host: config.postgres.host,
port: config.postgres.port,
max: config.postgres.max || 10,
statement_timeout: config.postgres.statement_timeout || 10000 // eslint-disable-line
user,
password,
database,
host,
port,
ssl,
max,
// eslint-disable-next-line camelcase
statement_timeout: statementTimeout || 10000
};

let pool;
Expand Down
23 changes: 23 additions & 0 deletions lib/toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";
const config = require("exp-config");

const knownToggles = ["useGCPPostgres"];

knownToggles.sort();

config.toggle = config.toggle || {};

function toggle(name) {
if (knownToggles.indexOf(name) === -1) {
throw new Error(`Unknown toggle '${name}'`);
}
if (process.env.NODE_ENV === "test") {
return true;
}
const value = config.toggle[name];
return value === true || value === "true";
}

toggle.knownToggles = knownToggles;

module.exports = toggle;

0 comments on commit 5e49bb1

Please sign in to comment.