Skip to content

Instantly share code, notes, and snippets.

@vshjxyz
Created December 20, 2016 17:49
Show Gist options
  • Save vshjxyz/49a9945fe3c0ffe02774a6e7cf9e630d to your computer and use it in GitHub Desktop.
Save vshjxyz/49a9945fe3c0ffe02774a6e7cf9e630d to your computer and use it in GitHub Desktop.
DynamoDB JS FilterExpression with IN clause
function generateParametersList (name = '', params) {
return _.chain(params)
.uniq()
.map((param, i) => ({ [`:${name}${i}`]: param }))
.reduce((acc, curr) => ({ ...acc, ...curr }))
.value()
}
const typesMapping = generateParametersList('type', types)
const params = {
TableName: 'MyTable',
KeyConditionExpression: '#da = :date AND #lo = :location',
IndexName: 'location-date-index',
FilterExpression: `#ty IN (${Object.keys(typesMapping).join(', ')})`,
ExpressionAttributeNames: {
'#da': 'date',
'#lo': 'location',
'#ty': 'type'
},
ExpressionAttributeValues: {
':date': date,
':location': parseInt(location),
...typesMapping
}
}
@vshjxyz
Copy link
Author

vshjxyz commented Dec 20, 2016

Wanted to share this as it took quite a while to decipher dynamoDB's documentation and understand that you cannot pass an array as an ExpressionAttributeValues, even tho there are no errors showing up (the results will be always empty)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment