In my current database design, I want to read and write to messages subcollection of chat collection, but I want to ensure whether the chat id exists or not. Chat ID is of the form user1refID_user2refID
. If chat ID doesn't exist I want to create a new one and trigger cloud function to assign UIDs to member 1 and member 2 fields of chat documents by using a mapping from refID
to UserID
. In my security rules, I am checking whether incoming user ID is equal to member 1 uid or member 2 uid if the document already exists but how do I make sure that security rule wouldn't fail for the first time. In simple words, I want to check whether document ID exists or not but also do not allow read/write access after the document is created. Is this even possible?
My code looks like this https://stackoverflow.com/a/46965065/10807253 right now.