Skip to content

Instantly share code, notes, and snippets.

@brunobraga95
Created January 29, 2018 01:13
Show Gist options
  • Save brunobraga95/a2888e332988c71bc5c82bb99e4c9a64 to your computer and use it in GitHub Desktop.
Save brunobraga95/a2888e332988c71bc5c82bb99e4c9a64 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));
});
@Harshad-Akhani
Copy link

(node:7184) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: 7 PERMISSION_DENIED: Missing or insufficient permissions.
(node:7184) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled
will terminate the Node.js process with a non-zero exit code.

this error after this script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment