Skip to content

Instantly share code, notes, and snippets.

@daverickdunn
Last active April 23, 2021 22:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daverickdunn/2ec81525aa9ea75c15aa99f310fe2b28 to your computer and use it in GitHub Desktop.
Save daverickdunn/2ec81525aa9ea75c15aa99f310fe2b28 to your computer and use it in GitHub Desktop.
DynamoDB Scan FilterExpression item IN list
const listToObjectMappings = (list) => {
let x = {}
for (var i=0; i<list.length; i++){
x[':' + i.toString()] = list[i]
}
return x
}
let statuses = ['available', 'in-transit', 'delivered']
let mappings = listToObjectMappings(statuses)
let type = 'delivery' // type is just to demonstrate the use of Object.assign to build the 'ExpressionAttributeValues'
let joined = Object.keys(mappings).join(); // string: ":available, :in-transit, :delivered"
query = {
FilterExpression: '#type = :type AND #stat IN (' + joined + ')',
ExpressionAttributeNames: {
'#stat' : 'status',
'#type' : 'type'
},
ExpressionAttributeValues: Object.assign( { ':type': type }, mappings )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment