How to use the database function from firebase-admin

Find comprehensive JavaScript firebase-admin.database code examples handpicked from public code repositorys.

firebase-admin.database is a module that provides access to the Firebase Realtime Database service for server-side Node.js applications.

150
151
152
153
154
155
156
157
158
159
  This includes chat data in the Firestore and Realtime database.
*/
exports.deleteChat = functions.runWith({ timeoutSeconds: 540, memory: '2GB' }).database.ref('/presence/{topic}/chats/{chatID}/{uid}/active').onUpdate(async (change, context) => {
  //function fires when a users active value has changed
  if (change.after.val() == false) {//user has gone offline
    const database = admin.database();
    const chatID = context.params.chatID;
    const usersID = context.params.uid;
    const topic = context.params.topic
    let opponentActive; //set to false if there is no opponent
fork icon1
star icon2
watch icon1

73
74
75
76
77
78
79
80
81
82
83
84


admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://ring-relay-default-rtdb.europe-west1.firebasedatabase.app/"
});
const db = admin.database();


function handler(req, res) {
    try {
        if (req.body != undefined) {
fork icon0
star icon0
watch icon1

+ 8 other calls in file

How does firebase-admin.database work?

The firebase-admin.database module in Firebase Admin SDK for Node.js provides an interface for interacting with the Realtime Database service in Firebase, allowing users to read and write data to a Firebase database from a server-side environment using a secure authentication method. The module allows users to create, delete, modify and query data in the database, and provides features such as transactions, real-time updates and server-side event listeners.

65
66
67
68
69
70
71
72
73
74
75
76
    measurementId: process.env.FIREBASE_MEASUREMENT_ID
};
firebase.initializeApp(firebaseConfig);


//var database = firebase.database();
var database = admin.database();
var logref = database.ref('log');
var errorlogref = database.ref('log/error');


var challengetimeref = database.ref('challenge/times');
fork icon0
star icon0
watch icon3

+ 4 other calls in file

39
40
41
42
43
44
45
46
47
48
});
const retrieveHatim = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
    const token = req.headers['authorization'].split(' ')[1];
    yield admin.auth().verifyIdToken(token).then((response) => __awaiter(void 0, void 0, void 0, function* () {
        // Get a reference to the desired node in the database
        const nodeRef = admin.database().ref('Hatim');
        // Read the data at the node once
        nodeRef.once('value', (snapshot) => {
            if (snapshot.exists()) {
                // access the data from the snapshot if it wxists
fork icon0
star icon0
watch icon1

+ 9 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const admin = require("firebase-admin");
const serviceAccount = require("./path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://.firebaseio.com",
});

const db = admin.database();
const ref = db.ref("path/to/data");

ref.once("value", (snapshot) => {
  console.log(snapshot.val());
});

This example initializes the Firebase Admin SDK with a service account key, sets the Realtime Database URL, gets a database reference to a specific path, and reads the data at that path using the once() method. The snapshot.val() method returns the data as a JavaScript object.