Hi, I don’t understand why dgraph still has no increment/decrement feature :(( We need that feature buddies
Unfortunately upserts ain’t a solution, because if 1000 users like within one second a picture, dgraph can’t keep up with that and the transactions will fail
Firestore works differently as counters pretty much don’t exist, so IMHO they are not comparable.
I see two options.
Use a custom lambda with DQL with an upsert:
Create a new node for each count, then aggregate the total.
Ex. The number of likes on a posts:
type Post {
id: ID!
likes: [Like] @hasInverse(field: post)
...
}
type User {
id: ID!
likedPosts: [Like] @hasInverse(field: user)
...
}
type Like {
id: string @id
user: User
post: Post
}
The like id will be a composite key like user__post to enforce uniqueness. I would also put this in a custom mutation until dgraph supports composite indexes and lambda pre-hooks in about 5 years.
This way, when you remove a like, add a like etc, the aggregate count will always be up-to-date. Increments can be fishy sometimes.
First solution would still have problems if 999 users like something at the same time. Right? (because 998 mutations/transactions would fail)
Or am I missing something?
Second solution: With that solution, to get the total like count, I would have to aggregate/count it every time i request it. is that right?
With the sentence:
you mean that it will up-to-date, with the composite index & pre-hooks features in 5 years?
Or already now? How? With a ‘custom mutation’? What exactly do you mean with that?