Skip to content

Instantly share code, notes, and snippets.

@timohausmann
Last active June 17, 2021 10:32
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 timohausmann/15200907c125194c7dbb6a45b6b1594d to your computer and use it in GitHub Desktop.
Save timohausmann/15200907c125194c7dbb6a45b6b1594d to your computer and use it in GitHub Desktop.
Two simple scripts to quickly export and import any collection data as JSON with node.js firebase-admin
const fs = require('fs');
const path = require('path');
const admin = require('firebase-admin');
const collectionName = 'characters';
(async () => {
/**
* How to get your serviceAccountKey:
* Go to https://console.firebase.google.com/
* Open Project Settings > Service Accounts
*/
var serviceAccount = require("./keys/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://YOUR_DATABASE_NAME.firebaseio.com"
});
const firestore = admin.firestore();
const collectionRef = firestore.collection(collectionName);
const querySnapshot = await collectionRef.get();
if (querySnapshot.empty) {
console.log(`Collection ${collectionName} is empty.`);
return;
}
console.log('Found', querySnapshot.size, 'documents.');
const json = {};
querySnapshot.forEach(function (documentSnapshot) {
json[documentSnapshot.id] = documentSnapshot.data();
});
const now = new Date().toISOString().replace(/:/g, '-').substring(0, 19);
const filePath = path.resolve(`./${collectionName}-${now}.json`);
console.log('writing file to', filePath, '...')
await fs.writeFile(filePath, JSON.stringify(json, null, 2), (err) => {
if (err) console.log(err);
});
})();
const admin = require('firebase-admin');
const collectionName = 'characters';
const importFile = 'characters-2021-06-17T09-23-40.json';
const importData = require(`./${importFile}`);
(async () => {
/**
* How to get your serviceAccountKey:
* Go to https://console.firebase.google.com/
* Open Project Settings > Service Accounts
*/
var serviceAccount = require("./keys/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://YOUR_DATABASE_NAME.firebaseio.com"
});
const firestore = admin.firestore();
const collectionRef = firestore.collection(collectionName);
const querySnapshot = await collectionRef.get();
if (querySnapshot.empty) {
console.log(`Collection ${collectionName} is empty.`);
} else {
console.log(`Collection ${collectionName} is not empty.`);
console.log('Found', querySnapshot.size, 'documents.');
}
//collect existing document IDs
const existingIds = [];
querySnapshot.forEach(function (documentSnapshot) {
existingIds.push(documentSnapshot.id);
});
for (let id in importData) {
//skip existing IDs
if (existingIds.indexOf(id) > -1) {
console.log(`Document ${id} already exists. Skipping ...`)
continue;
}
collectionRef.doc(id).set(importData[id]).then(documentReference => {
console.log(`✔ Created new document: ${id}`);
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment