Before starting with Realtime Database, we have to initialize Firebase App and get Realtime Database.
const { firebaseHelper, realtimeHelper } = require('firebase-functions-helper');
const serviceAccount = require('./serviceAccountKey.json');
// Initialize Firebase App
const app = firebaseHelper.initializeApp(serviceAccount, databaseURL);
// Get Realtime Database
const db = app.realtime;The result will be a Promise of an array or an object of data.
realtimeHelper
.getData(db, 'data-path')
.then(data => console.log(data);)You will have two options that help to replace entire data path or keep and replace the data. This function will return a Promise: {status: boolean, message: string}
const data = {
td: {
name: 'Toronto-Dominion Bank'
},
rbc: {
name: 'RBC Royal Bank'
}
}
// Remove everything and add new data
realtimeHelper
.saveData(db, 'data-path', data, true);
// Replace or add new data without remove the old data
realtimeHelper
.saveData(db, 'data-path', data, false);This function will have you to add extra information to an existing data. If the path in data is incorrect, it will create a new data. This function will return a Promise: {status: boolean, message: string}
const updatedData = {
'td/location': 'Toronto',
'rbc/location': 'Toronto'
}
// Add location info to td and rbc
realtimeHelper
.updateData(db, 'banks', updatedData);This function will delete all data under a path. It will return a Promise: {status: boolean, message: string}
realtimeHelper
.deleteData(db, 'data-path');