-
-
Save duzluk/539ed8ac9dffe17485bffa857d76729e to your computer and use it in GitHub Desktop.
exportFirestoreDB
This file contains 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