Pieter Moeyersons
Pieter Moeyersons

Reputation: 17

AWS AppSync getting the count of another table (for example counting the likes of a post)

I have the following AWS AppSync/Amplify model

type Event @model {
  id: ID!
  name: String!
  ticketsSold: Int!
  moneySum: Int!
}

The ticketsSold property must reflect the count of orders with eventId = X. moneySum must represent a SUM of all the orders ticket price with eventId = X.

There are 2 options (or more?) to consider:

  1. With every putItem (createOrder mutation), update the correct event row with the new count/sum
  2. Do a query/scan of all the orders with eventId = X via an AWS Lambda resolver

There are some pro's and con's with each of the options. With option 1 there is the possibility to get out of sync if multiple people order at the same time (transactions?). With option 2 there is the drawback that scanning is very expensive & slow...

The events are shown on the home page of a ReactJS app with the ticketsSold and moneySum shown as a preview and on the detail so it will get fetched "a lot".

Suggestions? Thanks a lot!

Upvotes: 0

Views: 276

Answers (1)

cyberwombat
cyberwombat

Reputation: 40163

I would go with option 1 using streams from your ticket DB into a lambda that updates some table with the aggregated info. It's going to be pretty fast and way cleaner than endless scans. I use this to aggregate lambda usage for users, logging lambda runtime into a table that can be instantly queried for the total. Works great.

Upvotes: 1

Related Questions