1.0.6 • Published 9 years ago

moiz-firebase v1.0.6

Weekly downloads
7
License
MIT
Repository
-
Last release
9 years ago

moiz-firebase

moiz-firebase is a more easy (Moiz) firebase API. It provides a simple and easy to use data access layer based for Firebase with reactive extension (rxjs).

Installation

First install node.js and sign up for Firebase. Then:

About Firebase

Firebase is database with a tree structure. Data in Firebase is stored in key-value pair.

$ npm install moiz-firebase

Prerequisite

Create new Firebase Database and then click "Add Firebase to your web app" to retrive configuration or connection details

// configuration from Firebase 

var config = {
    apiKey: "AIzaSyB6qU0Y7mVOEr_PjdIy9dsBgRpUJs_taBo",
    authDomain: "wesshop-332d6.firebaseapp.com",
    databaseURL: "https://xxxx-332d6.firebaseio.com",
    storageBucket: "xxx-332d6.appspot.com",
    messagingSenderId: "xxx"
 };
 

The above code is the connection detail required by Firebase

Using moiz-firebase

We will use Typescript (more like ES6) syntax in our example. However you can use ES 5 syntax if you like.

Please note we will be using Typescript generics in our example. We will use "any" as type argument as I want to keep simple for documentation purpose. In your project you can replace with some of your own type like or .

import { Db } from 'moiz-firebase'; 

Create Data Access Layer object

let db = new Db();  

Configure Data Access Layer to use connection setting from Firebase

db.configure(config);  

Create Data

The below method creates or appends item under "/products" with 2 keys values (title , price) with an Firebase auto generated Id

db.create<any>('products', {title : 'Sample Product' , price : 99}) 
    .subscribe(
                data => console.log('Success' ),
                err => console.log('Failure ', err)
            );

Read Data

Read value at specified location

The below code shows we are reading value at specific node

db.getValue<any>('countries/au/vic')
    .subscribe(
                data => console.log('Retreived data ' , data ),
                err => console.log('Failure ', err)
            );

Read all child nodes at specified location

db.getAll<any>('products')
    .subscribe(
                data => console.log('Retreived data ' , data ),
                err => console.log('Failure ', err)
            );

Read one item at specified location using Firebase auto generated Id

db.getById<any>('products', '-KWvhHBg1qMF669mLaU9')
    .subscribe(
                data => console.log('Retreived data ' , data ),
                err => console.log('Failure ', err)
            );

Update Data

Update / Create all the content at a particular node

 db.setValue<any>('countries/au',
              {
                'vic': 
                  {
                      name : 'Victoria',
                      capital : 'Melbourne'
                  },
                  'nsw': 
                  {
                      name : 'New South Wales',
                      capital : 'Sydney'
                  },
                  'sa': 
                  {
                      name : 'South Australia',
                      capital : 'Adelaide'
                  }
            })
    .subscribe(
                data => console.log('Success'  ),
                err => console.log('Failure ', err)
            );

Update only specified keys at a particular node

db.update<any>('countries/au',
              {
                'vic': 
                  {
                      name : 'Victoria',
                      capital : 'Melbourne'
                  }
            })
    .subscribe(
                data => console.log('Success'  ),
                err => console.log('Failure ', err)
            );

The above code will update details of node with key "vic" at location "countries/au".

Delete Data

The code below will delete at location "products" with Firebase auto generated id "-KWvhHBg1qMF669mLaU9".

db.remove('products', '-KWvhHBg1qMF669mLaU9')
 .subscribe(
                data => console.log('Delete uccess' ),
                err => console.log('Failure ', err)
            );

The code below will delete location node "products" .

db.remove('products')
  .subscribe(
                data => console.log('Delete Success' ),
                err => console.log('Failure ', err)
            );

Advance

Get Node Reference

moiz-firebase library is a wrapper around standard Firebase library. In a situation you would like to work directly with Firebase Library Node references. You can use the code below

let nodeRef = db.getNodeRef('countries/au');

Once you have the node reference you could listen to different events. For example

nodeRef.on('child_added', (snapshot) => {
            let newItem = snapshot.val();
});

Using Email and Password Authentication

Sign in

The code below sign in the user using email address and password setup in Firebase

db.signInWithEmailAndPassword('emailAddress', 'password')
 .subscribe(
                user => console.log('Logged In ', user ),
                err => console.log('Failure ', err)
            );

Sign out

db.signOut()
 .subscribe(
                data => console.log('Signed Out '),
                err => console.log('Failure ', err)
            );

License

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago