项目作者: dalenguyen

项目描述 :
NPM package for backup and restore Firebase Firestore
高级语言: TypeScript
项目地址: git://github.com/dalenguyen/firestore-backup-restore.git
创建时间: 2018-05-03T13:32:42Z
项目社区:https://github.com/dalenguyen/firestore-backup-restore

开源协议:MIT License

下载


firestore-export-import

GitHub version
Build Status

NPM package for backup and restore Firebase Firestore

You can export and import data from firestore with sub collection.

Installation

Install using npm.

  1. npm install firestore-export-import
  2. OR
  3. yarn add 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.

Initialize Firebase App

You have initialize the Firebase App in order to use Firestore service. It doesn’t matter if you initialize it by using this plugin method or the offical way.

  1. const { initializeFirebaseApp } = require('firestore-export-import')
  2. const serviceAccount = require('./serviceAccountKey.json')
  3. // If you want to pass settings for firestore, you can add to the options parameters
  4. const options = {
  5. firestore: {
  6. host: 'localhost:8080',
  7. ssl: false,
  8. },
  9. }
  10. // Initiate Firebase App
  11. // appName is optional, you can omit it.
  12. const appName = '[DEFAULT]'
  13. const firestore = initializeFirebaseApp(serviceAccount, appName, options)
  14. // the appName & options are OPTIONAL
  15. // you can initialize the app without them
  16. // const firestore = initializeFirebaseApp(serviceAccount)

Export data from firestore

You can export collection and sub collection from your data. The sub collection is optional.

Export options - OPTIONAL

  1. // Export options
  2. const options = {
  3. docsFromEachCollection: 10, // limit number of documents when exporting
  4. refs: ['refKey', 'deep.level.key'], // reference Path
  5. }
  1. // In your index.js
  2. const { backup } = require('firestore-export-import')
  3. // Start exporting your data
  4. backup(firestore, 'collection-name', options).then((data) =>
  5. console.log(JSON.stringify(data))
  6. )

Export data from document

Backup a document with sub collections

  1. // you can pass options as a third option - optional
  2. backupFromDoc(firestore, 'collection-name', 'document-id').then((data) =>
  3. console.log(JSON.stringify(data))
  4. )

Sub collections will be added under ‘subCollection’ object.

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.

  1. const { backups } = require('firestore-export-import')
  2. backups(firestore, ['collectionName1', 'collectionName2']) // Array of collection's name is OPTIONAL
  3. .then((collections) => {
  4. // You can do whatever you want with collections
  5. console.log(JSON.stringify(collections))
  6. })

Export data with query

You are can back update based on query criteria. In this example, I am backing up all data from users collection, where name equals Dale Nguyen.

  1. const queryByName = (collectionRef) =>
  2. collectionRef.where('name', '==', 'Dale Nguyen').get()
  3. const users = await backup(firestore, 'users', {
  4. queryCollection: queryByName,
  5. })

Import data to firestore (Predefined Document Id)

This code will help you to import data from a JSON file to firestore. You have two options:

  • Restore from a JSON file from your local machine
  • Restore from a JSON from a HTTP request

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

  1. firebase firestore:delete [options] <<path>>

Import / Restore Options

This is the options for the restore function. All of them are optional.

  1. export interface IImportOptions {
  2. dates?: string[]
  3. autoParseDates?: boolean
  4. geos?: string[]
  5. autoParseGeos?: boolean
  6. refs?: string[]
  7. showLogs?: boolean
  8. }

For local JSON

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:

  1. // Import options
  2. const options = {
  3. dates: ['date1', 'date1.date2', 'date1.date2.date3'],
  4. geos: ['location', 'locations'],
  5. refs: ['refKey'],
  6. }

If you don’t want to specify dates, you can use another parameter in order to transform fields to date automatically.

  1. // Import options with auto parse date
  2. const options = {
  3. autoParseDates: true // use this one in stead of dates: [...]
  4. geos: ['location', 'locations'],
  5. refs: ['refKey'],
  6. };

After that, the data will be converted based on their types.

  1. // In your index.js
  2. const { initializeFirebaseApp, restore } = require('firestore-export-import')
  3. const serviceAccount = require('./serviceAccountKey.json')
  4. // Initiate Firebase App
  5. // appName is optional, you can omit it.
  6. const appName = '[DEFAULT]'
  7. const firestore = initializeFirebaseApp(serviceAccount, databaseURL, appName)
  8. // Start importing your data
  9. // The array of date, location and reference fields are optional
  10. restore(firestore, 'your-file-path.json', {
  11. dates: ['date1', 'date1.date2', 'date1.date2.date3'],
  12. geos: ['location', 'locations'],
  13. refs: ['refKey', 'arrayRef'],
  14. })

For HTTP Request

  1. import request from 'request-promise';
  2. ...
  3. const backupData = await request('JSON-URL');
  4. const status = await restore(firestore, JSON.parse(backupData), {
  5. dates: ['date'],
  6. geos: ['location']
  7. });

The JSON is formatted as below. The collection name is test. first-key and second-key are document ids.

  1. {
  2. "test": {
  3. "first-key": {
  4. "website": "dalenguyen.me",
  5. "date": {
  6. "_seconds": 1534046400,
  7. "_nanoseconds": 0
  8. },
  9. "schedule": {
  10. "time": {
  11. "_seconds": 1534046400,
  12. "_nanoseconds": 0
  13. }
  14. },
  15. "three": {
  16. "level": {
  17. "time": {
  18. "_seconds": 1534046400,
  19. "_nanoseconds": 0
  20. }
  21. }
  22. },
  23. "custom": {
  24. "lastName": "Nguyen",
  25. "firstName": "Dale"
  26. },
  27. "location": {
  28. "_latitude": 49.290683,
  29. "_longitude": -123.133956
  30. },
  31. "locationNested": {
  32. "geopoint": {
  33. "_latitude": 49.290683,
  34. "_longitude": -123.133956
  35. }
  36. },
  37. "locations": [
  38. {
  39. "_latitude": 50.290683,
  40. "_longitude": -123.133956
  41. },
  42. {
  43. "_latitude": 51.290683,
  44. "_longitude": -123.133956
  45. }
  46. ],
  47. "email": "dungnq@itbox4vn.com",
  48. "secondRef": "test/second-key",
  49. "arrayRef": ["test/second-key", "test/second-key"],
  50. "nestedRef": {
  51. "secondRef": "test/second-key"
  52. },
  53. "subCollection": {
  54. "test/first-key/details": {
  55. "33J2A10u5902CXagoBP6": {
  56. "dogId": "2",
  57. "dogName": "hello"
  58. },
  59. "MSZTWEP7Lewx0Qr1Mu5s": {
  60. "dogName": "lala",
  61. "dogId": "2"
  62. }
  63. },
  64. "test/first-key/contacts": {
  65. "33J2A10u5902CXagoBP6": {
  66. "contactId": "1",
  67. "name": "Dale Nguyen"
  68. },
  69. "MSZTWEP7Lewx0Qr1Mu5s": {
  70. "contactId": "2",
  71. "name": "Yen Nguyen"
  72. }
  73. }
  74. }
  75. },
  76. "second-key": {
  77. "website": "google.com",
  78. "date": {
  79. "_seconds": 1534262435,
  80. "_nanoseconds": 0
  81. },
  82. "custom": {
  83. "lastName": "Potter",
  84. "firstName": "Harry"
  85. },
  86. "location": {
  87. "_latitude": 49.290683,
  88. "_longitude": -123.133956
  89. },
  90. "email": "test@dalenguyen.me"
  91. }
  92. }
  93. }

Import data to firestore (auto generate document id)

It works the same way as above. However the structure of JSON file is different. It’s an array of documents.

  1. // import-array-to-firestore.json
  2. {
  3. "test": [
  4. {
  5. "name": "Dale Nguyen",
  6. "email": "dale@dalenguyen.me",
  7. "subCollection": {
  8. "details": [
  9. {
  10. "dogId": "2",
  11. "dogName": "hello"
  12. },
  13. {
  14. "dogName": "lala",
  15. "dogId": "2"
  16. }
  17. ]
  18. }
  19. },
  20. {
  21. "name": "Yen Nguyen",
  22. "email": "yenchan@gmail.com"
  23. },
  24. {
  25. "name": "Harry Potter",
  26. "email": "harry@potter.me"
  27. }
  28. ]
  29. }

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!