FirebaseExtensions 1.2.0

dotnet add package FirebaseExtensions --version 1.2.0
NuGet\Install-Package FirebaseExtensions -Version 1.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FirebaseExtensions" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FirebaseExtensions --version 1.2.0
#r "nuget: FirebaseExtensions, 1.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FirebaseExtensions as a Cake Addin
#addin nuget:?package=FirebaseExtensions&version=1.2.0

// Install FirebaseExtensions as a Cake Tool
#tool nuget:?package=FirebaseExtensions&version=1.2.0

Firebase/Firestore/Exstension

A library has been created to simplify data retrieval and uploading in Firestore.

FirebaseExtension Code Description

Overview

The FirebaseExtension class provides extension methods for DocumentReference to perform Firestore operations. These methods facilitate setting and retrieving document data asynchronously, with options for customization and error handling.

Methods

SetValueAsync Methods

  1. SetValueAsync<T>(this DocumentReference documentReference, T value)

    • Sets a document's data in Firestore.
    • Parameters:
      • documentReference: The document reference.
      • value: The value to set in the document.
    • Returns: A task representing the result of the write operation.
  2. SetValueAsync<T>(this DocumentReference documentReference, T value, SetOptions? setOptions)

    • Sets a document's data in Firestore with specified set options.
    • Parameters:
      • documentReference: The document reference.
      • value: The value to set in the document.
      • setOptions: The options for setting the value.
    • Returns: A task representing the result of the write operation.
  3. SetValueAsync<T>(this DocumentReference documentReference, T value, CancellationToken cancellationToken)

    • Sets a document's data in Firestore with a cancellation token.
    • Parameters:
      • documentReference: The document reference.
      • value: The value to set in the document.
      • cancellationToken: A token to monitor for cancellation requests.
    • Returns: A task representing the result of the write operation.
  4. SetValueAsync<T>(this DocumentReference documentReference, T value, SetOptions? setOptions, CancellationToken cancellationToken)

    • Sets a document's data in Firestore with specified set options and a cancellation token.
    • Parameters:
      • documentReference: The document reference.
      • value: The value to set in the document.
      • setOptions: The options for setting the value.
      • cancellationToken: A token to monitor for cancellation requests.
    • Returns: A task representing the result of the write operation.

GetValueAsync Methods

  1. GetValueAsync<T>(this DocumentReference documentReference) where T : new()

    • Retrieves a document's data from Firestore.
    • Parameters:
      • documentReference: The document reference.
    • Returns: A task representing the result of the read operation.
  2. GetValueAsync<T>(this DocumentReference documentReference, CancellationToken cancellationToken) where T : new()

    • Retrieves a document's data from Firestore with a cancellation token.
    • Parameters:
      • documentReference: The document reference.
      • cancellationToken: A token to monitor for cancellation requests.
    • Returns: A task representing the result of the read operation.

Error Handling

  • The methods return a Result object that indicates the success or failure of the operation.
  • If the value or documentReference parameters are null, the SetValueAsync methods return a failed Result with an appropriate error message.
  • Exceptions encountered during the operations are caught and result in a failed Result with the exception message.

Firebase Initialization Example

Overview

This document provides an example of how to initialize the Firebase Firestore database using the Firebase.Init method with a provided JSON credentials file. The example demonstrates the process of combining the base directory path with the JSON key file to create the necessary initialization.

Example Usage

Initialization Code

// Combine the base directory with the key.json file to get the full path
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "key.json");

// Initialize Firebase using the path to the credentials file
Result r = Firebase.Init(path);

// Check the result of the initialization
if (r.IsSuccess)
{
    Console.WriteLine("Firebase initialized successfully.");
}
else
{
    Console.WriteLine($"Error initializing Firebase: {r.Error}");
}

Usage Example

var documentReference = firestoreDb.Collection("collectionName").Document("documentName");

// Setting a value
var result = await documentReference.SetValueAsync(new { Name = "John Doe", Age = 30 });
if (result.IsSuccess)
{
    Console.WriteLine("Document updated successfully.");
}
else
{
    Console.WriteLine($"Error: {result.Error}");
}

// Getting a value
var getResult = await documentReference.GetValueAsync<MyClass>();
if (getResult.IsSuccess)
{
    var myObject = getResult.Value;
    Console.WriteLine($"Document retrieved: {myObject.Name}, {myObject.Age}");
}
else
{
    Console.WriteLine($"Error: {getResult.Error}");
}

Library version 1.2.0

In the new library version 1.2.0, the following functions and constants have been added:

  • Constants for identifying various Firebase configuration parameters, including the account type, project ID, private key ID and key, client email and ID, authentication and token URIs, X509 certificate URLs for the authentication provider and client, as well as the universe domain.

  • Methods for retrieving the values of these parameters from the Firebase configuration JSON file, including GetType(), GetPrivateKeyId(), GetPrivateKey(), GetClientEmail(), GetClientId(), GetAuthUri(), GetTokenUri(), GetAuthProviderX509CertUrl(), GetClientX509CertUrl(), and GetUniverseDomain().

These changes facilitate working with Firebase configuration, allowing developers to easily extract the necessary parameters for initializing and working with Firebase Firestore.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 55 6/14/2024
1.0.1 58 6/6/2024
1.0.0 59 5/31/2024

Added new methods and changed existing ones.