Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
firestore-export-import
Advanced tools
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
OR
yarn add 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
// appName is optional, you can omit it.
const appName = '[DEFAULT]';
firestoreService.initializeApp(serviceAccount, databaseURL, appName);
// Start exporting your data
firestoreService
.backup('collection-name')
.then((data) => console.log(JSON.stringify(data)));
Sub collections will be added under 'subCollection' object.
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']) // Array of collection's name is OPTIONAL
.then((collections) => {
// You can do whatever you want with collections
console.log(JSON.stringify(collections));
});
This code will help you to import data from a JSON file to firestore. You have two options:
This will return a Promise<{status: boolean, message: string}>
Remember that, this action doesn't remove the collection. It will override or add new data to the collection. If you want to remove the current collection, you should do it from firebase console or using firebase firestore:delete
firebase firestore:delete [options] <<path>>
Usually the date, location & reference are not converted correctly when you backup the Firestore database. In order to import correctly, you have to pass to parameters for the options:
// Import options
const options = {
dates: ['date1', 'date1.date2', 'date1.date2.date3'],
geos: ['location', 'locations'],
refs: ['refKey'],
nested: true, // look up the data keys also in nested maps
};
After that, the data will be converted based on their types.
// In your index.js
const firestoreService = require('firestore-export-import');
const serviceAccount = require('./serviceAccountKey.json');
// Initiate Firebase App
// appName is optional, you can obmit it.
const appName = '[DEFAULT]';
firestoreService.initializeApp(serviceAccount, databaseURL, appName);
// Start importing your data
// The array of date, location and reference fields are optional
firestoreService.restore('your-file-path.json', {
dates: ['date1', 'date1.date2', 'date1.date2.date3'],
geos: ['location', 'locations'],
refs: ['refKey', 'arrayRef'],
});
import request from 'request-promise';
...
const backupData = await request('JSON-URL');
const status = await firestoreService.restore(JSON.parse(backupData), {
dates: ['date'],
geos: ['location']
});
The JSON is formated as below. The collection name is test. first-key and second-key are document ids.
{
"test": {
"first-key": {
"website": "dalenguyen.me",
"date": {
"_seconds": 1534046400,
"_nanoseconds": 0
},
"schedule": {
"time": {
"_seconds": 1534046400,
"_nanoseconds": 0
}
},
"three": {
"level": {
"time": {
"_seconds": 1534046400,
"_nanoseconds": 0
}
}
},
"custom": {
"lastName": "Nguyen",
"firstName": "Dale"
},
"location": {
"_latitude": 49.290683,
"_longitude": -123.133956
},
"locationNested": {
"geopoint": {
"_latitude": 49.290683,
"_longitude": -123.133956
}
},
"locations": [
{
"_latitude": 50.290683,
"_longitude": -123.133956
},
{
"_latitude": 51.290683,
"_longitude": -123.133956
}
],
"email": "dungnq@itbox4vn.com",
"secondRef": "test/second-key",
"arrayRef": ["test/second-key", "test/second-key"],
"nestedRef": {
"secondRef": "test/second-key"
},
"subCollection": {
"test/first-key/details": {
"33J2A10u5902CXagoBP6": {
"dogId": "2",
"dogName": "hello"
},
"MSZTWEP7Lewx0Qr1Mu5s": {
"dogName": "lala",
"dogId": "2"
}
},
"test/first-key/contacts": {
"33J2A10u5902CXagoBP6": {
"contactId": "1",
"name": "Dale Nguyen"
},
"MSZTWEP7Lewx0Qr1Mu5s": {
"contactId": "2",
"name": "Yen Nguyen"
}
}
}
},
"second-key": {
"website": "google.com",
"date": {
"_seconds": 1534262435,
"_nanoseconds": 0
},
"custom": {
"lastName": "Potter",
"firstName": "Harry"
},
"location": {
"_latitude": 49.290683,
"_longitude": -123.133956
},
"email": "test@dalenguyen.me"
}
}
}
It works the same way as above. However the structure of JSON file is different. It's an array of documents.
// import-array-to-firestore.json
{
"test": [
{
"name": "Dale Nguyen",
"email": "dale@dalenguyen.me",
"subCollection": {
"details": [
{
"dogId": "2",
"dogName": "hello"
},
{
"dogName": "lala",
"dogId": "2"
}
]
}
},
{
"name": "Yen Nguyen",
"email": "yenchan@gmail.com"
},
{
"name": "Harry Potter",
"email": "harry@potter.me"
}
]
}
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!
FAQs
NPM package for backup and restore Firebase Firestore
The npm package firestore-export-import receives a total of 3,431 weekly downloads. As such, firestore-export-import popularity was classified as popular.
We found that firestore-export-import demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.