Giter Site home page Giter Site logo

alexvfornazieri / firestore-backup-restore Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dalenguyen/firestore-backup-restore

1.0 2.0 0.0 69 KB

NPM package for backup and restore Firebase Firestore

Home Page: https://npmjs.com/package/firestore-export-import

License: MIT License

TypeScript 87.24% JavaScript 12.76%

firestore-backup-restore's Introduction

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.

// 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
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']) // Array of collection's name is OPTIONAL 
  .then(collections => {
    // You can do whatever you want with collections
    console.log(JSON.stringify(collections));
  })

Import data to firestore

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
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
      }
    }
  }
}

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!

firestore-backup-restore's People

Contributors

jcummings2 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.