Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Client.cs #189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

NJMarzina
Copy link

What kind of change does this PR introduce?

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 is the current behavior?

Please link any relevant issues here.

What is the new behavior?

Feel free to include screenshots if it includes visual changes.

Additional context

Add any other context or screenshots.

Code example:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Supabase;
using Supabase.Postgrest.Responses;

public class InventoryManager
{
private readonly Client _supabaseClient;

public InventoryManager(string supabaseUrl, string supabaseKey)
{
    _supabaseClient = new Client(supabaseUrl, supabaseKey);
}

public async Task<bool> ProcessOrderAsync(int orderId, List<OrderItem> items, int customerId)
{
    try
    {
        // Create a list of operations to perform in a single transaction
        var operations = new List<PostgrestOperation>();
        
        // Operation 1: Update order status
        operations.Add(new PostgrestOperation
        {
            Method = "UPDATE",
            Table = "orders",
            Parameters = new Dictionary<string, object> { { "id", $"eq.{orderId}" } },
            Payload = new { status = "processing", updated_at = DateTime.UtcNow }
        });
        
        // Operation 2: Update inventory quantities for each item
        foreach (var item in items)
        {
            operations.Add(new PostgrestOperation
            {
                Method = "UPDATE",
                Table = "inventory",
                Parameters = new Dictionary<string, object> { { "product_id", $"eq.{item.ProductId}" } },
                Payload = new { quantity = item.NewQuantity, last_updated = DateTime.UtcNow }
            });
        }
        
        // Operation 3: Insert entry in the order_history table
        operations.Add(new PostgrestOperation
        {
            Method = "INSERT",
            Table = "order_history",
            Payload = new 
            { 
                order_id = orderId,
                customer_id = customerId,
                action = "order_processing",
                timestamp = DateTime.UtcNow
            }
        });
        
        // Execute all operations in a single atomic transaction
        var response = await _supabaseClient.ExecuteBatchTransactionAsync(operations, new BatchOptions { Atomic = true });
        
        // Check if all operations were successful (status codes 200-299)
        bool allSuccessful = response.Status.TrueForAll(status => status >= 200 && status < 300);
        
        return allSuccessful;
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error processing order: {ex.Message}");
        return false;
    }
}

}

public class OrderItem
{
public int ProductId { get; set; }
public int Quantity { get; set; }
public int NewQuantity { get; set; } // The updated inventory quantity after deduction
}

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<TModel>() method for strongly typed database operations.

Remote Procedure Calls - Enables calling PostgreSQL functions through the Rpc methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant