node.jsamazon-dynamodbaws-sdkdocumentclient

How would I rewrite this query using the DynamoDB DocumentClient?


I am testing queries in the console and would like to implement them in my code. I was curious how to get this one in my Node.js code. I think it has something to do with QueryFilter but I am struggling to get it.

enter image description here

let params = {
    TableName: tableName,
    KeyConditionExpression:
      'TimeId = :timeId and begins_with ( TypeKey , :typeKey) ',
    QueryFilter: ?,
    ExpressionAttributeValues: {
      ':timeId': `${year}-${week}`,
      ':typeKey': 'GA',
    },
  };

Solution

  • The query parms should look something like this

    {
      "TableName": "tableName",
      "KeyConditionExpression": "#PK = :timeId And begins_with(#SK, :typeKey)",
      "FilterExpression": "contains(#homeTeam, :team)",
      "ExpressionAttributeValues": {
        ":timeId": {
          "S": "2020-12"
        },
        ":typeKey": {
          "S": "GA"
        },
        ":team": {
          "S": "Value"
        }
      },
      "ExpressionAttributeNames": {
        "#PK": "TimeId",
        "#SK": "TypeKey",
        "#homeTeam": "homeTeam"
      }
    }
    

    When learning to put together queries for DDB, check out Amazons NoSQL Workbench. It has a nice GUI interface for building operations (e.g. update, delete, query, scan, etc) and will even generate the code to execute the operation. I've found it very useful in situations like your facing.