-
Notifications
You must be signed in to change notification settings - Fork 263
/
response.js
238 lines (215 loc) · 7.95 KB
/
response.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
let createClient = (() => {
var _ref3 = _asyncToGenerator(function*(db, credentials) {
if (db === 'postgres') return yield createPostgresClient(credentials)
if (db === 'bigquery') return yield createBigQueryClient(credentials)
if (db === 'mysql') return yield createMySQLClient(credentials)
throw new Error('database ' + db + ' not recognized')
})
return function createClient(_x4, _x5) {
return _ref3.apply(this, arguments)
}
})()
let createMySQLClient = (() => {
var _ref4 = _asyncToGenerator(function*(credentials) {
const client = yield mysql.createConnection(credentials)
return {
query(sql) {
return _asyncToGenerator(function*() {
const [rows, fields] = yield client.execute(sql)
console.log(rows, fields)
if (fields) {
const field_list = fields.map(function(k) {
return k.name
})
return {
columns: field_list,
values: rows.map(function(row) {
return field_list.map(function(k) {
return row[k]
})
}),
}
} else {
return {
columns: ['result'],
values: [[rows]],
}
}
})()
},
close() {
return _asyncToGenerator(function*() {
return yield client.end()
})()
},
}
})
return function createMySQLClient(_x6) {
return _ref4.apply(this, arguments)
}
})()
let createPostgresClient = (() => {
var _ref5 = _asyncToGenerator(function*(credentials) {
const client = new PostgresClient(credentials)
;[1082, 1114, 1184].forEach(function(oid) {
return client.setTypeParser(oid, function(val) {
return val
})
})
yield client.connect()
return {
query(sql) {
return _asyncToGenerator(function*() {
let results = yield client.query({
text: sql,
rowMode: 'array',
})
if (Array.isArray(results)) {
results = results[results.length - 1]
}
// console.log(results.rows, results)
if (results.rows.length > 10000)
throw new Error(
'Too many result rows to serialize: Try using a LIMIT statement.'
)
return results
})()
},
close: client.end.bind(client),
}
})
return function createPostgresClient(_x7) {
return _ref5.apply(this, arguments)
}
})()
function _asyncToGenerator(fn) {
return function() {
var gen = fn.apply(this, arguments)
return new Promise(function(resolve, reject) {
function step(key, arg) {
try {
var info = gen[key](arg)
var value = info.value
} catch (error) {
reject(error)
return
}
if (info.done) {
resolve(value)
} else {
return Promise.resolve(value).then(
function(value) {
step('next', value)
},
function(err) {
step('throw', err)
}
)
}
}
return step('next')
})
}
}
const { Client: PostgresClient } = require('pg')
const mysql = require('mysql2/promise')
const BigQueryClient = require('@google-cloud/bigquery')
const tmp = require('tmp')
const fs = require('fs')
const credentials = require('./credentials.js')
const localCtx = {}
module.exports = (() => {
var _ref = _asyncToGenerator(function*(message, ctx = localCtx) {
const { action, id } = message
try {
if (action === 'open') {
const { credentials, db } = message
ctx.client = yield createClient(db, credentials)
return { ready: true }
} else if (action === 'exec') {
const { sql } = message
const results = yield ctx.client.query(sql, message)
return { results }
} else if (action === 'close') {
yield ctx.client.close()
return { closed: true }
} else if (action == 'get_postgres_credentials') {
return credentials
} else if (action == 'get_bigquery_schema') {
const get = (() => {
var _ref2 = _asyncToGenerator(function*(o, prop, ...rest) {
return typeof prop === 'undefined'
? o
: typeof o[prop] === 'function'
? get(yield o[prop](), ...rest)
: Array.isArray(o[prop])
? Promise.all(
o[prop].map(function(sub) {
return get(sub, ...rest)
})
)
: typeof prop === 'function'
? get(yield prop(o), ...rest)
: new Error('not found: ' + o + ' ' + prop)
})
return function get(_x2, _x3) {
return _ref2.apply(this, arguments)
}
})()
const flatten = function(arr, result = []) {
arr.forEach(function(value) {
return Array.isArray(value) ? flatten(value, result) : result.push(value)
})
return result
}
const raw = yield get(
ctx.client,
'getDatasets',
0,
'getTables',
0,
'getMetadata',
function(metadata) {
return metadata[0]
}
)
const schema = flatten(raw).map(function(table) {
return {
schema: table.tableReference.datasetId,
name: table.tableReference.tableId,
columns: table.schema.fields.map(function(f) {
return f.name
}),
}
})
return { schema }
} else {
throw new Error('Unknown action: ' + action)
}
} catch (e) {
console.log(e)
return { error: e.message || e.stack.split('\n')[0] }
}
})
function response(_x) {
return _ref.apply(this, arguments)
}
return response
})()
function createBigQueryClient(credentials) {
if (credentials.keyFile) {
const { name, data } = credentials.keyFile
const { name: keyFilename, fd } = tmp.fileSync({ postfix: name })
fs.writeFileSync(fd, Buffer.from(data, 'hex'))
credentials.keyFilename = keyFilename
}
console.log(credentials)
const client = new BigQueryClient(credentials)
return {
query: (sql, { useLegacySql }) => client.query({ query: sql, useLegacySql }),
getDatasets: () => client.getDatasets(),
close() {
console.log('no bigquery close method')
},
}
}