npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

moiz-firebase

v1.0.6

Published

Simplified Data Access Layer for Firebase

Downloads

8

Readme

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.