amazon-dynamodbdynamodb-queriesamazon-dynamodb-index

How to Retrieve and Paginate Items in DynamoDB Table Ordered by Date?


I want to retrieve all items from the "albums" table in DynamoDB using pagination, ordered by date. Currently, I can paginate items using the Scan command, but the result set is not ordered.

Here's my current code for paginating with the Scan command:

async function run(): Promise<void> {
  const client = new DynamoDBClient({});

  const params1: ScanCommandInput = {
    TableName: 'albums',
    Limit: 2,
    ExclusiveStartKey: {
      portfolioId: { S: '3f6b3897-a530-4f42-857c-443e6d09e44a' },
      albumId: { S: '724c1fba-1afa-4e01-9f83-4a66d95d56ed' }
    }
  };

  const data = await client.send(new ScanCommand(params1));
  console.dir(data, { depth: null });
}

The Scan command does not return ordered results. How should I update the current table definition and query command to retrieve paginated data ordered by date?

Here's the current table definition:

 private getAlbumsTable(): dynamoDb.ITable {
    const table = new dynamoDb.Table(this, 'albums', {
      partitionKey: {
        name: 'albumId',
        type: dynamoDb.AttributeType.STRING
      },
      sortKey: {
        name: 'portfolioId',
        type: dynamoDb.AttributeType.STRING
      },
      tableName: 'albums',
      removalPolicy: cdk.RemovalPolicy.DESTROY,
      stream: dynamoDb.StreamViewType.NEW_AND_OLD_IMAGES,
      billingMode: dynamoDb.BillingMode.PAY_PER_REQUEST
    });

    table.addGlobalSecondaryIndex({
      indexName: 'portfolioIdIndex',
      partitionKey: {
        name: 'portfolioId',
        type: dynamoDb.AttributeType.STRING
      }
    });

 
    return table;
  }

I understand that the Scan operation does not support sorting. Should I use a Query operation instead? If so, how should I structure my table and query to achieve this?

Thank you for your help!


Solution

  • You need to create an index with a static value as partition key and date as sort key, that way you get global sorter order.

    This blog explains in more detail.