Aws-sdk-js: Typescript definition for AttributeValue for DynamoDb leads to query failures

Created on 3 Oct 2017  路  3Comments  路  Source: aws/aws-sdk-js

I have a DynamoDb table named User which has a key schema like the below

    UserTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: "Users"
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH

I am using the following typescript code to query from this table

let params: QueryInput = {
      TableName: "Users",
      KeyConditionExpression: "#id = :id",
      ExpressionAttributeNames: {
        "#id": "id"
      },
      ExpressionAttributeValues: {
        ":id": {
              S: "1"
           }
      }
    }

    let result = await ddbClient.query(params).promise();

This is resulting in the following error

ValidationException: One or more parameter values were invalid: Condition parameter type does not match schema type

I have read on the internet that you do not need to specify the types in ExpressionAttributeValue. However, due to the published definition of AttributeValue interface, I cannot do away without specifying the type. What I am doing, for now, is declaring the params variable to be of type any and get rid of type specified in the ExpressionAttributeValue like the below

let params: any = {
      TableName: "Users",
      KeyConditionExpression: "#id = :id",
      ExpressionAttributeNames: {
        "#id": "id"
      },
      ExpressionAttributeValues: {
        ":id": "1"
      }
    }

    let result = await ddbClient.query(params).promise();

This works. Is this a bug? Or am I doing something wrong?

guidance

Most helpful comment

@schatekar
Are you referencing the QueryInput interface from AWS.DynamoDB.DocumentClient.QueryInput or AWS.DynamoDB.QueryInput? Since the DocumentClient automatically handles transforming values into AttributeValues, its interfaces are different. In your case, you should be using the QueryInput from the DocumentClient.

All 3 comments

@schatekar
Are you referencing the QueryInput interface from AWS.DynamoDB.DocumentClient.QueryInput or AWS.DynamoDB.QueryInput? Since the DocumentClient automatically handles transforming values into AttributeValues, its interfaces are different. In your case, you should be using the QueryInput from the DocumentClient.

@chrisradek That worked like a charm. Thanks.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

Was this page helpful?
0 / 5 - 0 ratings