/
connection.js
46 lines (37 loc) · 1.06 KB
/
connection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
const nconf = require('nconf');
const winston = require('winston');
const _ = require('lodash');
const connection = module.exports;
connection.getConnectionOptions = function (postgres) {
postgres = postgres || nconf.get('postgres');
// Sensible defaults for PostgreSQL, if not set
if (!postgres.host) {
postgres.host = '127.0.0.1';
}
if (!postgres.port) {
postgres.port = 5432;
}
const dbName = postgres.database;
if (dbName === undefined || dbName === '') {
winston.warn('You have no database name, using "nodebb"');
postgres.database = 'nodebb';
}
var connOptions = {
host: postgres.host,
port: postgres.port,
user: postgres.username,
password: postgres.password,
database: postgres.database,
ssl: String(postgres.ssl) === 'true',
};
return _.merge(connOptions, postgres.options || {});
};
connection.connect = function (options, callback) {
const Pool = require('pg').Pool;
const connOptions = connection.getConnectionOptions(options);
const db = new Pool(connOptions);
db.connect(function (err) {
callback(err, db);
});
};