1

I'm using react and firebase real-time database. I want to create a list for each user. A list of id to later look for files in storage. In this case what is the best method to generate and add an id to list in database? (Not list of data, the data stored in the database is the list)

So I have two methods in mind. I'll write in pseudo code

//Method 1
//get id list
receivedList = getKeyListFromDB()
//generate ID
newID = generateID()
// append new id
receivedList.append(newID)
//set value with updated list
updateDB(receivedList)

//Method 2
// Using the list provided by the firebase
newRef = firebase.database().ref().child('pathToList').push()
newRef.set({
      key: newRef.key
    })

I was going for method 1, but couldn't find a way to generate id. And retreiving the list to update it seemed inefficient. So I tried to find another method. But I can't find a good way to do this.

Solution to add a value to list in database

I have solved using transaction method. But if there is a way to solve by using push please do answer.

3 Answers 3

2

There are many reasons why Firebase has a built-in push() operation to add a child node to a list. In fact, you've already listen one of them in your question:

retreiving the list to update it seemed inefficient

Other reasons are that push IDs will work while your app is offline, and that they scale much better across multiple users.

If you want to learn more about why you should use push(), read Best Practices: Arrays in Firebase.

Sign up to request clarification or add additional context in comments.

Comments

1

First, you can generate a unique key by using below code

const newID = firebase.database.ref().push().key

According to this answer, whenever you use push on a Database Reference, a new data node is generated with a unique key that includes the server timestamp. These keys look like -KiGh_31GA20KabpZBfa.

Because of the timestamp, you can be sure that the given key will be unique, without having to check the other keys inside your database.

Second, you can add an item to a list in database using transaction.

const newID = firebase.database.ref().push().key
//user is user object from firebase auth module
const newDatabaseRouteRef = firebase.database.ref().child('users/' + user.uid + '/ids')
// if null, create a new list with id, if not add newID to list
newDatabaseRouteRef.transaction((currentValue) => {
  return currentValue ? currentValue.push(newID) : [newID]
})

Comments

0

I'm creating another answer just to show different kinds of solutions. This solution, using push is better than using transaction.

// Get a new ref by push
const newDatabaseRouteRef = firebase.database.ref().child('users/' + user.uid + '/routes').push()
// Set the value to the key of received ref
newDatabaseRouteRef.set(newDatabaseRouteRef.key)

This is better because

  1. Transaction needs multiple round trips where push() doesn't, thus reducing time.
  2. Is more safe according to this article.

This is achieves the same result because even using list, when the list is saved in firebase database, it is stored as object. ex) ["a","b","c"] => {0:"a",1:"b",2:"c"}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.