Skip to content

Instantly share code, notes, and snippets.

@duzluk
Forked from brunobraga95/exportFirestoreDB.js
Created July 24, 2020 19:42
Show Gist options
  • Save duzluk/539ed8ac9dffe17485bffa857d76729e to your computer and use it in GitHub Desktop.
Save duzluk/539ed8ac9dffe17485bffa857d76729e to your computer and use it in GitHub Desktop.
exportFirestoreDB
const admin = require('firebase-admin');
var serviceAccount = require("./your-firestore-key.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
const dumped = {};
const schema = {
users: {
friends: {
messages: {},
},
groups: {
messages: {},
},
},
groups: {},
};
var db = admin.firestore();
const dump = (dbRef, aux, curr) => {
return Promise.all(Object.keys(aux).map((collection) => {
return dbRef.collection(collection).get()
.then((data) => {
let promises = [];
data.forEach((doc) => {
const data = doc.data();
if(!curr[collection]) {
curr[collection] = {
data: { },
type: 'collection',
};
curr[collection].data[doc.id] = {
data,
type: 'document',
}
} else {
curr[collection].data[doc.id] = data;
}
promises.push(dump(dbRef.collection(collection).doc(doc.id), aux[collection], curr[collection].data[doc.id]));
})
return Promise.all(promises);
});
})).then(() => {
return curr;
})
};
let aux = { ...schema };
let answer = {};
dump(db, aux, answer).then((answer) => {
console.log(JSON.stringify(answer, null, 4));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment