NPM package for backup and restore Firebase Firestore
You can export and import data from firestore with sub collection.
Install using npm.
npm install firestore-export-import
You can Generate New Private Key from Project Settings from Firebase Console.
After that you need to copy the databaseURL for initiating the App.
You have to import this package in a JavaScript file and work from there.
You can export collection and sub collection from your data. The sub collection is optional.
// In your index.js
const firestoreService = require('firestore-export-import');
const serviceAccount = require('./serviceAccountKey.json');
// Initiate Firebase App
firestoreService.initializeApp(serviceAccount, databaseURL);
// Start exporting your data
let result = firestoreService.backup('collection-name', 'sub-collection-optional');
result.then(data => console.log(JSON.stringify(data)))
This code will help you to import data from a JSON file to firestore
// In your index.js
const firestoreService = require('firestore-export-import');
const serviceAccount = require('./serviceAccountKey.json');
// Initiate Firebase App
firestoreService.initializeApp(serviceAccount, databaseURL);
// Start importing your data
// The array of date fields is optional
let result = firestoreService.restore('your-file-path.json', ['date1-field', 'date2-field']);
The JSON is formated as below. The collection name is test. first-key and second-key are document ids.
{
"test" : {
"first-key" : {
"email" : "[email protected]",
"website" : "dalenguyen.me",
"custom" : {
"firstName" : "Dale",
"lastName" : "Nguyen"
},
"date": {
"_seconds":1534046400,
"_nanoseconds":0
}
},
"second-key" : {
"email" : "[email protected]",
"website" : "google.com",
"custom" : {
"firstName" : "Harry",
"lastName" : "Potter"
},
"date": {
"_seconds":1534262435,
"_nanoseconds":0
}
}
}
}
This project is based on firestore-import-export, feel free to report bugs and make feature requests in the Issue Tracker, fork and create pull requests!