Created
November 24, 2023 04:39
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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