DEV Community

Erik Guzman
Erik Guzman

Posted on • Originally published at notion.so

Stream 11/13/19 - npx is pretty cool

Check Out the Stream @ https://twitch.tv/talk2megooseman

Today Objective

✅ Work on a fork of

GitHub logo talk2MeGooseman / firestore-backup-restore

NPM package for backup and restore Firebase Firestore

firestore-export-import

GitHub version Build Status David badge

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
to experiment adding npx support to it

Notes

NPX

During off stream time I was looking for a good way to export Firestore data that I have for a couple of my extension s so that I can import into any other DB service of my choice.

Unfortunately Firebase does not offer a clean export tool, instead, you can only export to their propriety format. BUT I ended finding an NPM package that someone wrote to export data to JSON: firestore-export-import

I was lazy and didn't want to spend time integrating this package into any project I have. So instead I got the crazy idea, "What if I made this npx compatible?" And here we are now.

So I forked the repo and start laying the groundwork for creating npx compatible version of this package.

I got alot of my inspiration of how to do this from the cowsay package:

GitHub logo piuccio / cowsay

cowsay is a configurable talking cow

cowsay

 __________________
< srsly dude, why? &gt
 ------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

cowsay is a configurable talking cow, originally written in Perl by Tony Monroe

This project is a translation in JavaScript of the original program and an attempt to bring the same silliness to node.js.

Install

npm install -g cowsay

Usage

cowsay JavaScript FTW

or

cowthink node.js is cool

It acts in the same way as the original cowsay, so consult cowsay(1) or run cowsay -h

 ________
< indeed &gt
 --------
    \
     \
                                   .::!!!!!!!:.
  .!!!!!:.                        .:!!!!!!!!!!!!
  ~~~~!!!!!!.                 .:!!!!!!!!!UWWW$$$
      :$$NWX!!:           .:!!!!!!XUWW$$$$$$$$$P
      $$$$$##WX!:      .<!!!!UW$$$$"  $$$$$$$$#
      $$$$$  $$$UX   :!!UW$$$$$$$$$   4$$$$$*
      ^$$$B  $$$$\     $$$$$$$$$$$$   d$$R"
        "*$bd$$$$      '*$$$$$$$$$$$o+#"
             """"          """""""

Usage as a module

cowsay can be used as any other npm dependency

var cowsay = require("cowsay");

console.log(cowsay.say({
    text : "I'm a moooodule",
    e : "oO",
    T : "U "
}));

// or cowsay.think()
 _________________
( I'm a

After completing the work and getting the MVP done for import and export, next came refactor phase. One thing that I realized needed a refactoring was the usage of optimist

It has been deprecated for a long time so I wanted to look for a better alternative. Luckily ABuffSeagull came flying in and recommended commander

The end result is the it worked out great and I feel like the command and options structure feels a lot better.

Shoutouts and Thanks

ABuffSeagull

  • Thanks for recommending commander.js and an alternative to optimist cli package commander

Kodder

  • 2 month resub!

Booperinos

  • Thanks for the 18 months of sub love!

Future action items

⬜ Wrap up doing a pull request for firestore-backup-restore. But its a done MVP <3

GitHub logo talk2MeGooseman / firestore-backup-restore

NPM package for backup and restore Firebase Firestore

firestore-export-import

GitHub version Build Status David badge

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

Top comments (5)

Collapse
 
pavelloz profile image
Paweł Kowalski

I wish we used yargs instead of commander for our company cli tool. It has been problematic over the years and it seems like it wants to do too much making it harder to introduce new devs into the project.

Collapse
 
talk2megooseman profile image
Erik Guzman

Oh interesting! I am still new to libraries to use to make a good CLI interface. What would you say are the best reason to use yargs? Ill take a look at it since I am making another npx script for another project. Thanks for commenting and suggesting it.

Collapse
 
pavelloz profile image
Paweł Kowalski

The best reason to use yargs for me is that i had a lot of issues with commander and a lot of serious tools are written using yargs. It also looks lighter/simpler/more focused on args parsing, which is nice.

Thread Thread
 
talk2megooseman profile image
Erik Guzman

Thanks a lot for the suggestion, Ill be switching to yargs on a new npx project since I like the offerings over commander.

Collapse
 
talk2megooseman profile image
Erik Guzman

Took a quick look at yargs and HOT DAMN it looks good. Importing a directory full of command modules? Yes, please. Thanks for telling me about this.