What is the best practice for querying an AWS Amplify GraphQL/DynamoDB database for changes since your last query?

273 views Asked by At

I have an iOS AWS Amplify Project using GraphQL API and DynamoDB to store data. This is a chat app.

Initially when a user signs in he will get all messages for a conversation. If then he comes back to the app a few days later I want to update any new messages in each conversation.

At the moment I am using the Date of the last message as a predicate for future queries. However I was wondering if there is a better or more standard way of querying a table for changes since the last time you queried?

I am not using DataStore. I am managing my own local storage.

let keys = Message.keys
let predicate = keys.conversationID == conversationID && keys.createdAt > lastQueryDate
Amplify.API.query(request: .paginatedList(Message.self, where: predicate, limit: 100)) { [weak self]
    DispatchQueue.main.async {
        switch event {
        case .success(let result):
            switch result {
            case .success(let messages):
                // handle data
            case .failure(let error):
                print("Got failed result with \(error.errorDescription)")
            }
        case .failure(let error):
            print("Got failed event with error \(error)")
        }
    }
}
1

There are 1 answers

0
Ermiya Eskandary On

Querying based on the date of the last message is perfectly acceptable & standard.

DynamoDB itself doesn't provide a way to get "new" items (messages in this case) like you would be able to do using SQS for example - there's no way to "acknowledge" an item.

For pagination, you could use markers like ExclusiveStartKey however you already have a date field to filter by so there is absolutely no need to do so.

You're following best practices already by modeling your data correctly.