firestore-export-import
NPM package for backup and restore Firebase Firestore
You can export and import data from firestore with sub collection.
Installation
Install using npm.
npm install firestore-export-import
Get Google Cloud Account Credentials from Firebase
You can Generate New Private Key from Project Settings from Firebase Console.
After that you need to copy the databaseURL for initiating the App.
Usage
You have to import this package in a JavaScript file and work from there.
Export data from firestore
You can export collection and sub collection from your data. The sub collection is optional.
const firestoreService = require('firestore-export-import');
const serviceAccount = require('./serviceAccountKey.json');
firestoreService.initializeApp(serviceAccount, databaseURL);
firestoreService
.backup('collection-name', 'sub-collection-optional')
.then(data => console.log(JSON.stringify(data)))
Get all collections data
This is a suggestion from jcummings2 and leningsv
The ['collectionName1', 'collectionName2'] is OPTIONAL, you can remove this parameter to get all of the current collections in your firestore.
The result is an object of collection's data.
firestoreService
.backups(['collectionName1', 'collectionName2'])
.then(collections => {
console.log(JSON.stringify(collections));
})
Import data to firestore
This code will help you to import data from a JSON file to firestore.
This will return a Promise<{status: boolean, message: string}>
const firestoreService = require('firestore-export-import');
const serviceAccount = require('./serviceAccountKey.json');
firestoreService.initializeApp(serviceAccount, databaseURL);
firestoreService.restore('your-file-path.json', ['date1', 'date2'], ['location1', 'location2']);
The JSON is formated as below. The collection name is test. first-key and second-key are document ids.
{
"test" : {
"first-key" : {
"email" : "dungnq@itbox4vn.com",
"website" : "dalenguyen.me",
"custom" : {
"firstName" : "Dale",
"lastName" : "Nguyen"
},
"date": {
"_seconds":1534046400,
"_nanoseconds":0
},
"location": {
"_latitude": 49.290683,
"_longitude": -123.133956
}
},
"second-key" : {
"email" : "test@dalenguyen.me",
"website" : "google.com",
"custom" : {
"firstName" : "Harry",
"lastName" : "Potter"
},
"date": {
"_seconds":1534262435,
"_nanoseconds":0
},
"location": {
"_latitude": 49.290683,
"_longitude": -123.133956
}
}
}
}
Contributions
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!