-
-
Save susthitsoft/0cd612ec2de4b90572c22e80f716c840 to your computer and use it in GitHub Desktop.
exportFirestoreDB
This file contains hidden or 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
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
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!