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

nativescript-plugin-firebase-common

v0.10.11

Published

Fire. Base. Firebase!

Downloads

64

Readme

NativeScript Firebase Common plugin

How this plugin differs from nativescript-plugin-firebase:

This plugin aims to provide a common interface for firebase instances that can be easily used across Android, iOS, and the Web. This means that the API surface should be limited as much as possible and should try to mimic the detail of the native APIs as much as possible.

As it currently stands, the nativescript-plugin-firebase project does not offer an API that is similar to any of the Firebase SDK APIs and offers high-level functionality at the expense of composability.

For this project right now, the common API is quite limited, but can easily be expanded to encapsulate additional features.

Use when

  • you need to store JSON data in the cloud,
  • you want to sync that data to other devices and platforms,
  • you want to optionally protect that data by having users log in,
  • you want to update clients at the moment the data changes (think chat and multiplayer games).

Prerequisites

NativeScript 1.3.0 (tns --version) is required for smooth installation, so please upgrade if you need to.

Head on over to firebase.com and sign up for a free account. Your first 'Firebase' will be automatically created and made available via a URL like https://resplendent-fire-4211.firebaseio.com/.

Installation

From the command prompt go to your app's root folder and execute:

tns plugin add nativescript-plugin-firebase-common

Usage

createNew

  var Firebase = require("nativescript-plugin-firebase-common").Firebase;

  var firebase = Firebase.createNew({
    url: 'https://resplendent-fire-4211.firebaseio.com'
    // persist: false // turn off offline disk persistence
  });

All further examples assume firebase has been required.

child

Gets a firebase reference for the location at the specified relative path.

    var categories = firebase.child("categories");

set

Replaces the data at the current firebase location. Analogous to set() in the Firebase JavaScript SDK.


  // to store a JSON object
  var promise = firebase.set(
      {'foo':'bar'}
  );

  // to store an array of JSON objects
  var otherPromise = firebase.set(
      [
        {name: 'Telerik', country: 'Bulgaria'},
        {name: 'Google', country: 'USA'}
      ]
  );

push

This function will store the given object as a new child at the current location:

  var promise = firebase.push(
      {
        'first': 'Eddy',
        'last': 'Verbruggen',
        'birthYear': 1977,
        'isMale': true,
        'address': {
          'street': 'foostreet',
          'number': 123
        }
      }
  );

on

To listen for changes in your database you can pass in a listener callback function. The possible event types are:

  • value, Observes the value for the firebase ref.
  • child_added, Observes when a new child is added to the firebase ref.
  • child_changed, Observes when a child was modified in the firebase ref.
  • child_moved, Observes when a child was moved in the firebase ref.
  • child_removed, Observes when a child was removed from the firebase ref.

The plugin will take care of serializing native data structures to JSON data.

  var onChildAdded = function(result) {
    console.log("Key: " + result.key());
    console.log("Value: " + JSON.stringify(result.val()));
  };

  var users = firebase.child("users");

  // listen to new "children" in the /users path
  var cancellationToken = users.on("child_added", onChildAdded);
  
  // to disable the event listener:
  users.off("child_added", cancellationToken);

This method and the related off() method have been designed to be analogous to the Firebase JavaScript SDK versions.

remove

You can remove the entire database content by omitting the param, but if you only want to wipe everything at '/users', do this:

  var promise = firebase.remove("/users");

login

v 1.1.0 of this plugin adds the capability to log your users in. Either anonymously or by email and password. You need to add support for those features in your Firebase instance at the 'Login & Auth' tab.

You can expect more login mechanisms to be added in the future.

Anonymous login

  firebase.login({
    // note that you need to enable anonymous login in your firebase instance
    type: firebase.loginType.ANONYMOUS
  }).then(
      function (result) {
        // the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token
        JSON.stringify(result);
      },
      function (errorMessage) {
        console.log(errorMessage);
      }
  )

Password login

  firebase.login({
      // note that you need to enable email-password login in your firebase instance
    type: firebase.loginType.PASSWORD,
    email: '[email protected]',
    password: 'theirpassword'
  }).then(
      function (result) {
        // the result object has these properties: uid, provider, expiresAtUnixEpochSeconds, profileImageURL, token
        JSON.stringify(result);
      },
      function (errorMessage) {
        console.log(errorMessage);
      }
  )

Creating a Password account

  firebase.createUser({
    email: '[email protected]',
    password: 'firebase'
  }).then(
      function (uid) {
        dialogs.alert({
          title: "User created",
          message: "uid: " + uid,
          okButtonText: "Nice!"
        })
      },
      function (errorMessage) {
        dialogs.alert({
          title: "No user created",
          message: errorMessage,
          okButtonText: "OK, got it"
        })
      }
  )

logout

Shouldn't be more complicated than:

  firebase.logout();

Credits

The starting point for this plugin was this great Gist by John Bristowe.