Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scan Function in DynamoDB with reserved keyword as FilterExpression NodeJS

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 ???

like image 310
Saleem Ahmed Avatar asked Apr 18 '16 16:04

Saleem Ahmed


People also ask

How do you use reserved words in DynamoDB?

If you need to write an expression containing an attribute name that conflicts with a DynamoDB reserved word, you can define an expression attribute name to use in the place of the reserved word. For more information, see Expression attribute names in DynamoDB.

What is Filterexpression in DynamoDB?

A filter expression determines which items within the Query results should be returned to you. All of the other results are discarded. A filter expression is applied after a Query finishes, but before the results are returned.

How does scan work in DynamoDB?

A Scan operation in Amazon DynamoDB reads every item in a table or a secondary index. By default, a Scan operation returns all of the data attributes for every item in the table or index. You can use the ProjectionExpression parameter so that Scan only returns some of the attributes, rather than all of them.

What is difference between scan and Query in DynamoDB?

DynamoDB supports two different types of read operations, which are query and scan. A query is a lookup based on either the primary key or an index key. A scan is, as the name indicates, a read call that scans the entire table in order to find a particular result.


1 Answers

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) {....}); 
like image 190
Saleem Ahmed Avatar answered Oct 08 '22 23:10

Saleem Ahmed