I've read Firebase data consistency across multiple nodes, the multi update stuff works as expected, however I don't know how to do the following:
Let's say I have 2 collections: answers
and posts
. Now I want to write an answer to a post and increment the answers counter on the post object keeping the data consistent between collections. So the actual objects look similar to these:
post
{
...,
answers: X
}
answer
{
postID: <random string>,
...
}
As I said above, that link answers how to do a merged update, however, how can I be sure that the answers
property will only be incremented/decremented (in case of deletion) by 1? What I have so far is this:
"answers": {
".validate": "newData.isNumber() && (newData.val() === data.val() + 1 || newData.val() === data.val() - 1)"
}
Obviously that would work (I assume, haven't tried), but that would still allow me to create a new answer and decrement the counter. How can I overcome that issue?