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
|
|
|
|
2021-03-10 13:31:35 -06:00
|
|
|
if (process.env.PARSE_SERVER_LOG_LEVEL === 'debug') {
|
|
|
|
|
const monitor = require('pg-monitor');
|
2021-03-13 09:05:22 -06:00
|
|
|
try {
|
|
|
|
|
monitor.attach(initOptions);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
monitor.detach();
|
|
|
|
|
monitor.attach(initOptions);
|
|
|
|
|
}
|
2021-03-10 13:31:35 -06:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|