-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (141 loc) · 3.83 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const requireg = require('requireg');
const assert = require('assert');
const camelcaseKeys = require('camelcase-keys');
const pgNamed = require('node-postgres-named');
let PGClient;
/**
* Helper to work with Postgres
*/
class Postgre extends Helper {
/**
*
* @param {object} config
*/
constructor(config) {
super(config);
PGClient = requireg('pg').Client;
this._validateConfig(config);
}
/**
*
* @param {object} config
* @private
*/
_validateConfig(config) {
this.options = {
default: {
port: 5432,
},
};
this.connections = {};
// override defaults with config
Object.assign(this.options, config);
if (!this.options.default || !this.options.default.host || !this.options.dbs) {
throw new Error(`
PG requires at host and dbs login/password to be set.
Check your codeceptjs config file to ensure this is set properly
{
"helpers": {
"postgre": {
"default" : {
"host": "YOUR_HOST",
"port": "YOUR_PORT",
"user": "YOUR_USER",
"password": "YOUR_PASSWORD"
},
"dbs": {
oms: { },
universeGatewayDb: {
"user": "UNIVERSE_USER",
"password": "UNIVERSE_PASSWORD"
}
}
}
}
}
`);
}
}
/**
*
* @returns {string[]}
* @private
*/
// eslint-disable-next-line consistent-return
static _checkRequirements() {
try {
requireg('pg');
} catch (e) {
return ['pg'];
}
// eslint-disable-next-line consistent-return
}
/**
* Open connect to db
* @param {string} dbName
* @returns {Promise<*>}
* @private
*/
_openConnect(dbName) {
if (this.options.dbs[dbName]) {
this.options.dbs[dbName] = { ...this.options.default, ...this.options.dbs[dbName] };
if (typeof this.options.dbs[dbName].user === 'string' && typeof this.options.dbs[dbName].password === 'string') {
this.connections[dbName] = new PGClient({
user: this.options.dbs[dbName].user,
host: this.options.dbs[dbName].host,
database: dbName,
password: this.options.dbs[dbName].password,
port: this.options.dbs[dbName].port,
});
pgNamed.patch(this.connections[dbName]);
return this.connections[dbName].connect();
}
throw new Error(`There is no user or password for DB ${dbName} in config`);
}
throw new Error(`There is no DB ${dbName} in config`);
}
/**
* Run query
* @param {string} query
* @param [{ [key: string]: any }] params
* @param {string} dbName
* @returns {Promise<*>}
*/
query(query, params = {}, dbName) {
if (this.connections[dbName]) {
return this.connections[dbName].query(query, params)
.then((res) => {
if (res.command === 'SELECT') {
return camelcaseKeys(res.rows);
}
if (typeof res.rows !== 'undefined') {
res.rows = camelcaseKeys(res.rows)
}
return camelcaseKeys(res);
})
.catch((err) => assert.fail(err));
}
return this._openConnect(dbName)
.then(() => this.connections[dbName].query(query, params)
.then((res) => {
if (res.command === 'SELECT') {
return camelcaseKeys(res.rows);
}
if (typeof res.rows !== 'undefined') {
res.rows = camelcaseKeys(res.rows)
}
return camelcaseKeys(res);
})
.catch((err) => assert.fail(err)));
}
/**
*
* @private
*/
_finishTest() {
Object.keys(this.connections).forEach((connect) => {
this.connections[connect].end();
});
}
}
module.exports = Postgre;