Simple tools for Ionic2 and Firebase development - davidgeller/public-docs GitHub Wiki

It’s been a few weeks now since I jumped into the Ionic2 and Firebase pool. For a while I was neck-deep trying to figure things out. Ionic2, being relatively new and still in beta, presented vast challenges due to brittle code and sparse documentation. Firebase, while quite mature, was still challenging because it was new to me and some of its linkages to Ionic2 / Angular2 were also new.

###Testing###

Testing during development is critically important. While module and code-specific tests are important, I also just wanted a simple way to start up Ionic and always view my project in Chrome (I normally use Safari as my desktop browser). This shell script made it easy:

#!/bin/sh
ionic serve --browser “Google Chrome”

To use it, change its permissions to execute (chmod +x debug.sh) and then run it through ./debug.sh.

chmod +x debug.sh

Run the script:

./debug.sh

###Deploying###

After extensive testing you can deploy your application directly to firebase where it will be served directly from their servers. This saves you the hassle of maintaining your own web server. Additionally, Google's Firebase hosting platform providing a primitive versioning feature which lets you roll-back previously deployed versions of your app, if necessary.

Previously I was deploying to an Amazon S3-hosted storage space and the Firebase alternative is much faster for uploading.

Here's a script to take my www directory and push it up to Firebase:

#!/bin/sh
npm run ionic:build --prod
firebase deploy

Make the file executable the same way you did for the debug.sh script. For that to work and for it to know to push up www I created a firebase.json configuration file that looks like:

{
  "database": {
    "rules": "database.rules.json"
  },
  "hosting": {
    "public": "www"
  }
}

###Backing Up Your Data###

Did you know you could easily setup a simple backup scheme for your Firebase data that pulls it down from Firebase and stores it on your own computer (or wherever you want to save it)? Thankfully Google created the Firebase command-line-interface (CLI) and set of tools deployed through an npm package named firebase-tools. Install it via:

npm install -g firebase-tools

Once you've got it installed and you've logged into your Firebase account you'll be able to use it to perform some fairly sophisticated actions against your data. Here's my script for creating a simple backup file with the date and time appended to it for uniqueness:

#!/bin/sh
NOW=$(date +"%Y-%m-%dT%H:%M:%S")
firebase database:get -o "db_backup-$NOW.json" --export /

Make it executable (see previous example) and you're ready to use it by just running:

./backup.sh

You can modify it to save portions of your data tree or everything starting at root, as I have done. Additionally, you can create a cron job to pull a copy of your data at whatever interval you like.