forked from jeremydaly/data-api-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
521 lines (430 loc) · 17.1 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
'use strict'
/*
* This module provides a simplified interface into the Aurora Serverless
* Data API by abstracting away the notion of field values.
*
* More detail regarding the Aurora Serverless Data APIcan be found here:
* https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html
*
* @author Jeremy Daly <jeremy@jeremydaly.com>
* @version 1.0.0-beta
* @license MIT
*/
// Require the aws-sdk. This is a dev dependency, so if being used
// outside of a Lambda execution environment, it must be manually installed.
const AWS = require('aws-sdk')
// Require sqlstring to add additional escaping capabilities
const sqlString = require('sqlstring')
// Supported value types in the Data API
const supportedTypes = [
'arrayValue',
'blobValue',
'booleanValue',
'doubleValue',
'isNull',
'longValue',
'stringValue',
'structValue'
]
/**********************************************************************/
/** Enable HTTP Keep-Alive per https://vimeo.com/287511222 **/
/** This dramatically increases the speed of subsequent HTTP calls **/
/**********************************************************************/
const https = require('https')
const sslAgent = new https.Agent({
keepAlive: true,
maxSockets: 50, // same as aws-sdk
rejectUnauthorized: true // same as aws-sdk
})
sslAgent.setMaxListeners(0) // same as aws-sdk
/********************************************************************/
/** PRIVATE METHODS **/
/********************************************************************/
// Simple error function
const error = (...err) => { throw Error(...err) }
// Parse SQL statement from provided arguments
const parseSQL = args =>
typeof args[0] === 'string' ? args[0]
: typeof args[0] === 'object' && typeof args[0].sql === 'string' ? args[0].sql
: error('No \'sql\' statement provided.')
// Parse the parameters from provided arguments
const parseParams = args =>
Array.isArray(args[0].parameters) ? args[0].parameters
: typeof args[0].parameters === 'object' ? [args[0].parameters]
: Array.isArray(args[1]) ? args[1]
: typeof args[1] === 'object' ? [args[1]]
: args[0].parameters ? error('\'parameters\' must be an object or array')
: args[1] ? error('Parameters must be an object or array')
: []
// Parse the supplied database, or default to config
const parseDatabase = (config,args) =>
config.transactionId ? config.database
: typeof args[0].database === 'string' ? args[0].database
: args[0].database ? error('\'database\' must be a string.')
: config.database ? config.database
: error('No \'database\' provided.')
// Parse the supplied hydrateColumnNames command, or default to config
const parseHydrate = (config,args) =>
typeof args[0].hydrateColumnNames === 'boolean' ? args[0].hydrateColumnNames
: args[0].hydrateColumnNames ? error('\'hydrateColumnNames\' must be a boolean.')
: config.hydrateColumnNames
// Prepare method params w/ supplied inputs if an object is passed
const prepareParams = ({ secretArn,resourceArn },args) => {
return Object.assign(
{ secretArn,resourceArn }, // return Arns
typeof args[0] === 'object' ?
omit(args[0],['hydrateColumnNames','parameters']) : {} // merge any inputs
)
}
// Utility function for removing certain keys from an object
const omit = (obj,values) => Object.keys(obj).reduce((acc,x) =>
values.includes(x) ? acc : Object.assign(acc,{ [x]: obj[x] })
,{})
// Utility function for picking certain keys from an object
const pick = (obj,values) => Object.keys(obj).reduce((acc,x) =>
values.includes(x) ? Object.assign(acc,{ [x]: obj[x] }) : acc
,{})
// Utility function for flattening arrays
const flatten = arr => arr.reduce((acc,x) => acc.concat(x),[])
// Normize parameters so that they are all in standard format
const normalizeParams = params => params.reduce((acc,p) =>
Array.isArray(p) ? acc.concat([normalizeParams(p)])
: Object.keys(p).length === 2 && p.name && p.value ? acc.concat(p)
: acc.concat(splitParams(p))
,[]) // end reduce
// Prepare parameters
const processParams = (sql,sqlParams,params,row=0) => {
return {
processedParams: params.reduce((acc,p) => {
if (Array.isArray(p)) {
let result = processParams(sql,sqlParams,p,row)
if (row === 0) { sql = result.escapedSql; row++ }
return acc.concat([result.processedParams])
} else if (sqlParams[p.name]) {
if (sqlParams[p.name].type === 'n_ph') {
acc.push(formatParam(p.name,p.value))
} else if (row === 0) {
let regex = new RegExp('::' + p.name + '\\b','g')
sql = sql.replace(regex,sqlString.escapeId(p.value))
}
return acc
} else {
return acc
}
},[]),
escapedSql: sql
}
}
// Converts parameter to the name/value format
const formatParam = (n,v) => formatType(n,v,getType(v))
// Converts object params into name/value format
const splitParams = p => Object.keys(p).reduce((arr,x) =>
arr.concat({ name: x, value: p[x] }),[])
// Get all the sql parameters and assign them types
const getSqlParams = sql => {
// TODO: probably need to remove comments from the sql
// TODO: placeholders?
// sql.match(/\:{1,2}\w+|\?+/g).map((p,i) => {
return (sql.match(/:{1,2}\w+/g) || []).map((p) => {
// TODO: future support for placeholder parsing?
// return p === '??' ? { type: 'id' } // identifier
// : p === '?' ? { type: 'ph', label: '__d'+i } // placeholder
return p.startsWith('::') ? { type: 'n_id', label: p.substr(2) } // named id
: { type: 'n_ph', label: p.substr(1) } // named placeholder
}).reduce((acc,x) => {
return Object.assign(acc,
{
[x.label]: {
type: x.type
}
}
)
},{}) // end reduce
}
// Gets the value type and returns the correct value field name
// TODO: Support more types as the are released
const getType = val =>
typeof val === 'string' ? 'stringValue'
: typeof val === 'boolean' ? 'booleanValue'
: typeof val === 'number' && parseInt(val) === val ? 'longValue'
: typeof val === 'number' && parseFloat(val) === val ? 'doubleValue'
: val === null ? 'isNull'
: Buffer.isBuffer(val) ? 'blobValue'
// : Array.isArray(val) ? 'arrayValue' This doesn't work yet
// TODO: there is a 'structValue' now for postgres
: typeof val === 'object'
&& Object.keys(val).length === 1
&& supportedTypes.includes(Object.keys(val)[0]) ? null
: undefined
// Creates a standard Data API parameter using the supplied inputs
const formatType = (name,value,type) => {
return Object.assign(
{ name },
type === null ? { value }
: {
value: {
[type ? type : error(`'${name}' is an invalid type`)]
: type === 'isNull' ? true : value
}
}
)
} // end formatType
// Formats the results of a query response
const formatResults = (
{ // destructure results
columnMetadata, // ONLY when hydrate or includeResultMetadata is true
numberOfRecordsUpdated, // ONLY for executeStatement method
records, // ONLY for executeStatement method
generatedFields, // ONLY for INSERTS
updateResults // ONLY on batchExecuteStatement
},
hydrate,
includeMeta
) =>
Object.assign(
includeMeta ? { columnMetadata } : {},
numberOfRecordsUpdated !== undefined && !records ? { numberOfRecordsUpdated } : {},
records ? {
records: formatRecords(records, hydrate ? columnMetadata : false)
} : {},
updateResults ? { updateResults: formatUpdateResults(updateResults) } : {},
generatedFields && generatedFields.length > 0 ?
{ insertId: generatedFields[0].longValue } : {}
)
// Processes records and either extracts Typed Values into an array, or
// object with named column labels
const formatRecords = (recs,columns) => {
// Create map for efficient value parsing
let fmap = recs && recs[0] ? recs[0].map((x,i) => {
return Object.assign({},
columns ? { label: columns[i].label } : {} ) // add column labels
}) : {}
// Map over all the records (rows)
return recs ? recs.map(rec => {
// Reduce each field in the record (row)
return rec.reduce((acc,field,i) => {
// If the field is null, always return null
if (field.isNull === true) {
return columns ? // object if hydrate, else array
Object.assign(acc,{ [fmap[i].label]: null })
: acc.concat(null)
// If the field is mapped, return the mapped field
} else if (fmap[i] && fmap[i].field) {
return columns ? // object if hydrate, else array
Object.assign(acc,{ [fmap[i].label]: field[fmap[i].field] })
: acc.concat(field[fmap[i].field])
// Else discover the field type
} else {
// Look for non-null fields
Object.keys(field).map(type => {
if (type !== 'isNull' && field[type] !== null) {
fmap[i]['field'] = type
}
})
// Return the mapped field (this should NEVER be null)
return columns ? // object if hydrate, else array
Object.assign(acc,{ [fmap[i].label]: field[fmap[i].field] })
: acc.concat(field[fmap[i].field])
}
}, columns ? {} : []) // init object if hydrate, else init array
}) : [] // empty record set returns an array
} // end formatRecords
// Format updateResults and extract insertIds
const formatUpdateResults = res => res.map(x => {
return x.generatedFields && x.generatedFields.length > 0 ?
{ insertId: x.generatedFields[0].longValue } : {}
})
// Merge configuration data with supplied arguments
const mergeConfig = (initialConfig,args) =>
Object.assign(initialConfig,args)
/********************************************************************/
/** QUERY MANAGEMENT **/
/********************************************************************/
// Query function (use standard form for `this` context)
const query = async function(config,..._args) {
// Flatten array if nested arrays (fixes #30)
const args = Array.isArray(_args[0]) ? flatten(_args) : _args
// Parse and process sql
const sql = parseSQL(args)
const sqlParams = getSqlParams(sql)
// Parse hydration setting
const hydrateColumnNames = parseHydrate(config,args)
// Parse and normalize parameters
const parameters = normalizeParams(parseParams(args))
// Process parameters and escape necessary SQL
const { processedParams,escapedSql } = processParams(sql,sqlParams,parameters)
// Determine if this is a batch request
const isBatch = processedParams.length > 0
&& Array.isArray(processedParams[0]) ? true : false
// Create/format the parameters
const params = Object.assign(
prepareParams(config,args),
{
database: parseDatabase(config,args), // add database
sql: escapedSql // add escaped sql statement
},
// Only include parameters if they exist
processedParams.length > 0 ?
// Batch statements require parameterSets instead of parameters
{ [isBatch ? 'parameterSets' : 'parameters']: processedParams } : {},
// Force meta data if set and not a batch
hydrateColumnNames && !isBatch ? { includeResultMetadata: true } : {},
// If a transactionId is passed, overwrite any manual input
config.transactionId ? { transactionId: config.transactionId } : {}
) // end params
try { // attempt to run the query
// Capture the result for debugging
let result = await (isBatch ? config.RDS.batchExecuteStatement(params).promise()
: config.RDS.executeStatement(params).promise())
// Format and return the results
return formatResults(
result,
hydrateColumnNames,
args[0].includeResultMetadata === true ? true : false
)
} catch(e) {
if (this && this.rollback) {
let rollback = await config.RDS.rollbackTransaction(
pick(params,['resourceArn','secretArn','transactionId'])
).promise()
this.rollback(e,rollback)
}
// Throw the error
throw e
}
} // end query
/********************************************************************/
/** TRANSACTION MANAGEMENT **/
/********************************************************************/
// Init a transaction object and return methods
const transaction = (config,_args) => {
let args = typeof _args === 'object' ? [_args] : [{}]
let queries = [] // keep track of queries
let rollback = () => {} // default rollback event
const txConfig = Object.assign(
prepareParams(config,args),
{
database: parseDatabase(config,args), // add database
hydrateColumnNames: parseHydrate(config,args), // add hydrate
RDS: config.RDS // reference the RDSDataService instance
}
)
return {
query: function(...args) {
if (typeof args[0] === 'function') {
queries.push(args[0])
} else {
queries.push(() => [...args])
}
return this
},
rollback: function(fn) {
if (typeof fn === 'function') { rollback = fn }
return this
},
commit: async function() { return await commit(txConfig,queries,rollback) }
}
}
// Commit transaction by running queries
const commit = async (config,queries,rollback) => {
let results = [] // keep track of results
// Start a transaction
const { transactionId } = await config.RDS.beginTransaction(
pick(config,['resourceArn','secretArn','database'])
).promise()
// Add transactionId to the config
let txConfig = Object.assign(config, { transactionId })
// Loop through queries
for (let i = 0; i < queries.length; i++) {
// Execute the queries, pass the rollback as context
let result = await query.apply({rollback},[config,queries[i](results[results.length-1],results)])
// Add the result to the main results accumulator
results.push(result)
}
// Commit our transaction
const { transactionStatus } = await txConfig.RDS.commitTransaction(
pick(config,['resourceArn','secretArn','transactionId'])
).promise()
// Add the transaction status to the results
results.push({transactionStatus})
// Return the results
return results
}
/********************************************************************/
/** INSTANTIATION **/
/********************************************************************/
// Export main function
module.exports = (params) => {
// Set the options for the RDSDataService
const options = typeof params.options === 'object' ? params.options
: params.options !== undefined ? error('\'options\' must be an object')
: {}
// Update the default AWS http agent with our new sslAgent
if (typeof params.keepAlive === 'boolean' ? params.keepAlive : true) {
AWS.config.update({ httpOptions: { agent: sslAgent } })
}
// Update the AWS http agent with the region
if (typeof params.region === 'string') {
AWS.config.update({ region: params.region })
}
// Disable ssl if wanted for local development
if (params.sslEnabled === false) {
// AWS.config.update({ sslEnabled: false })
options.sslEnabled = false
}
// Set the configuration for this instance
const config = {
// Require secretArn
secretArn: typeof params.secretArn === 'string' ?
params.secretArn
: error('\'secretArn\' string value required'),
// Require resourceArn
resourceArn: typeof params.resourceArn === 'string' ?
params.resourceArn
: error('\'resourceArn\' string value required'),
// Load optional database
database: typeof params.database === 'string' ?
params.database
: params.database !== undefined ? error('\'database\' must be a string')
: undefined,
// Load optional schema DISABLED for now since this isn't used with MySQL
// schema: typeof params.schema === 'string' ? params.schema
// : params.schema !== undefined ? error(`'schema' must be a string`)
// : undefined,
// Set hydrateColumnNames (default to true)
hydrateColumnNames:
typeof params.hydrateColumnNames === 'boolean' ?
params.hydrateColumnNames : true,
// TODO: Put this in a separate module for testing?
// Create an instance of RDSDataService
RDS: new AWS.RDSDataService(options)
} // end config
// Return public methods
return {
// Query method, pass config and parameters
query: (...x) => query(config,...x),
// Transaction method, pass config and parameters
transaction: (x) => transaction(config,x),
// Export promisified versions of the RDSDataService methods
batchExecuteStatement: (args) =>
config.RDS.batchExecuteStatement(
mergeConfig(pick(config,['resourceArn','secretArn','database']),args)
).promise(),
beginTransaction: (args) =>
config.RDS.beginTransaction(
mergeConfig(pick(config,['resourceArn','secretArn','database']),args)
).promise(),
commitTransaction: (args) =>
config.RDS.commitTransaction(
mergeConfig(pick(config,['resourceArn','secretArn']),args)
).promise(),
executeStatement: (args) =>
config.RDS.executeStatement(
mergeConfig(pick(config,['resourceArn','secretArn','database']),args)
).promise(),
rollbackTransaction: (args) =>
config.RDS.rollbackTransaction(
mergeConfig(pick(config,['resourceArn','secretArn']),args)
).promise()
}
} // end exports