step-up-labs / firebase-database-dotnet

C# library for Firebase Realtime Database.
MIT License
671 stars 170 forks source link

Setting custom key when pushing new data to firebase database #265

Closed saamerm closed 2 years ago

saamerm commented 2 years ago

Thank you for this awesome package!

I have been raking my brain for weeks trying to figure out how to set a custom key when pushing new data to the firebase real time database.

I was trying to do this to push the data, while hoping to use the UID as the key:

await firebase.Child("ContactUs").Child(UID)
            .PostAsync<ContactusModel>(new ContactusModel()
            { FirstName = contactus.FirstName, LastName = contactus.LastName, PhoneNumber = contactus.PhoneNumber, Email = contactus.Email, Description = contactus.Description, MainIssue = contactus.MainIssue });
            return true;

However, as you notice, it was adding another child with ID inside my UID:

ContactUs

I tried to search far and wide but the API's in the Google Firebase docs for Real time database are different than this package and several functions meant to do this aren't available. Close to giving up I decided to try PutAsync instead of PushAsync like this:

await firebase.Child("ContactUs").Child(UID)
            .PutAsync<ContactusModel>(new ContactusModel()
            { FirstName = contactus.FirstName, LastName = contactus.LastName, PhoneNumber = contactus.PhoneNumber, Email = contactus.Email, Description = contactus.Description, MainIssue = contactus.MainIssue });
            return true;

Miraculously it seems to have fixed the issue of the extra child, but I have no clue why. I also checked the docs and the source code to see what's going on but I have no clue.

ContactUs

Does any one have an explanation for this behavior? And is it possible use PostAsync instead of PutAsync since Put is supposed to be meant for updating and not creating a new item?

ghost commented 2 years ago

Came here cause started using this a few hours ago and noticed the exact same thing, and was wondering if there was any downside (other than it being a Put request) in using Update to "Upsert" records, as I need my keys to match too lol

cabauman commented 2 years ago

No downside. PutAsync just acts as an upsert (like you said), and is the way to achieve custom keys when using this particular library.

dave-sekula commented 2 years ago

Cool - thanks for clarifying - perfect for what I need :)

cabauman commented 2 years ago

For the original poster, so you don't have to just take my word for it, here's a short excerpt from the docs:

Since each user will have a unique username, it makes sense to use PUT here instead of POST since we already have the key and don't need to create one.

Post is designed to create a new child node with a generated ID under the current path. Put doesn't create a new child node; it just operates on the current path.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 2 years ago

Closing the issue due to inactivity. Feel free to re-open