Search code examples
amazon-web-servicesamazon-dynamodbdynamodb-queries

@aws/dynamodb-data-mapper: How to write ConditionExpression in QueryOptions filter area?


I would like to fetch data from DynamoDB database by using [AWS-DynamoDB-Data-Mapper][1]. I am using QueryIterator and to provide indexName, limit, filter purpose, I am using [QueryOptions][2]. In QueryOptions, I would like to use filter?: ConditionExpression. Where I would like to use contains() function, but I am not able to form ConditionExpression in the below example -

{ "Id": {"N": "456" }, "ProductCategory": {"S": "Sporting Goods" }, "Price": {"N": "650" } }

const paginator = dbMapper.query(
  Products,
  {
    Id: 456,
    Price: between(500, 1000)
  },
  { 
    indexName: 'indexByPriceId', 
    limit: 10, 
    scanIndexForward: false,
    filter: contains(ProductCategory, 'goods')
   } 
);

It returns an syntax error in this line filter: contains(ProductCategory, 'goods'), it says ContainsPredicate is not a ConditionExpression.

Please help me out by providing me the ConditionExpression in QueryOptions. [1]: https://github.com/awslabs/dynamodb-data-mapper-js [2]: https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-data-mapper/interfaces/queryoptions.html#projection


Solution

  • Very late response, but if anyone else is looking for how to use this, the best documentation I was able to find is here: https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-expressions

    In order to use contains, you need to pass an object of type SimpleConditionExpression to filter like so:

    filter: { subject: 'DB_COLUMN', ...contains('STRING_TO_SEARCH') }
    

    So for the example above it should be:

    const paginator = dbMapper.query(
      Products,
      {
        Id: 456,
        Price: between(500, 1000)
      },
      { 
        indexName: 'indexByPriceId', 
        limit: 10, 
        scanIndexForward: false,
        filter: { subject: 'ProductCategory', ...contains('goods') }
       } 
    );