Skip to content

Instantly share code, notes, and snippets.

@susthitsoft
Forked from brunobraga95/exportFirestoreDB.js
Created August 1, 2018 18:19
Show Gist options
  • Save susthitsoft/0cd612ec2de4b90572c22e80f716c840 to your computer and use it in GitHub Desktop.
Save susthitsoft/0cd612ec2de4b90572c22e80f716c840 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));
});
@keeyong
Copy link

keeyong commented Aug 13, 2018

Sorry to ask some very naive question. I am new to Firestore (and GCP actually). I need to copy some collections from Firestore to Cloud Storage or Big Query. I am guessing I can modify this for that purpose but have no idea what to do with this:

1> Where is this script copying data to?
2> The answer for 1> depends on where I run this script?

Again sorry to ask very basic questions. Thanks!

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