Firebase RTDB: updating/merging new with old data from CLI

喜你入骨 提交于 2019-12-10 15:02:10

问题


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?


回答1:


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}


来源:https://stackoverflow.com/questions/50996510/firebase-rtdb-updating-merging-new-with-old-data-from-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!