Using Fauna to build a notification feed (mass insert, multiple updates, order, limit...)

Indexes are your friend (disclaimer, didn’t test the queries since I have to leave my desk in about 5 minutes :slight_smile: ), given that you have an index with two terms (user and seen) something in the vein of:

Map(
    Paginate(Match(Index('notifications_by_user_and_seen'), 'someuser', false),
    Lambda(['notificationRef'], Update(Var('notificationRef'),  .. do whatever you want .. )) 
)

You could also update the info that is linked to the transaction by first mapping over the notification refs, getting them and get the info and so on and updating whatever you want. It’s much like how you would do it in a regular programming language since FQL is procedural, not declarative. FQL is literally made for these kind of more complex transactions (hence why it is procedural). Forget about the idea that a scalable databases is because it’s scalable very limited in the way you query, FaunaDB’s FQL is quite powerful.

If you only want to get those between a certain range (e.g. the time the notification came in), you add the creation time to the values of your index and wrap Range around the match (values are for three things, range queries, sorting and the index’s result values).

Map(
    Paginate(
         Range(
              Match(Index('notifications_by_user_and_seen'), 'someuser', false),  
              Time(...), Time(...)))
    Lambda(['notificationRef'], Update(Var('notificationRef'),  .. do whatever you want .. )) 
)

Of course, I can’t write it for you :upside_down_face: so I’ll only explain it in pseudo FQL code for now (I might have missed a bracket here and there) . If you do get blocked on how to do something, let us know.

So you probably want to read up on these specifically:

  • Indexes
  • Match/Paginate
  • Range
  • Map/Lambda + Get (Let to structure)
  • Update

to get started :slight_smile:

1 Like