Created
January 29, 2018 01:13
-
-
Save brunobraga95/a2888e332988c71bc5c82bb99e4c9a64 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
(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