Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Firebase

 Firebase

Introduction to Firebase: web API and hosting

Leszek Rybicki

July 15, 2016
Tweet

More Decks by Leszek Rybicki

Other Decks in Programming

Transcript

  1. Initialize <script src="https://www.gstatic.com/firebasejs/3.2.0/firebase.js"></script> <script> // Initialize Firebase var config =

    { apiKey: "AIzaSyAxMx5ZJpxyS1Va4A-A1xhOwpiOZZzbopM", authDomain: "abecon-3c221.firebaseapp.com", databaseURL: "https://abecon-3c221.firebaseio.com", storageBucket: "abecon-3c221.appspot.com", }; firebase.initializeApp(config); </script>
  2. Reference let db = new firebase.database() var usersRef = db.ref('users')

    usersRef.child(‘tetsuri’) usersRef.key() // users A reference identifies an address in the database.
  3. Listing nodes let query = usersRef .orderByKey() .limitToLast(5) query.once('value', function(snapshot){})

    query.off(‘value’) query.on(‘child_added’, function(child){}) query.on(‘child_removed’, function(child){}) query.on(‘child_moved’, function(child){}) query.on(‘child_changed’, function(child){}) A query sends events when its results change. We can listen to child CRUD events and an even on any value change.
  4. Snapshot // Test for the existence of certain keys within

    a DataSnapshot var ref = firebase.database().ref("users/ada"); ref.once("value").then(function(snapshot) { var name = snapshot.child("name").val(); // { first: "Ada", last: "Lovelace"} var firstName = snapshot.child("name/first").val(); // "Ada" var lastName = snapshot.child("name").child("last").val(); // "Lovelace" var age = snapshot.child("age").val(); // null });
  5. Snapshot’s children // Iterating over children in a snapshot var

    ref = firebase.database().ref("users"); ref.once("value").then(function(snapshot) { snapshot.forEach(function(childSnapshot) { var key = childSnapshot.key; // childData will be the actual contents of the child var childData = childSnapshot.val(); }); });
  6. Creating nodes // create a child with key “ada” and

    set its value users.child(‘ada’).set({ first: 'Ada', last: 'Lovelace' })
  7. Changing node values // change the value of “ada” child

    node. users.child(‘ada’).set({ first: 'Ada', last: 'Lovelace' }) // same as create!
  8. Removing nodes // remove ada var removed = users.child(‘ada’).remove() //

    returns Promise removed.then(function() { console.log(‘remove succeeded’) }).catch(function() { console.log(‘problem removing’) })
  9. Example let db = new firebase.database() var usersRef = db.ref('users')

    usersRef.set({ taro: “Taro Yoshida”, tetsuri: “Tetsuri Moriya” }) usersRef.on(‘child_removed’, child => alert(“Delete user” + child.val()) ) usersRef.child(“tetsuri”).remove() Trigger an event when a child node is removed from the referenced node.
  10. Hosting [leszek:~/Documents/megakanban]$ firebase deploy === Deploying to 'megakanban'... i deploying

    database, hosting ✔ database: rules ready to deploy. i hosting: preparing public directory for upload... ✔ hosting: 2 files uploaded successfully i starting release process (may take several minutes)... ✔ Deploy complete! Hosting Site: https://megakanban.firebaseapp.com Dashboard: https://console.firebase.google.com/project/megakanban/overview Visit the URL above or run firebase open