diff --git a/.gitignore b/.gitignore index d8f09c5..05e1511 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ node_modules .npm dump.rdb + +.vscode/ diff --git a/index.js b/index.js index 695b5ff..2e7e356 100644 --- a/index.js +++ b/index.js @@ -206,22 +206,33 @@ const formatResults = ( updateResults // ONLY on batchExecuteStatement }, hydrate, - includeMeta + includeMeta, + convertSnakeToCamel ) => Object.assign( includeMeta ? { columnMetadata } : {}, numberOfRecordsUpdated !== undefined && !records ? { numberOfRecordsUpdated } : {}, records ? { - records: formatRecords(records, hydrate ? columnMetadata : false) + records: formatRecords(records, hydrate ? columnMetadata : false, convertSnakeToCamel) } : {}, updateResults ? { updateResults: formatUpdateResults(updateResults) } : {}, generatedFields && generatedFields.length > 0 ? { insertId: generatedFields[0].longValue } : {} ) +const snakeToCamel = (value) => value.replace( + /([-_][a-z])/g, + (group) => group.toUpperCase().replace('-', '').replace('_', ''), +) + // Processes records and either extracts Typed Values into an array, or // object with named column labels -const formatRecords = (recs,columns) => { +const formatRecords = (recs,columns ,convertSnakeToCamel) => { + + if(convertSnakeToCamel){ + columns.filter(c => c.label.includes('_')).forEach(c => c.label = snakeToCamel(c.label)) + } + // Create map for efficient value parsing let fmap = recs && recs[0] ? recs[0].map((x,i) => { @@ -242,7 +253,7 @@ const formatRecords = (recs,columns) => { : acc.concat(null) // If the field is mapped, return the mapped field - } else if (fmap[i] && fmap[i].field) { + } else if (fmap[i] && fmap[i].field && Object.keys(field)[0] !== 'arrayValue') { return columns ? // object if hydrate, else array Object.assign(acc,{ [fmap[i].label]: field[fmap[i].field] }) : acc.concat(field[fmap[i].field]) @@ -253,7 +264,13 @@ const formatRecords = (recs,columns) => { // Look for non-null fields Object.keys(field).map(type => { if (type !== 'isNull' && field[type] !== null) { - fmap[i]['field'] = type + if (type === 'arrayValue') { + field = field.arrayValue + const [arrayType] = Object.keys(field) + fmap[i]['field'] = arrayType + } else { + fmap[i]['field'] = type + } } }) @@ -334,7 +351,8 @@ const query = async function(config,..._args) { return formatResults( result, hydrateColumnNames, - args[0].includeResultMetadata === true ? true : false + args[0].includeResultMetadata === true ? true : false, + config.convertSnakeToCamel ) } catch(e) { @@ -484,7 +502,9 @@ module.exports = (params) => { // TODO: Put this in a separate module for testing? // Create an instance of RDSDataService - RDS: new AWS.RDSDataService(options) + RDS: new AWS.RDSDataService(options), + + convertSnakeToCamel: params.convertSnakeToCamel || false, } // end config