Stack: React on AWS Amplify with GraphQL(DynamoDB)
Since non-relationship database does not offer SELECT .. COUNT .. FROM ..., it has been an issue for me to effectively show the number of items on my app.
Current implementation is to update count manually. Every time new post is created by a user, I manually update post count + 1 with separate call which will be saved under different DynamoDB table. This has an issue that the count + 1 request does not always execute base on the internet connection status.
Another option is to use scan operator on DynamoDB that is not efficient at all since it needs to go through all items in the table that can have over millions of rows.
Seems like @searchable directive can be an option that can be called as a query when new post is created, execute query to search all post by user ID and get the total count number. However, I'm not very familiar with this solution and not sure how the performance will be plus running cost(since it requires EC2)
How did you solve this issue and what is the best way to implement this?
AmplifyCountDirective
Example This package provides the
@count directivewhich can be used to annotate your models. Let's say we use it in the following schema:This will compile into:
Finally you can query the number of items in your table using the same filters as you would use to list them: