Abdul Momen
Abdul Momen

Reputation: 407

Get a custom key generated when pushing to firebase

I am saving some data to firebase, I am using golang admin sdk. My problem is when I push object to firebase it generates an ID which is good for some use cases, but I don't need that what need is a custom integer key like 1,3,4..... The object may look like this after pushing.

enter image description here

I know I can set it by fetching all data then count them and create next id and add my object under that ID but I don't want this. Is there any way to achieve this in firebase automatically.

Upvotes: 0

Views: 439

Answers (1)

DIGI Byte
DIGI Byte

Reputation: 4174

You can technically do this but it requires some additional setup. You will have to maintain the current index in a 'global' position and increment it

there is a risk of write failure if there is a race condition between 2 or more users. to minimize this, you can look at using transactions and rules to control the flow of data.

Additionally, you can use cloud functions to process this if you don't want the client to handle the transaction and updating of the global counter.

Sources:

Cloud Functions

Server side increment

Rules for existing vs new data

Upvotes: 2

Related Questions