Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix map array value #52

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ node_modules
.npm

dump.rdb

.vscode/
34 changes: 27 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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])
Expand All @@ -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
}
}
})

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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

Expand Down