5

I have multiple (big) JSON files that I want to add to Firebase Real-time Database (RTBD). I'm using Geofire, so all the children nodes need to be under the same parent. I'm storing static geo-data but I want to periodically refresh it in pieces. If I can't refresh it in pieces, it becomes problematic to always guarantee that the one big JSON I am updating with is always 100% complete.

My desired data structure is like this:

{"parent" : {"child_a" : 1, "child_b" : 2}

Where my first JSON looks like this

$ cat 1.json
{"parent": {"child_a" : 1 }}

and my second like this

$ cat 2.json
{"parent": {"child_b" : 2 }}

I've tried using firebase-import with the --merge flag:

$ firebase-import --database_url https://my_db.firebaseio.com/ --path / --json 2.json --service_account auth.json --merge

And also tried using firebase-tools with the update flag:

$ firebase database:update / 2.json

But both are removing the contents of 1.json from the DB, and giving me this

{"parent": {"child_b" : 2 }}

How can I merge multiple JSON files into the same node, without overwriting the previous JSON content?


partial answer: Use set with merge, shown here for Firestore. But how can I do that with the CLI and Firebase?

philshem
  • 24,761
  • 8
  • 61
  • 127
  • 1
    Both of your files have a common node `parent`, and the update isn't going to merge the two children after the second invocation. It's going to overwrite the common parent entirely each time. I think you're either going to have to rewrite your JSON to contain just the children you want to add without the common parent, then use the parent name on the command line. Or write some code to do this in a script. – Doug Stevenson Jun 23 '18 at 03:15
  • @DougStevenson ah, now I understand. So take out the `geofire` key in the individual JSON, but then add it back on the command line `/geofire/`. I gotta test this... – philshem Jun 25 '18 at 09:26

1 Answers1

2

Based on the comment from Doug,

I can add individual keys to the parent node in multiple steps, without overwriting the existing children, by removing the top-level parent node from my JSON, and then adding it to the firebase-import command.

So from the example in the question, with no parent node in the JSON files and also the parent node in the --path flag of the import statement

$ cat 1.json
{"child_a" : 1 }

$ firebase-import --database_url https://my_db.firebaseio.com/ --path /parent --json 1.json --service_account auth.json --merge

and

$ cat 2.json
{"child_b" : 2 }

$ firebase-import --database_url https://my_db.firebaseio.com/ --path /parent --json 2.json --service_account auth.json --merge

gives me the data the way I need:

{"parent" : {"child_a" : 1, "child_b" : 2}
philshem
  • 24,761
  • 8
  • 61
  • 127