@coko/server
Version:
Reusable server for use by Coko's projects
49 lines • 2.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = __importDefault(require("../configManager/config"));
const getDbConnectionConfig = (key = 'db') => {
/* eslint-disable-next-line prefer-const */
let { allowSelfSignedCertificates, caCert, ...connectionConfig } = (config_1.default.has(key) && config_1.default.get(key)) || {};
// clone to get around an issue of knex deleting password from the original object
let connection = { ...connectionConfig };
// Fallback to the values of db for the keys that are not defined
if (key !== 'db') {
const { allowSelfSignedCertificates: allowSelfSignedCertificatesDefault, caCert: caCertDefault, ...connectionConfigDefault } = config_1.default.get('db');
connection = {
...connectionConfigDefault,
...Object.fromEntries(
// make sure empty properties do not overwrite existing properties
Object.entries(connection).filter(([_k, v]) => {
return !!v;
})),
};
if (typeof allowSelfSignedCertificates === 'undefined') {
allowSelfSignedCertificates = allowSelfSignedCertificatesDefault;
}
caCert = caCert || caCertDefault;
}
if (allowSelfSignedCertificates) {
if (!connection.ssl)
connection.ssl = {};
connection.ssl.rejectUnauthorized = false;
}
if (caCert) {
if (!connection.ssl)
connection.ssl = {};
connection.ssl.rejectUnauthorized = true;
/**
* The value of the env variable should be the base64 encoded crt file.
* eg. the result of `base64 -w0 ca-certificate.crt`
* It gets decoded here. This is to prevent issues with newlines when trying
* to pass the contents of a cert file as an environment variable in some
* deployment environments.
*/
connection.ssl.ca = Buffer.from(caCert, 'base64').toString('utf-8');
}
return connection;
};
exports.default = getDbConnectionConfig;
//# sourceMappingURL=connectionConfig.js.map