-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver-db2.js
258 lines (219 loc) · 6.28 KB
/
server-db2.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
require('dotenv').config(); // used to get COLUMN_IDS during local dev
const encUtil = require('./encrypt-util');
const logger = require('winston');
const rp = require('request-promise');
const dashApi = '/dashdb-api/v2';
const db = 'BLUDB';
logger.info(`Column IDs specified ${process.env.COLUMN_IDS}`);
const columnIds = process.env.COLUMN_IDS ? process.env.COLUMN_IDS.split(',') : [];
function getUsage(name, type) {
// check if the column name is whitelisted as an ID
if (columnIds.find(id => id === name)) {
return 'identifier';
}
// fall back to type inference
switch (type) {
case 'VARCHAR':
return 'identifier';
case 'BIGINT':
case 'DECIMAL':
case 'DOUBLE':
case 'INTEGER':
case 'NUMERIC':
case 'SMALLINT':
case 'TINYINT':
return 'fact';
default:
return 'automatic';
}
}
class DB2 {
constructor(host, user, password, schema) {
this.host = host;
this.user = user;
this.password = password;
this.schema = schema;
}
async authenticate() {
logger.verbose('Requesting authentication token');
const uri = `https://${this.host}${dashApi}/auth`;
const response = await rp({
method: 'POST',
uri,
body: {
userid: this.user,
password: this.password,
},
json: true,
});
this.token = response.token;
if (this.token) {
logger.verbose('Successfully obtained token');
try {
setTimeout(() => this.authenticate(), 3600 * 1000);
} catch (e) {
logger.error(e);
}
}
return response;
}
async getDatasource(id, name, table) {
const { comments, columns: tableCols } = await this.getTableInfo(table);
// map the tableCols to DDE format
const column = tableCols.map(tableCol => (
{
name: tableCol.name,
description: tableCol.comments,
datatype: `${tableCol.type_name}(${tableCol.type_precision})`,
nullable: tableCol.nullable === 'yes',
label: tableCol.name.toLowerCase(),
usage: getUsage(tableCol.name, tableCol.type_name),
regularAggregate: 'countDistinct',
}
));
const source = {
id,
name,
module: {
xsd: 'https://ibm.com/daas/module/1.0/module.xsd',
source: {
id: `${id}_source`,
jdbc: {
jdbcUrl: `jdbc:db2://${this.host}:50000/${db}`,
driverClassName: 'com.ibm.db2.jcc.DB2Driver',
schema: this.schema,
},
user: this.user,
password: this.password,
},
table: {
name: table,
description: comments,
column,
},
label: `${name} module`,
identifier: `${id}_module`,
},
shaping: {
embeddedModuleUpToDate: true,
},
};
return source;
}
getTableInfo(table) {
const uri = `https://${this.host}${dashApi}/schemas/${this.schema}/tables/${table}`;
return rp({
uri,
headers: {
Authorization: `Bearer ${this.token}`,
},
json: true,
});
}
getTables() {
const uri = `https://${this.host}${dashApi}/schemas/${this.schema}/tables`;
return rp({
uri,
headers: {
Authorization: `Bearer ${this.token}`,
},
json: true,
});
}
getSqlResult(jobId) {
return rp({
uri: `https://${this.host}${dashApi}/sql_jobs/${jobId}`,
headers: {
Authorization: `Bearer ${this.token}`,
},
json: true,
});
}
runSql(body) {
logger.debug(`Executing SQL ${JSON.stringify(body)}`);
return rp({
method: 'POST',
uri: `https://${this.host}${dashApi}/sql_jobs`,
headers: {
Authorization: `Bearer ${this.token}`,
},
body,
json: true,
});
}
}
exports.configure = (app, service) => {
logger.info('Configuring IBM DB2 Warehouse middleware');
const {
hostname,
username,
password,
} = service.credentials;
const db2 = new DB2(hostname, username, password, username.toUpperCase());
// TOOD handle token expiration
db2.authenticate()
.then(res => logger.verbose(`DB2 connection successful ${res.token !== undefined}`))
.catch(err => logger.error(`DB2 error ${err}`));
// gets a list of tables in DB2 that could act as a datasource
app.get('/api/db2/tables', (request, response) => {
db2.getTables()
.then(res => response.json(res));
});
// returns a specific table (all rows and cols) as a datasource
app.get('/api/db2/tables/:name', (request, response) => {
const { name } = request.params;
db2.getDatasource(name, name, name)
.then((datasource) => {
// if the DDE session is set, we can encrypt datasource information
const { ddeSession } = request.session;
if (ddeSession) {
logger.verbose('Encrypting datasource');
const encDatasource = encUtil(ddeSession, datasource);
response.json(encDatasource);
} else {
response.json(datasource);
}
})
.catch(err => response.json(err));
});
// execute a SQL job per a REST api
app.post('/api/db2/sql', (request, response) => {
if (request.user) {
const { email } = request.user;
logger.verbose(`Replacing all occurences of %e with ${email}`);
request.body.commands = request.body.commands.replace(/%e/g, email);
}
db2.runSql(request.body)
.then(res => response.json(res))
.catch(err => response.json(err));
});
/**
* Gets the result of a SQL job and returns data as a CSV.
*/
app.get('/api/db2/sql/:jobId', (request, response) => {
db2.getSqlResult(request.params.jobId)
.then((res) => {
// the SQL job is finished
if (res.status === 'completed') {
const { columns, rows } = res.results[0];
// flatten the columns and rows into a CSV file
let csv = `${columns.join(',')}\n`;
rows.forEach((row) => {
csv += `${row.join(',')}\n`;
});
logger.debug(`Sending CSV data\n${csv}`);
response.send(csv);
} else {
response.send('');
}
})
.catch((err) => {
// return a portion of the error to user
const { statusCode, message } = err;
response.json({
statusCode,
message,
});
});
});
};