From 947218c1288cb36f3a8bf0c6e56eb8ce43cba584 Mon Sep 17 00:00:00 2001 From: Nathan Marzina <46549002+NJMarzina@users.noreply.github.com> Date: Wed, 2 Apr 2025 23:08:50 -0400 Subject: [PATCH] Update Client.cs The ExecuteBatchTransactionAsync method would allow developers to batch multiple database operations into a single transaction. This is particularly useful for: Performance optimization - Reducing network round trips when performing multiple operations Data integrity - Ensuring that a series of operations succeed or fail together as a unit Simplifying code - Replacing multiple sequential calls with a single call What the rest of the code does: The existing Supabase C# client shown in your code performs several key functions: Client Coordination - Acts as the main entry point for all Supabase services: Auth (user authentication) Realtime (websocket connections for real-time updates) Functions (serverless edge functions) Postgrest (database operations) Storage (file storage) Authentication Management - Handles user sessions, token refresh, and propagates auth state changes to other services. Header Management - Provides authentication headers to all child clients. Initialization - Sets up the necessary URLs and configurations for each service. Typed Data Access - Provides the From() method for strongly typed database operations. Remote Procedure Calls - Enables calling PostgreSQL functions through the Rpc methods. --- Supabase/Client.cs | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/Supabase/Client.cs b/Supabase/Client.cs index 3f6e6772..d0e93ddc 100644 --- a/Supabase/Client.cs +++ b/Supabase/Client.cs @@ -280,5 +280,82 @@ internal Dictionary GetAuthHeaders() return headers; } +///// + /// + /// Executes multiple database operations as a single transaction. + /// + /// A list of operations to execute in a transaction. + /// Optional configuration for the transaction. + /// A response containing the results of all operations. + public async Task ExecuteBatchTransactionAsync(List operations, BatchOptions? options = null) + { + options ??= new BatchOptions { Atomic = true }; + + // Prepare the batch request + var batch = new BatchRequest + { + Operations = operations, + Options = options + }; + + // Send the batch request to the Postgrest client + var response = await Postgrest.ExecuteBatchAsync(batch); + + // Process results and return + return response; + } + + /// + /// Represents a database operation to be included in a batch transaction. + /// + public class PostgrestOperation + { + /// + /// The method to execute (INSERT, UPDATE, DELETE, SELECT). + /// + public string Method { get; set; } = string.Empty; + + /// + /// The table name to operate on. + /// + public string Table { get; set; } = string.Empty; + + /// + /// The query parameters. + /// + public Dictionary? Parameters { get; set; } + + /// + /// The data to insert or update. + /// + public object? Payload { get; set; } + } + + /// + /// Configuration options for batch operations. + /// + public class BatchOptions + { + /// + /// When true, all operations are executed in a single transaction. + /// + public bool Atomic { get; set; } = true; + } + + /// + /// Response from a batch operation. + /// + public class BatchResponse + { + /// + /// Results from each operation in the batch. + /// + public List Results { get; set; } = new List(); + + /// + /// Status of each operation. + /// + public List Status { get; set; } = new List(); + } } }