Skip to content

Firebase Database API

Luis Rodriguez edited this page Aug 2, 2018 · 1 revision

Initialization

All the following firebase functions are accessible through a firebase instance.

var Database = require("Firebase/Database");

Functions


## Push Adds a new child in the given path.

Parameters

  • reference - Path where the new child will be added
  • child - JSON to add

Return

This method returns the key of the new record.

Example

var user = {
    name : "Joe Doe",
    email: '[email protected]'
}
Database.push("users", user);

Push with timestamp

This will insert new object to given path, it will have timestamp property set by Firebase server

Parameters

  • reference - Path where the new child will be added
  • child - JSON to add

Example

Database.pushWithTimestamp(path, data);

Save

If changes are made to data, then calling save() will push those changes to the server. It's the equivalent of native setValue method.

Parameters

  • reference - Path of the object
  • object - JSON to set

Example

var user = {
    name : "Juan Perez",
    email: '[email protected]'
}
Database.save("users/customId", user);

Listen

Set a path to listen when a change occurs in the database. The events will be caught by onData

Parameters

  • reference - Path to listen

Example

Database.listen("users");

OnData

Trigger a function with path and msg when there is any change in the specified path.

Example

Database.listen("users");
Database.on("data", function(path, msg) {
    console.log(path); // users
    console.log(msg); // {"-L0hFqwnwiTrw2aE5bnV":{"name":"Joe Doe","email":"joedoe@example.com"} ... }
});

Read

Read an object from the database on a specific path.

Parameters

  • reference - Path where the object will be read out

Return

String representing the object. It could be null if the object does not exist in the given route.

Example

Database.read("users")
    .then(function(result) {
        console.log(result); // {"-L0hFqwnwiTrw2aE5bnV":{"name":"Joe Doe","email":"joedoe@example.com"} ... }
    });

Delete

Delete an object in the database on a specific path.

Parameters

  • reference - Path of the object to be deleted

Example

Database.delete("users/customId");

Query

Search in the database by using specific key.

Parameters

  • path - Path of the object to be deleted
  • key - The key to search in
  • value - The value to search

Example

Database.readByQueryEqualToValue("users","name","Luis Rodriguez")
    .then(function (json) {
        console.log(json);
    }).catch(function (reason) {
        console.log('Unable to read -> ' +reason);
    });

Query subset of records

Say you want to get old messages for particular user you want to get messages before particular timestamp

Example

var chatMessagesPath = 'users/pavel/messages';
var oldestMessageTimestamp = 1517459417719;
var count = 20;

// We're going to query 20 messages before `oldestMessageTimestamp`

Database.readByQueryEndingAtValue(
    chatMessagesPath,
    'timestamp', // field we're comaring with
    oldestMessageTimestamp, // oldest message timestamp on our device
    count // how many records to get
);

// You need to do this only once in your code
// This event handler will be called for every Database.readByQueryEndingAtValue method
Database.on('readByQueryEndingAtValue', function (eventPath, newMessages) {
    // newMessages here is a JSON string
    if (eventPath === chatMessagesPath) {
        console.log(chatMesssagesPath);
        console.log(newMessages);
    }
});

Clone this wiki locally