To add support for server value increment
Albert-Gao opened this issue · comments
Hi, I have a use case of updating a counter field in the realtime db, and i am looking for this ServerValue.Increment()
API, seems not supported in golang sdk.
I want to update this field on the server side without having to read the original value and do not need to worry about multi-write conflicts.
- The doc for ServerValue.Increment():
https://firebase.google.com/docs/database/web/read-and-write#atomic_server-side_increments\\
- And a ref about the performance gain compare to transactional update:
https://www.faqcode4u.com/faq/59074/how-quickly-can-you-atomically-increment-a-value-on-the-firebase-realtime-database
- It's there in the node.js sdk seems
I found a few problems with this issue:
- I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
- This issue does not seem to follow the issue template. Make sure you provide all the required information.
After investigating the source code, you could just do this to make server value increment working.
type ServerValue struct {
SV interface{} `json:".sv"`
}
type ServerValueIncrement struct {
Increment int64 `json:"increment"`
}
...
...
ref := client.NewRef(fmt.Sprintf("user_scores/%d/%s", 1, "score"))
sv := ServerValue{
SV: &ServerValueIncrement{
Increment: 1,
},
}
if err := ref.Set(context.TODO(), &sv); err != nil {
log.Fatal(err)
}
fmt.Println("score increment successfully!")