Skip to content

Instantly share code, notes, and snippets.

@JoeRoddy
Created October 18, 2017 16:19
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save JoeRoddy/1c706b77ca676bfc0983880f6e9aa8c8 to your computer and use it in GitHub Desktop.
Save JoeRoddy/1c706b77ca676bfc0983880f6e9aa8c8 to your computer and use it in GitHub Desktop.
Convert Firebase Database JSON to Firestore Collections
var db = firebase.firestore();
var content = require("./sourceData.json");
content &&
Object.keys(content).forEach(contentKey => {
const nestedContent = content[contentKey];
if (typeof nestedContent === "object") {
Object.keys(nestedContent).forEach(docTitle => {
firebase
.firestore()
.collection(contentKey)
.doc(docTitle)
.set(nestedContent[docTitle]);
});
}
});
@joelmarquez90
Copy link

You're a genius!!

@newmesiss
Copy link

master funciono de maravillla

@tomekszy
Copy link

What I should do if I get "Auth error:Error: read ECONNRESET" ? Only first few records are imported, and then this error appears for all other files. I used solution from https://stackoverflow.com/questions/46640981/how-to-import-csv-or-json-to-firebase-cloud-firestore, and it worked, but I don't want to get nested collections, so your script work for me best, but I don't know how to slow it down to get rid of this error. My node.js programming skills are below begginer :)

@nikmartin
Copy link

this will run you out of stack and memory if your input json is too large, as all those set calls are async

@gabrigcl
Copy link

Thank you!

@drveresh
Copy link

drveresh commented May 6, 2019

Worked perfectly, thanks allot JoeRoddy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment