Git Product home page Git Product logo

firebasetest's Introduction

Firebase logo FirebaseTest

This is a simple test with 🔥base which has a study purpose. The matter is simple: understand what is able to do a realtime distributed NoSQL database, which recently acquired by Google, who can send global updates about data to all subscribers.


Setup

Create a Firebase DB

Go to Firebase website and create an new profile. They'll provide you an URL for database access with a pattern like:

https://someFancyNameHere.firebaseio.com/

Connect to the db

Create a simple connection to the database using: var db = new Firebase('https://firebaseDbFancyNameHere.firebaseio.com/');


Usage

Get element reference

var collection = db.child('students');

Write data

To perform a database write there are 4 methods: set(), update(), transition() e push().

  • set(): writes or overwrites data, used also to remove data
db.child('name').set('Andrea');
db.child('poets').set({
  BBrecht: {
    firstName: 'Bertolt',
    lastName: 'Brecht',
    age: 55
  },
  OWilde: {
    firstName: 'Oscar',
    lastName: 'Wilde',
    age 35
  }
});
db.child('poets/OWilde').set({});
  • update(): updates data overwriting data nested in
db.child('poets').update({
  'BBrecht/firstname': 'B.',
  'OWilde/firstname': 'O.'
});
  • push(): adds and object to a collection using an auto generated key
db.child('posts').push({
  author: 'BBrecht',
  text: 'Hello to everyone'
});
  • transaction(): perform a database transaction, preventing race conditions
db.child('fixNumber').transaction(function(value){
  return (value || 0) + 1;
});

Events

On every set / update an event will be fired letting the other clients to get updated data.

Data read

Event handlers allow to receive updates from other clients using event handlers like value, child_added, child_changed, child_removed or child_moved. Every callback will receive a DataSnapshot object with a snapshot of the situation at that time. DataSnapshot can be inspected with key() and val() methods to gain key and value associated.

db.child('poets').on('value', function(snapshot){
  snapshot.forEach(function(poet){
    console.log("Key: " + poet.key() + " Value: " + poet.val());
  });
});

firebasetest's People

Contributors

atrogolo avatar

Watchers

 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.