DAX not working with lastEvaluatedKey

0

Hi all, I have a piece of code which is getting data for array of params after all the Promise settled i am returning the response.I am using DAX service. On first read i am getting the response but when i am reading for 2nd Time.I am not getting any response.does the implemented code works with DAX?

let queryArray: any = [];
    let response: any[] = [];
    const promiseRes: any = await Promise.allSettled(
      params.map(async (param) => {
        let data: any = await this.client.query(param).promise();

        queryArray = [...queryArray, ...data.Items];
        while (typeof data.LastEvaluatedKey != 'undefined') {
          param['ExclusiveStartKey'] = data.LastEvaluatedKey;

          data = await this.client.query(param).promise();
          queryArray = [...queryArray, ...data.Items];
        }
        return queryArray;
      })
    );
asked 2 years ago345 views
1 Answer
0

Hi there,

DAX APIs for reading and writing table items are fully compatible with standard DynamoDB APIs. Results should be no different than querying DynamoDB directly.

What happens if you bypass DAX and use the same code directly on the table? My guess is that you will have the same problem. I'm not sure, but my instinct is that there's a logic error with the way you are managing the promises and/or returning map results.

I don't have a DAX cluster running, but I wrote the following Javascript example that you should be able to quickly adapt and test on both DynamoDB and DAX. I also opted for a do...while loop which I think makes things a bit easier.

var AWS = require('aws-sdk');

// I'm using AWS SSO, so extra steps needed to make this work with SDK:
const { fromSSO } = require("@aws-sdk/credential-provider-sso");


async function main(client, params) {
    let queryResults = [];

    for (const param of params) {
        let done = false;

        do {
            let data = await client.query(param).promise();

            // Per docs below, sometimes query returns empty [] results.
            // https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html
            if (data.Items.length > 0) queryResults.push(data.Items);
            param.ExclusiveStartKey = data.LastEvaluatedKey;
            done = !('LastEvaluatedKey' in data);
        } while (!done);   
    }

    return queryResults;
}

(async () => {
    try {
        // I'm using AWS SSO, so extra steps needed to make this work with SDK:
        const credentials = await fromSSO({ profile: "personal" })();
        var client = new AWS.DynamoDB.DocumentClient({
            region: 'us-west-2',
            credentials: credentials
        });  

        let params = [];
        let param1 = {
            TableName: 'test-with-sk',
            Limit: 1,
            KeyConditionExpression: "pk = :pk",
            ExpressionAttributeValues: {
                ":pk": "1"
            }
        }

        params.push(param1);
        let results = await main(client, params);
        console.log('Combined query results:\n', results.map(r => JSON.stringify(r)));

    } catch (e) {
        console.log(e);
    }
})();
mwrb
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions