-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.js
38 lines (32 loc) · 1015 Bytes
/
export.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var admin = require("firebase-admin");
var fs = require('fs');
var serviceAccount = require("./serviceAccountKey.json");
var collectionName = process.argv[2];
// You should replae databaseURL with your own
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<your-project-name>.firebaseio.com"
});
var db = admin.firestore();
var data = {};
data[collectionName] = {};
var results = db.collection(collectionName)
.get()
.then(snapshot => {
snapshot.forEach(doc => {
data[collectionName][doc.id] = doc.data();
})
return data;
})
.catch(error => {
console.log(error);
})
results.then(dt => {
// Write collection to JSON file | remember, everytime you export a collection, you need to copy the content of <firestore-export.json> to another json file as back-up file.
fs.writeFile("firestore-export.json", JSON.stringify(dt), function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
})