-
Notifications
You must be signed in to change notification settings - Fork 27
Firebase Database API
All the following firebase functions are accessible through a firebase instance.
var Database = require("Firebase/Database");## Push Adds a new child in the given path.
-
reference- Path where the new child will be added -
child- JSON to add
This method returns the key of the new record.
var user = {
name : "Joe Doe",
email: '[email protected]'
}
Database.push("users", user);This will insert new object to given path, it will have timestamp property set by Firebase server
-
reference- Path where the new child will be added -
child- JSON to add
Database.pushWithTimestamp(path, data);If changes are made to data, then calling save() will push those changes to the server. It's the equivalent of native setValue method.
-
reference- Path of the object -
object- JSON to set
var user = {
name : "Juan Perez",
email: '[email protected]'
}
Database.save("users/customId", user);Set a path to listen when a change occurs in the database. The events will be caught by onData
-
reference- Path to listen
Database.listen("users");Trigger a function with path and msg when there is any change in the specified path.
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 an object from the database on a specific path.
-
reference- Path where the object will be read out
String representing the object. It could be null if the object does not exist in the given route.
Database.read("users")
.then(function(result) {
console.log(result); // {"-L0hFqwnwiTrw2aE5bnV":{"name":"Joe Doe","email":"joedoe@example.com"} ... }
});Delete an object in the database on a specific path.
-
reference- Path of the object to be deleted
Database.delete("users/customId");Search in the database by using specific key.
-
path- Path of the object to be deleted -
key- The key to search in -
value- The value to search
Database.readByQueryEqualToValue("users","name","Luis Rodriguez")
.then(function (json) {
console.log(json);
}).catch(function (reason) {
console.log('Unable to read -> ' +reason);
});Say you want to get old messages for particular user you want to get messages before particular timestamp
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);
}
});