Scan Function in DynamoDB with reserved keyword as FilterExpression NodeJS

Saleem Ahmed picture Saleem Ahmed · Apr 18, 2016 · Viewed 27.1k times · Source

My scan function :

var tableName = 'faasos_orders',
    filterExp = 'status = :delivered OR status = :void OR status = :bad',
    projectionValues = '',
    expressionAttr = {};    
    expressionAttr[":delivered"] = "delivered";
    expressionAttr[":bad"] = "bad";
    expressionAttr[":void"] = "void"; 
    limit = 10;
  dynamoConnector.getItemUsingScan(tableName, filterExp, projectionValues, expressionAttr, function (err, data) {  ...........} 

Error on running :

    { [ValidationException: Invalid FilterExpression: Attribute name is a reserved keyword; reserved keyword: status]
  message: 'Invalid FilterExpression: Attribute name is a reserved keyword; reserved keyword: status',
  code: 'ValidationException',
  time: Mon Apr 18 2016 21:57:30 GMT+0530 (IST),
  requestId: 'AV6QFHM7SPQT1QR3D4OO81ED4FVV4KQNSO5AEMVJF66Q9ASUAAJG',
  statusCode: 400,
  retryable: false,
  retryDelay: 0 }

Now I do get the point I am trying to use a reserved keyword in th e filterExpression which is illegal. But if I run the same function through aws gui it returns data beautifully (check image for details): Scan function on status through gui

So the question is how do I add the filter expression through node without having to change the key name ???

Answer

Saleem Ahmed picture Saleem Ahmed · Apr 19, 2016

Solved :

There are two parameters taken by aws-sdk :

Expression Attribute Name

Expression Attribute Value

both provide the functionality of replacing placeholders used in the attributes list. Here by Attributes it is a bit ambiguous, where I got confused. The wizards over at aws mean both the key and value when they use the term attribute.

So in a case where you want to use a reserved key word as a key attribute use the Expression Attribute Name parameter with #(pound) to denote the placeholder.

Similarly where you want to use placeholders for value attribute use the Expression Attribute Value parameter with :(colon) to denote the placeholder.

So finally my code (working) looks like this :

var param = {
  TableName: "faasos_orders",
  FilterExpression: "#order_status = :delivered OR #order_status = :void OR #order_status = :bad",
  ExpressionAttributeValues: {
    ":delivered": "delivered",
    ":void": "void",
    ":bad": "bad"
  },
  ExpressionAttributeNames: {
    "#order_status": "status"
  }
};  
  dynamodb.scan(param, function (err, data) {....});