Skip to content

Instantly share code, notes, and snippets.

@steinbring
Created November 24, 2023 04:39
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 steinbring/38e35d7dbb516464d8488d4fbf17a015 to your computer and use it in GitHub Desktop.
Save steinbring/38e35d7dbb516464d8488d4fbf17a015 to your computer and use it in GitHub Desktop.
This node script syncs a JSON file to Firebase Cloud Firestore (make sure to "npm install firebase" first)
const { initializeApp } = require('firebase/app');
const { getFirestore, collection, addDoc } = require('firebase/firestore');
const fs = require('fs');
// Firebase configuration from your Firebase project
const firebaseConfig = {
apiKey: "XX",
authDomain: "XX",
projectId: "XX",
storageBucket: "XX",
messagingSenderId: "XX",
appId: "XX"
};
// Initialize Firebase
const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);
// Function to upload data to Firestore
async function uploadData(jsonData) {
for (const state in jsonData) {
const stateData = jsonData[state];
for (const category in stateData) {
const locations = stateData[category];
for (const location of locations) {
const docRef = await addDoc(collection(db, `${state}-${category}`), location);
console.log("Document written with ID: ", docRef.id);
}
}
}
}
// Read JSON data and start the upload process
fs.readFile('state-parks-and-forests.json', 'utf8', (err, data) => {
if (err) {
console.error("Error reading the file:", err);
return;
}
const jsonData = JSON.parse(data);
uploadData(jsonData)
.then(() => console.log("Data upload complete."))
.catch((error) => console.error("Error uploading data:", error));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment