Firestore.ORM 1.1.7

dotnet add package Firestore.ORM --version 1.1.7
                    
NuGet\Install-Package Firestore.ORM -Version 1.1.7
                    
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="Firestore.ORM" Version="1.1.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Firestore.ORM" Version="1.1.7" />
                    
Directory.Packages.props
<PackageReference Include="Firestore.ORM" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Firestore.ORM --version 1.1.7
                    
#r "nuget: Firestore.ORM, 1.1.7"
                    
#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.
#:package Firestore.ORM@1.1.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Firestore.ORM&version=1.1.7
                    
Install as a Cake Addin
#tool nuget:?package=Firestore.ORM&version=1.1.7
                    
Install as a Cake Tool

Firestore.ORM 🔥

Firestore.ORM is a lightweight object-relational mapping (ORM) product for Firebase.Firestore : It provides an additional layer of abstraction on top of the existing Firestore SDK

One of the main advantages of this library is to provide a solid model structure base in order to ensure the integrity of the data present in Firestore. It also provides utility methods encapsulating the SDK functions in order to simplify writing and reading, serialization and synchronization. It sits on top of the official Firebase SDK, it's not intended to replace it. The ORM exposes the parts of the SDK it uses in a way that it remains usable within the project.

NuGet latest version

Model

  • All class from the model must derive from the type Firestore.ORM.FirestoreDocument. This abstract class is wrapping data provided by the SDK (such as Google.Cloud.Firestore.DocumentReference) and additional data provided by the ORM, such as a list of corruption errors if the data obtained from Firebase does not match the structure expected.

  public class User : FirestoreDocument
  {
    public User(DocumentReference reference) : base(reference)
    {

    }

    [Field("email")]
    public string Email
    {
        get;
        set;
    }

    [Field("firstname")]
    public string Firstname
    {
        get;
        set;
    }

    [Field("phone", FieldNullability.Nullable)]
    public string Phone
    {
        get;
        set;
    }


    [Field("role")]
    public int Role
    {
        get;
        set;
    } = 1;

    [Field("claims")]
    public List<DocumentReference> Claims
    {
      get;
      set;
    }


}

Connecting to Firebase

The initialization function establishes the connection with Firebase and introspects the code to discover the model.

Initialize(string projectId, FirestoreClient client, Assembly assembly,
            MappingBehavior mappingBehavior = MappingBehavior.Strict)

The assembly parameter indicates the assembly where the model is located. mappingBehavior corresponds to the level of tolerance that the ORM adopts when remote data structure is not valid compared to the model. In the case of strict tolerance, the ORM throws if any data is corrupted.


var client = new FirestoreClientBuilder
{
    CredentialsPath = "credentials.json"
}.Build();

FirestoreManager.Instance.Initialize("my-project", client, Assembly.GetExecutingAssembly());


Manipulating data

Basic fetch

IEnumerable<User> userCollection = await FirestoreManager.Instance.Collection("users").GetAsync<User>();

Listeners

CollectionReference collection = FirestoreManager.Instance.Collection("users");

SnapshotListener<User> userListener = new SnapshotListener<User>();
userListener.OnItemChange += ...
userListener.OnItemsUpdated += ...

/*
  First fetch the collection and start to listen to the changes
*/
await userListener.FetchAndListen();

/*
  Provide a snapshot of the collection,
  internal collection inside the listener is still in real time
*/

List<User> users = userListener.GetItems().ToList();

Writing

User? user = users.Find(x=> x.Firstname == "John");

user.Firstname = "Peter";

user.Update();

if (user.Phone.Contains("+33"))
{
    user.Delete();
}

User newUser = new User(collection.Document());

newUser.Name = "Maria";

newUser.Insert();

Data sanitize, incident report

Incident management allows you to verify that the data present in Firebase respects the structure expected by the model. It ensures the structuring of data. This feature is only available with mappingBehavior : 'MappingBehavior.Souple'.


IncidentManager.OnIncident += OnIncident;

static void OnIncident(Incident incident)
{
      switch (incident)
      {
          case MissingFieldIncident missingFieldIncident:
              // Solve missing field on document
              break;
          case InvalidFieldTypeIncident invalidFieldTypeIncident:
               // Solve wrong field type on document
              break;
      }
}


Contributors

Name
Marius Lumbroso Author (https://github.com/Skinz3)
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.1.7 322 10/10/2024
1.1.6 389 5/21/2024
1.1.5 183 5/20/2024
1.1.4 181 4/10/2024
1.1.3 281 4/10/2024
1.1.2 192 4/4/2024
1.1.1 175 4/4/2024
1.1.0 165 4/4/2024
1.0.8 265 3/27/2024
1.0.7 177 3/26/2024
1.0.6 169 3/26/2024
1.0.5 175 3/26/2024
1.0.4 182 3/22/2024
1.0.3 193 3/22/2024
1.0.2 186 3/22/2024
1.0.1 177 3/22/2024
1.0.0 189 3/22/2024