lakpahana/firebase_realtime_database

0.4.7
Module Overview

The FirebaseDatabaseClient module provides a Ballerina client to interact with Firebase Realtime Database. It allows you to perform various operations such as reading, writing, updating, and deleting data in your Firebase Realtime Database instance. This module handles the authentication process using a service account and ensures the validity of the access token.

Features

  • Initialize Firebase client with service account credentials and Firebase configuration.
  • Generate JWT and access token for authenticating Firebase requests.
  • Ensure access token validity and regenerate it if expired.
  • Perform CRUD operations on Firebase Realtime Database:
    • getData to read data.
    • postData to create new data.
    • putData to write or replace data.
    • patchData to update specific data fields.
    • deleteData to delete data.

Usage

Importing the Module

Copy
import lakpahana/firebase_realtime_database;

Initializing the Firebase Client

You need to provide the Firebase configuration as a JSON object and the path to your service account JSON file.

Copy
public function main() returns error? {
    json firebaseConfigJson = {
        apiKey: "<API_KEY>",
        authDomain: "<AUTH_DOMAIN>",
        databaseURL: "<DATABASE_URL>",
        projectId: "<PROJECT_ID>",
        storageBucket: "<STORAGE_BUCKET>",
        messagingSenderId: "<MESSAGING_SENDER_ID>",
        appId: "<APP_ID>",
        measurementId: "<MEASUREMENT_ID>"
    };

    FirebaseDatabaseClient firebaseClient = new FirebaseDatabaseClient();
    error? initError = firebaseClient.init(firebaseConfigJson, "./path/to/serviceAccount.json");
    if (initError != null) {
        log:printError("Failed to initialize Firebase client:", initError);
        return initError;
    }

    // Perform operations
}

Performing CRUD Operations

  • Get Data:
Copy
json|error data = firebaseClient.getData("path/to/data");
if (data is error) {
    log:printError("Failed to get data:", data);
} else {
    log:printInfo("Data retrieved: " + data.toString());
}
  • Post Data:
Copy
json newData = { "name": "John Doe", "age": 30 };
error? postError = firebaseClient.postData("path/to/data", newData);
if (postError != null) {
    log:printError("Failed to post data:", postError);
}
  • Put Data:
Copy
json updatedData = { "name": "Jane Doe", "age": 25 };
error? putError = firebaseClient.putData("path/to/data", updatedData);
if (putError != null) {
    log:printError("Failed to put data:", putError);
}
  • Patch Data:
Copy
json partialUpdate = { "age": 26 };
error? patchError = firebaseClient.patchData("path/to/data", partialUpdate);
if (patchError != null) {
    log:printError("Failed to patch data:", patchError);
}
  • Delete Data:
Copy
error? deleteError = firebaseClient.deleteData("path/to/data");
if (deleteError != null) {
    log:printError("Failed to delete data:", deleteError);
}

Import

import lakpahana/firebase_realtime_database;Copy

Metadata

Released date: 11 months ago

Version: 0.4.7


Compatibility

Platform: any

Ballerina version: 2201.9.0

GraalVM compatible: Yes


Pull count

Total: 9

Current verison: 0


Weekly downloads


Other versions