Firebase Realtime DB for JS developers

blog_2

Real-time databases are now part of progressive apps.They are no 3rd type to sql/NoSQL dbs.Below are some insights to them.

 

  • The word “real-time” data relates to data that syncs as soon as it is entered from the client to the database and from database to other receivers of the data as long as the receivers have subscribed to it.
  • “RealTime Database” is a solution that stores data in the cloud and provides an easy way to sync data/state among various devices

 

Firebase allows real-time synchronization and updating to active clients.

Using the Firebase SDK for Android, iOS, web or any other supported platform, your client will open a WebSocket channel between you and the Firebase cloud data store. Any updates in the cloud will be immediately propagated to all clients.

Firebase db is goto db for chat applications,note based apps etc.

Firebase provides a NoSQL schema where data is stored like a JSON tree(which is why the title). Unlike most NoSQL databases, firebase db as a tool has a look and feel of JSON Beautifier.

Firebase queries are  limited. Below are some common queries in order of complexity of operations in node/javascript/typescript.

  1. Initializing the database

admin = require(‘firebase-admin’);

  admin.initializeApp({

      credential: admin.credential.cert(serviceAccount),

      databaseURL: “<database URL in project settings>”

  });

  1. Creating a reference of a particular collection ,
    If you look at at above example, “users” is a root collection

let usersRef = admin.database().ref(‘/users);

This creates a reference to the users collection. It gives you a reference of the database / table so that you can query that,just like an ORM.

  1. Querying :-
    Those alphanumeric keys created by ref.push() are known as “push keys”. Push keys sort alphanumerically by time.

    The first query method that you need, always, is an orderby.
  • orderByChild(‘some-child-name’): Orders alphanumerically by any child key
  • orderByKey(): Orders by the key
  • orderByValue(): Only relevant if your nodes don’t have children.
  • orderByPriority(): Firebase still supports it, but orderByChild() has made it irrelevant.

    Next, you need to decide how many records you want and which direction you want to order.
  • limitToFirst(N): Starts with the oldest record and reads toward newer records
  • limitToLast(N): Returns N results from the newest records on the list, but returns them in ascending order, because Firebase only sorts in ascending order. So if I have records 1…10 and I run a limitToLast(3), I’d get records 8, 9 and 10 in that order.

Finally, decide which key you’d like to start or end at.

  • startAt(<someKey>): The key at which to start reading
  • endAt(<someKey>): The key at which to stop reading

    Eg :- usersRef.orderByKey().limitToFirst(10) + 1).startAt(‘9nvT28ZoT9eZ45nGGA8jeXRSrjC3’).once(value).then()

    4. End the query using .on or .once methods. This are integrated with promises and return fulfilled/rejected promises.

    CAUTION :- If you are writing these queries at server , use .once(). .on() method is an observable and watches any changes to database reference. This should be used only when you want your app to watch for changes and strictly at Client. This can affect your server performance.
  1. Below example shows how to club filtering with querying :-

    usersRef.orderByChild(‘meta/phone’).equalTo(“xyz”);
    Query reference is available in :- https://www.firebase.com/docs/web/api/query/

Leave a Reply

Your email address will not be published. Required fields are marked *


nine + = 17

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>