forked from dalenguyen/firestore-import-export
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added export function from firestore
- Loading branch information
Dale Nguyen
committed
Jan 17, 2018
1 parent
3c0d572
commit 0e40dab
Showing
5 changed files
with
3,086 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
serviceAccountKey.json | ||
firestore-export.json |
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
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 |
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
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!"); | ||
}); | ||
}) |
Oops, something went wrong.