I'll-Be-Back
I'll-Be-Back

Reputation: 10828

How to get entries based on date?

Using DynamoDB GSI Index, I like to return entries with the following condition: exit_at date that is passed after 30 mins of the current time. How do I archive this using query?

GSI example: sent is a PK and and exit_at is a Sort Key.

For example in the dynamodb:

{
  "exit_at": "2021-02-12T09:30:01Z",
  "sent": 0,
  "uuid": "ee1b0230-bfb7-1084-5020-8fef7gg6a86b",
}

{
  "exit_at": "2021-02-12T09:25:01Z",
  "sent": 0,
  "uuid": "gg2b0245-fgb2-5552-1454-g3gf7he6f4g",
}

Let say for example current time is 2021-02-12T10:00:01Z only 1 entry should return:

{
  "exit_at": "2021-02-12T09:30:01Z",
  "sent": 0,
  "uuid": "ee1b0230-bfb7-1084-5020-8fef7gg6a86b",
}

I would run a cron every 5 minutes to get the entries to send the messages.

If this is not possible, what is another approach to change the structure design or solution in AWS infrastructure?

Upvotes: 0

Views: 72

Answers (1)

Seth Geoghegan
Seth Geoghegan

Reputation: 5747

The good news is that your data model already supports this operation.

You are using ISO-8601 formatted timestamps, which have the useful characteristic of being lexicographically sortable. In other words, their alphabetical order is the same as their time order.

The operation will look something like this (in pseudo code)

ddbClient.query({
    TableName: "YOUR_TABLE_NAME",
    IndexName: "YOUR_INDEX_NAME",
    KeyConditionExpression: "GSIPK = :sent And exit_at >= :time",
    "ExpressionAttributeValues": {
      ":time": "TIMESTAMP_HERE",
      ":sent": 0
    }
})

Your client application would need to calculate the ISO-8601 timestamp for 30 minutes ago and place it where I've typed TIMESTAMP_HERE.

Upvotes: 1

Related Questions