title: Firebase... Add/Update Firebase Using node.js Script
body: <p>I have arbitrary JSON that is sensibly laid out like this:</p>
  
  <pre><code>[
    {
      "id":100,
      "name":"Buckeye, AZ",
      "status":"OPEN",
      "address":{
        "street":"416 S Watson RD",
        "city":"Buckeye"
        ...
      }
    }
  ]
  </code></pre>
  
  <p>I've written a node.js script like this for proof of concept (why I'm using node is that the JS API seems better supported than REST or Ruby for this. I could be wrong):</p>
  
  <pre><code>http = require('http')
  Firebase = require('firebase')
  
  all_sites_url = "http://supercharge.info/service/supercharge/allSites"
  firebase_url  = "https://tesla-supercharger.firebaseio.com/"
  
  http.get(all_sites_url, (res) -&gt;
    body = ""
    res.on "data", (chunk) -&gt;
      body += chunk
      return
  
    res.on "end", -&gt;
      response = JSON.parse(body)
      all_sites = response
      send_to_firebase(response)
      return
  
    return
  ).on "error", (e) -&gt;
    console.log "Got error: ", e
    return
  
  send_to_firebase = (response) -&gt;
    firebase_ref = new Firebase(firebase_url)
  
    for charger in response
      console.log charger
      new_child = firebase_ref.push()
      new_child.set {id: charger.id, data: charger}, (error) -&gt;
        if error
          console.log "Data cound not be saved #{error}"
        else
          console.log "Data saved successfully"
  </code></pre>
  
  <p>The result is a unique id generated by Firebase, which has as a child a <code>data</code> and an <code>id</code> child. The <code>data</code> child has the expected information like <code>name</code>, <code>status</code>, etc.</p>
  
  <p>What I'd prefer is to generate a key-value pair. E.g., for an <code>id</code> of 100:</p>
  
  <pre><code>- 100
      - name
      - address
        street
        city
  </code></pre>
  
  <p>etc. So my first question is how to accomplish this or if it is even sensible.</p>
  
  <p>After the first time around, this data (call it the data from an external server) will be there and a mobile app will have added some fields. These are not present in the data already there. Next time I fetch data from the external server, I want to update things that have changed that the server would know about, like status. I don't want to tamper with things that only the mobile devices would know about like <code>remote_observations</code>.</p>
  
  <p>I know I'm seeming a bit dense here, but I'm trying to put together a sensible data model that will be updatable from that server using a CRON job and incrementally updatable from a bunch of mobile devices.</p>
  
  <p>Any help is much appreciated.</p>
  
  <p>UPDATE: I have found that this works for getting the structure I want:</p>
  
  <pre><code>send_to_firebase = (response) -&gt;
    firebase_ref = new Firebase(firebase_url)
  
    for charger in response
      firebase_ref.child(charger.id).update charger, (error) -&gt;
        if error
          console.log "Data could not be saved #{error}"
        else
          responses_pending += 1
          console.log "Data saved successfully : #{responses_pending} pending"
          firebase_ref.on 'value', -&gt;
            console.log "value received rp=#{responses_pending}"
            process.exit() if (responses_pending -= 1) &lt; 1
  </code></pre>