Skip to content

Commit

Permalink
Added export function from firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
Dale Nguyen committed Jan 17, 2018
1 parent 3c0d572 commit 0e40dab
Show file tree
Hide file tree
Showing 5 changed files with 3,086 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
serviceAccountKey.json
firestore-export.json
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
A script that help to export and import in Cloud Firestore

# Requirements

You need [NODE](https://nodejs.org/en/download/) or something that can run JAVASCRIPT (JS) file.

Get **serviceAccount** JSON file from *Project Setting > SERVICE ACCOUNTS* in Firebase Console

# Export database from Firestore

This will help you create a backup of your collection from Firestore to a JSON file name **firestore-export.json**

```
node export.js <your-collection-name>
```

# Import database to Firestore
37 changes: 37 additions & 0 deletions export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var admin = require("firebase-admin");
var fs = require('fs');
var serviceAccount = require("./serviceAccountKey.json");

var collectionName = process.argv[2];

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://ionic-firestore-dn.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
fs.writeFile("firestore-export.json", JSON.stringify(dt), function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
})
Loading

0 comments on commit 0e40dab

Please sign in to comment.