Created
December 20, 2016 17:49
-
-
Save vshjxyz/49a9945fe3c0ffe02774a6e7cf9e630d to your computer and use it in GitHub Desktop.
DynamoDB JS FilterExpression with IN clause
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)