2016-10-16 02:59:54 +05:30
|
|
|
const parser = require('./PostgresConfigParser');
|
|
|
|
|
|
|
|
|
|
export function createClient(uri, databaseOptions) {
|
|
|
|
|
let dbOptions = {};
|
|
|
|
|
databaseOptions = databaseOptions || {};
|
|
|
|
|
|
|
|
|
|
if (uri) {
|
|
|
|
|
dbOptions = parser.getDatabaseOptionsFromURI(uri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const key in databaseOptions) {
|
|
|
|
|
dbOptions[key] = databaseOptions[key];
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-07 19:50:55 +08:00
|
|
|
const initOptions = dbOptions.initOptions || {};
|
2019-12-03 18:21:12 -06:00
|
|
|
initOptions.noWarnings = process && process.env.TESTING;
|
|
|
|
|
|
2017-04-07 19:50:55 +08:00
|
|
|
const pgp = require('pg-promise')(initOptions);
|
2016-12-07 15:17:05 -08:00
|
|
|
const client = pgp(dbOptions);
|
2016-10-16 02:59:54 +05:30
|
|
|
|
|
|
|
|
if (dbOptions.pgOptions) {
|
|
|
|
|
for (const key in dbOptions.pgOptions) {
|
2017-04-07 19:50:55 +08:00
|
|
|
pgp.pg.defaults[key] = dbOptions.pgOptions[key];
|
2016-10-16 02:59:54 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-16 12:06:17 -04:00
|
|
|
return { client, pgp };
|
2016-10-16 02:59:54 +05:30
|
|
|
}
|