Initializing a new client is pretty straightforward. Find your project url and public key from the admin panel and pass it into your client initialization function.
Supabase
is heavily dependent on Models deriving from BaseModel
. To interact with the API, one must have the associated model (see example) specified.
Leverage Table
, PrimaryKey
, and Column
attributes to specify names of classes/properties that are different from their C# Versions.
var url = Environment.GetEnvironmentVariable("SUPABASE_URL");
var key = Environment.GetEnvironmentVariable("SUPABASE_KEY");
var options = new Supabase.SupabaseOptions
\{
AutoConnectRealtime = true
\};
var supabase = new Supabase.Client(url, key, options);
await supabase.InitializeAsync();
public static MauiApp CreateMauiApp()
\{
// ...
var builder = MauiApp.CreateBuilder();
var url = Environment.GetEnvironmentVariable("SUPABASE_URL");
var key = Environment.GetEnvironmentVariable("SUPABASE_KEY");
var options = new SupabaseOptions
\{
AutoRefreshToken = true,
AutoConnectRealtime = true,
// SessionHandler = new SupabaseSessionHandler() <-- This must be implemented by the developer
\};
// Note the creation as a singleton.
builder.Services.AddSingleton(provider => new Supabase.Client(url, key, options));
\}
// Given the following Model representing the Supabase Database (Message.cs)
[Table("messages")]
public class Message : BaseModel
\{
[PrimaryKey("id")]
public int Id \{ get; set; \}
[Column("username")]
public string UserName \{ get; set; \}
[Column("channel_id")]
public int ChannelId \{ get; set; \}
public override bool Equals(object obj)
\{
return obj is Message message &&
Id == message.Id;
\}
public override int GetHashCode()
\{
return HashCode.Combine(Id);
\}
\}
void Initialize()
\{
// Get All Messages
var response = await client.Table<Message>().Get();
List<Message> models = response.Models;
// Insert
var newMessage = new Message \{ UserName = "acupofjose", ChannelId = 1 \};
await client.Table<Message>().Insert();
// Update
var model = response.Models.First();
model.UserName = "elrhomariyounes";
await model.Update();
// Delete
await response.Models.Last().Delete();
// etc.
\}