Fireship · @fireship

Firestore Increment - Counters that Scale

Uploaded April 8, 2019 · Length 3:51

Firestore Increment - Counters that Scale

Summary

Firestore increment function simplifies counters and data aggregation with atomic updates.

  • firestore increment
  • atomic updates
  • data aggregation
  • cloud firestore
  • firebase

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:06

    One of the challenges with fire store is

  3. 0:08

    keeping an accurate count on a document

  4. 0:10

    because you might have multiple clients

  5. 0:11

    competing to update the same value at

  6. 0:13

    the same time. Fire store has always had

  7. 0:15

    a way to maintain accurate counters in

  8. 0:17

    the form of database transactions. But

  9. 0:19

    they require a pretty significant amount

  10. 0:20

    of code just to do something simple like

  11. 0:22

    update a counter. Fortunately, just a

  12. 0:24

    couple weeks ago, fire store released a

  13. 0:25

    new tool called increment. In today's

  14. 0:27

    quick video, you'll learn how increment

  15. 0:29

    can help simplify your code and do

  16. 0:30

    things like data aggregation entirely

  17. 0:32

    from the front end. If you're new here,

  18. 0:33

    like and subscribe and you can grab the

  19. 0:35

    full source code from firehip io.

  20. 0:37

    Imagine you're in a situation where you

  21. 0:39

    have a blog post and you want to keep

  22. 0:40

    track of the total number of likes on

  23. 0:42

    that post. Prior to increment, the way

  24. 0:44

    we would handle this is to create a

  25. 0:45

    database transaction where we can read

  26. 0:47

    the current value, increment it, and

  27. 0:49

    then update the database with the new

  28. 0:50

    value. This works perfectly fine, but we

  29. 0:52

    can now dramatically simplify the

  30. 0:54

    process by just passing an increment

  31. 0:55

    value to fire store and it will do all

  32. 0:57

    the calculation for us atomically on the

  33. 0:59

    back end. You can see in the demo here I

  34. 1:01

    have an increment and decrement button

  35. 1:02

    and when I push these buttons they

  36. 1:03

    either increase or decrease the count by

  37. 1:05

    one. And what makes this magical is that

  38. 1:07

    we don't need to know the actual value

  39. 1:08

    of the current count to make this

  40. 1:10

    update. Let's take a look at how easy

  41. 1:12

    this actually is in the code. The first

  42. 1:13

    thing we'll do is make a reference to

  43. 1:15

    fire store. And you want to make sure

  44. 1:16

    you're using the latest version of the

  45. 1:17

    SDK because this is a new feature. And

  46. 1:20

    that will give you access to field value

  47. 1:21

    increment which is a function that

  48. 1:23

    expects an argument with the value that

  49. 1:25

    you want to increment by. For a lot of

  50. 1:27

    simple use cases, you can increase your

  51. 1:28

    counter by one or you can decrease it by

  52. 1:31

    passing negative one. From there, we'll

  53. 1:33

    just make a reference to a document in

  54. 1:34

    the database. And then we can call

  55. 1:36

    update or set with the merge true option

  56. 1:38

    to update the counter. So when you pass

  57. 1:40

    the increment field value to fire store,

  58. 1:42

    it will look at the value that currently

  59. 1:43

    exists on that document, then perform

  60. 1:45

    the math to update it server side, which

  61. 1:47

    means that your clients can never update

  62. 1:48

    it with the wrong value. That's how it

  63. 1:50

    looks in the code, but you're not

  64. 1:51

    limited to just using integer values.

  65. 1:53

    For example, you can pass in floating

  66. 1:55

    point or decimal values, and fire store

  67. 1:57

    knows how to perform those calculations

  68. 1:59

    as well. But one of the places I find it