Fireship · @fireship
Firestore Increment - Counters that Scale
Uploaded April 8, 2019 · Length 3:51
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.
- 0:00
[Music]
- 0:06
One of the challenges with fire store is
- 0:08
keeping an accurate count on a document
- 0:10
because you might have multiple clients
- 0:11
competing to update the same value at
- 0:13
the same time. Fire store has always had
- 0:15
a way to maintain accurate counters in
- 0:17
the form of database transactions. But
- 0:19
they require a pretty significant amount
- 0:20
of code just to do something simple like
- 0:22
update a counter. Fortunately, just a
- 0:24
couple weeks ago, fire store released a
- 0:25
new tool called increment. In today's
- 0:27
quick video, you'll learn how increment
- 0:29
can help simplify your code and do
- 0:30
things like data aggregation entirely
- 0:32
from the front end. If you're new here,
- 0:33
like and subscribe and you can grab the
- 0:35
full source code from firehip io.
- 0:37
Imagine you're in a situation where you
- 0:39
have a blog post and you want to keep
- 0:40
track of the total number of likes on
- 0:42
that post. Prior to increment, the way
- 0:44
we would handle this is to create a
- 0:45
database transaction where we can read
- 0:47
the current value, increment it, and
- 0:49
then update the database with the new
- 0:50
value. This works perfectly fine, but we
- 0:52
can now dramatically simplify the
- 0:54
process by just passing an increment
- 0:55
value to fire store and it will do all
- 0:57
the calculation for us atomically on the
- 0:59
back end. You can see in the demo here I
- 1:01
have an increment and decrement button
- 1:02
and when I push these buttons they
- 1:03
either increase or decrease the count by
- 1:05
one. And what makes this magical is that
- 1:07
we don't need to know the actual value
- 1:08
of the current count to make this
- 1:10
update. Let's take a look at how easy
- 1:12
this actually is in the code. The first
- 1:13
thing we'll do is make a reference to
- 1:15
fire store. And you want to make sure
- 1:16
you're using the latest version of the
- 1:17
SDK because this is a new feature. And
- 1:20
that will give you access to field value
- 1:21
increment which is a function that
- 1:23
expects an argument with the value that
- 1:25
you want to increment by. For a lot of
- 1:27
simple use cases, you can increase your
- 1:28
counter by one or you can decrease it by
- 1:31
passing negative one. From there, we'll
- 1:33
just make a reference to a document in
- 1:34
the database. And then we can call
- 1:36
update or set with the merge true option
- 1:38
to update the counter. So when you pass
- 1:40
the increment field value to fire store,
- 1:42
it will look at the value that currently
- 1:43
exists on that document, then perform
- 1:45
the math to update it server side, which
- 1:47
means that your clients can never update
- 1:48
it with the wrong value. That's how it
- 1:50
looks in the code, but you're not
- 1:51
limited to just using integer values.
- 1:53
For example, you can pass in floating
- 1:55
point or decimal values, and fire store
- 1:57
knows how to perform those calculations
- 1:59
as well. But one of the places I find it


