Created
December 20, 2023 14:10
-
-
Save UtmostCreator/324af86894d53270275d1e488db18b12 to your computer and use it in GitHub Desktop.
importing json file data into firebase using nodejs script
This file contains 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
// https://stackoverflow.com/questions/43951942/how-to-import-a-json-file-to-firebase-database | |
const firestoreService = require('firestore-export-import') | |
const serviceAccount = require('./serviceAccount.json') | |
const fs = require('fs') | |
const path = require('path') | |
const tempFileName = `${__dirname}${path.sep}data-temp.json`; | |
const admin = require('firebase-admin'); | |
// Initialize Firebase Admin SDK | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount) | |
}); | |
// Get a reference to the Firestore database | |
const db = admin.firestore(); | |
(async () => { | |
try { | |
const fileContents = fs.readFileSync(`${__dirname}/src/data.json`, 'utf8'); | |
const data = JSON.parse(fileContents); | |
const transformed = transformDataForFirestore(data); | |
fs.writeFileSync(tempFileName, JSON.stringify(transformed)); | |
await jsonToFirestore(); | |
fs.unlinkSync(tempFileName); | |
} catch (error) { | |
console.error('Error occurred during the import process:', error); | |
} | |
})(); | |
async function jsonToFirestore() { | |
try { | |
console.log('Initialzing Firebase') | |
await firestoreService.initializeFirebaseApp(serviceAccount) | |
console.log('Firebase Initialized') | |
await firestoreService.restore(db, tempFileName) | |
console.log('Upload Success') | |
} catch (error) { | |
console.log(error) | |
} | |
} | |
// In order to preserve ids in data.json | |
// as ids in firestore | |
// must use keyed object (id being the key) instead of array of records | |
function transformDataForFirestore(data) { | |
const collections = {...data}; // Create a shallow copy to avoid mutating the original 'data' | |
delete collections.stats; | |
const collectionsById = {}; | |
Object.keys(collections).forEach((collectionKey) => { | |
collectionsById[collectionKey] = {}; | |
// Check if 'collection' is an array before iterating | |
const collection = collections[collectionKey]; | |
if (Array.isArray(collection)) { | |
collection.forEach((record) => { | |
collectionsById[collectionKey][record.id] = {...record}; // Create a copy of 'record' to avoid modifying the original object | |
delete collectionsById[collectionKey][record.id].id; | |
}); | |
} else { | |
console.error(`The collection '${collectionKey}' is not an array.`); | |
// Decide how to handle this scenario, whether to skip or handle differently | |
} | |
}); | |
return collectionsById; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment