awslabs / dynamodb-data-mapper-js

A schema-based data mapper for Amazon DynamoDB.
https://awslabs.github.io/dynamodb-data-mapper-js/
Apache License 2.0
816 stars 106 forks source link

How to achieve nested filters scan? #194

Open kanhaiya-compro opened 4 years ago

kanhaiya-compro commented 4 years ago

I have an attribute named "NestedAttribute" in DynamoDB table. It has the following nested value.

{
     parent1: {
       child1-1: "Hi guys",
       child1-2: "Hi guys2"
     },
     parent2: {
       child2-1: "Hi guys",
       child2-2: "Hi guys2"
     }
}

I want to run scan to filter - parent1.child1-1 contains("guys") or parent1.child1-2 contains("guys"). My question is - Is this possible? and if yes, How is this possible in data mapper.

My current implementation is something like this.. how do i fit the above query in this?

const orExpression: ConditionExpression = {
            type: 'Or',
            conditions: [
                {
                    ...contains(searchString),
                    subject: 'NestedAttribute.parent1.child1-1',
                },
               {
                    ...contains(searchString),
                    subject: 'NestedAttribute.parent1.child1-2',
                }
            ]
        };
const iterator = dynamoWebService.scan(
                Table Name,
                {
                    filter: {
                        ...orExpression
                    },
                    projection: [...some attributes],
                    limit: 20 - results.length,
                    startKey: lastEvaluatedKey,
                    indexName: 'some index',
                });
            for await (const record of iterator) {
                results.push(record);
            }

Thanks in advance!

jdvachhani95 commented 3 years ago

Hi @kanhaiya-compro Most of the code above code make sense to me. However, i found a small mistake. contains expression predicts match the exact given string in List data type. So you can use a exact string to compare with using equals expression predict.

jdvachhani95 commented 3 years ago

Here is the example of one my application doing scan with nested filter expression, purpose of this filter expression is to fetch applications which have pending status AND either first name OR last name begins with given name:

const filterExpression: ConditionExpression = {
        type: 'And',
        conditions: [
          {
            type: 'Or',
            conditions: [
              {
                ...beginsWith(name),
                subject: 'firstname'
              },
              {
                ...beginsWith(name),
                subject: 'lastname'
              }
            ]
          },
          {
            ...inList('pending'),
            subject: 'status'
          }
        ]
      };