GithubHelp home page GithubHelp logo

firebase-sync-node's Introduction

firebase-sync-node

This node module works with sync-node and allows you to instance your Firebase database as an associative array (synchronously).

$ npm install firebase-sync-node

Information

Quick setup:

var path      = "some/path/to/reference"                // path to reference
var database  = admin.database()                        // firebase database instance

var fireSync = new FirebaseSyncNode(database, path)     // firebase-sync-node
var queue = fireSync.getQueue()                         // database's queue 
fireSync.syncFromDatabase()                             // initial synchronization to create the reference

Once you have set it up, your database reference will be stored on ref var:

// print database reference
queue.pushJob(function() {
    console.log(JSON.stringify(fireSync.ref))
})

Of course you can modify that reference:

queue.pushJob(function() {
    fireSync.ref["some_random_prop"] = "2b or not 2b"
})

Call syncToDatabase method every time you want to sync data on Firebase.

queue.pushJob(function() {
    fireSync.ref.name = "Draco"
})

// sync fireSync.ref variable
fireSync.syncToDatabase()     

Maybe you'll need some fresh data from database, so call the initial method syncFromDatabase to stay up to date from Firebase db:

queue.pushJob(function() {
    fireSync.ref.name = "Draco"
})

fireSync.syncFromDatabase()

queue.pushJob(function() {
    console.log("name: " + fireSync.ref.name)   // name: John
})

Example

var admin             = require("firebase-admin")
var FirebaseSyncNode  = require('firebase-sync-node')

// firebase (SDK 3.x)
var serviceAccount = require("./some_firebase_credentials.json");
var credentials = {
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://some_random_db.firebaseio.com/",
    databaseAuthVariableOverride: {
        uid: "lalilulelo"
    }
}

admin.initializeApp(credentials)

// let's instance a puppy from database

var path        = "puppies/draco"                         // path to reference
var database    = admin.database()                        // firebase database instance

var fireSync    = new FirebaseSyncNode(database, path)     // firebase-sync-node
var queue       = fireSync.getQueue()                      // database's queue

fireSync.syncFromDatabase()                                // initial synchronization to create the puppy reference

// add some properties to database
queue.pushJob(function() {
    fireSync.ref.color = "chocolate"
})

// let's save new data
fireSync.syncToDatabase()                               // draco is chocolate

queue.pushJob(function() {
    fireSync.ref.color = "white"
})

// let's save data again
fireSync.syncToDatabase()                               // now draco is white!

For more documentation about queue check out the original node module by @metalshan: sync-node

(mmmm choooocolate ๐Ÿคค)

firebase-sync-node's People

Contributors

efraespada avatar

Watchers

 avatar  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.