1
votes

I'm using DynamoDB to store records for a web application.

Each record has a uuid (partition key) and a unix timestamp (sort key). How can I get all records sorted by newest timestamp?

Using scan is expensive, and using submitting a partition key is apparently necessary using a Query

I get the following error with the code I have tried already:

 Query condition missed key schema element: uuid

My service:

serverCallChats() {
    return new Promise(async (resolve, reject) => {
        const _dynamoDB = new AWS.DynamoDB.DocumentClient();

        const daysBack = (days) => {
            const date = new Date();
            date.setDate(date.getDate() - days);
            return date.getTime();
        };

        const params = {
            TableName: 'chat-channel',
            KeyConditionExpression: '#sortKeyName < :days',
            ExpressionAttributeNames: {
                '#sortKeyName': 'timestamp'
            },
            ExpressionAttributeValues: {
                ':days': daysBack(7)
            }
        };
1
How often do you need to do a query like this? The best and most cost effective solution depend on this as well.Kirk
@Kirk the short answer is quite often.Gustav Vingtoft

1 Answers

0
votes

So the solution to my use case, was to use a scan and filter from the FilterExpression:

return new Promise(async (resolve, reject) => {
        const _dynamoDB = new AWS.DynamoDB.DocumentClient();

        const daysBack = (days) => {
            const date = new Date();
            date.setDate(date.getDate() - days);
            return date.getTime();
        };

        const params = {
            TableName: 'chat-table',
            FilterExpression: 'latestActivity > :hkey',
            ExpressionAttributeValues: {
                ':hkey': daysBack(7)
            },
        };

        _dynamoDB.scan(params, (err, data) => {
            if (err) {
                console.log(err);
                reject(err);
            } else {
                console.log('chatdata: ', data);
                resolve(data);
           }
        });
    });

With this solution, the output from the DB will not be sorted, and this will have to be done in the client. This is however a suitable solution for my case, considering the size of the scan