Skip to content

Instantly share code, notes, and snippets.

@hotsen
Forked from kobvel/json-to-firestore.js
Created December 21, 2017 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotsen/c75514203b3c28c22ecf0aac27791a51 to your computer and use it in GitHub Desktop.
Save hotsen/c75514203b3c28c22ecf0aac27791a51 to your computer and use it in GitHub Desktop.
const admin = require('./node_modules/firebase-admin');
const serviceAccount = require("./service-key.json");
const data = require("./data.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://YOUR_DB.firebaseio.com"
});
data && Object.keys(data).forEach(key => {
const nestedContent = data[key];
if (typeof nestedContent === "object") {
Object.keys(nestedContent).forEach(docTitle => {
admin.firestore()
.collection(key)
.doc(docTitle)
.set(nestedContent[docTitle])
.then((res) => {
console.log("Document successfully written!");
})
.catch((error) => {
console.error("Error writing document: ", error);
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment