Let Me Tink About It
Let Me Tink About It

Reputation: 16102

Pushing array to Firebase via REST API

Question

How do I (with a single HTTP request to the REST API) write an array to Firebase and give each array element a (non-integer) unique ID?

As described here.


Data

The data I have to write looks like the following.

data-to-write.js
myArray = [ {"user_id": "jack", "text": "Ahoy!"},
            {"user_id": "jill", "text": "Ohai!"} ];

Goal

When finished, I want my Firebase to look like this following.

my-firebase.firebaseio.com
{
  "posts": {
    "-JRHTHaIs-jNPLXOQivY": { // <- unique ID (non-integer)
      "user_id": "jack",
      "text": "Ahoy!"
    },
    "-JRHTHaKuITFIhnj02kE": { // <- unique ID (non-integer)
      "user_id": "jill",
      "text": "Ohai!"
    }
  }
}

I do not want it to look like this following...

my-anti-firebase.firebaseio.com
// NOT RECOMMENDED - use push() instead!
{
  "posts": {
    "0": {  // <- ordered array index (integer)
      "user_id": "jack",
      "text": "Ahoy!"
    },
    "1": {  // <- ordered array index (integer)
      "user_id": "jill",
      "text": "Ohai!"
    }
  }
}

I note this page where it says:

[...] if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase will render it as an array.

Code

Because I want to do this in a single HTTP request, I want to avoid iterating over each element in the array and, instead, I want to push a batch in a single request.

In other words, I want to do something like this:

pseudocode.js
curl -X POST -d '[{"user_id": "jack", "text": "Ahoy!"},
                  {"user_id": "jill", "text": "Ohai!"}]' \
                  // I want some type of batch operation here
  'https://my-firebase.firebaseio.com/posts.json'

However, when I do this, I get exactly what I describe above that I don't want (i.e., sequential integer keys).

I want to avoid doing something like this:

anti-pseudocode.js
for(i=0; i<=myArray.length; i++;){ // I want to avoid iterating over myArray
  curl -X POST -d '{"user_id": myArray[i]["user_id"],
                    "text": myArray[i]["text"]}' \
    'https://my-firebase.firebaseio.com/posts.json'
}

Is it possible to accomplish what I have described? If so, how?

Upvotes: 2

Views: 3441

Answers (3)

Firanto
Firanto

Reputation: 609

Your decision to use POST is correct. The one which cause numeric indexes as a key is because your payload is an array. Whenever you post/put and array, the key will always be indexes. Post your object one by one if you want the server generate key for you.

Upvotes: 1

Adarsh Madrecha
Adarsh Madrecha

Reputation: 7896

Firebase will generate unique ID only if you use POST. If you use PATCH unique ID is not generated.

Hence for the given case, will need to iterate through using some server/ client side code to save data in firebase.

Peseudo Code:

For each array 
   curl -X POST -d 
       "user_id": "jack",
       "text": "Ahoy!" 
    'https://my-firebase.firebaseio.com/posts.json'
Next

Upvotes: 0

Let Me Tink About It
Let Me Tink About It

Reputation: 16102

I don't think there is a way to use the Firebase API to do this as described in the OP.

However, it can be done with a server script as follows:

  1. Iterate through each array element.
  2. Assign each element a unique id (generated by server script).
  3. Create a return object with keys being the unique IDs and values being the corresponding array elements.
  4. Write object to Firebase with a single HTTP request using the patch method. Because post creates a new Firebase generated ID for the entire object itself. Whereas, patch does not; it writes directly to the parent node.
script.js
var myObject = {},
i = myArray.length;
while(i--){
  var key = function(){ /* return unique ID */ }();
  myObject[key] = myArray[i];
}
curl -X PATCH -d JSON.stringify(myObject) \
'https://my-firebase.firebaseio.com/posts.json'

Upvotes: 2

Related Questions