mirror of
https://github.com/supabase/supabase.git
synced 2026-10-06 09:55:06 +03:00
123 lines
3.5 KiB
Plaintext
123 lines
3.5 KiB
Plaintext
---
|
|
id: initializing
|
|
title: 'Initializing'
|
|
slug: initializing
|
|
custom_edit_url: https://github.com/supabase/supabase/edit/master/web/spec/supabase.yml
|
|
---
|
|
|
|
### Initializing a new Client
|
|
|
|
<RefSubLayout.EducationRow>
|
|
<RefSubLayout.Details>
|
|
|
|
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-csharp` 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.
|
|
|
|
</RefSubLayout.Details>
|
|
|
|
<RefSubLayout.Examples>
|
|
|
|
<Tabs
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="csharp-init-standard">
|
|
<TabPanel id="csharp-init-standard" label="Standard">
|
|
|
|
```csharp
|
|
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();
|
|
```
|
|
|
|
</TabPanel>
|
|
|
|
<TabPanel id="csharp-init-maui" label="Dependency Injection (Maui-like)">
|
|
|
|
```csharp
|
|
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()
|
|
};
|
|
|
|
// Note the creation as a singleton.
|
|
builder.Services.AddSingleton(provider => new Supabase.Client(url, key, options));
|
|
}
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="csharp-init-showing-models" label="With Models Example">
|
|
|
|
```csharp
|
|
// 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.
|
|
}
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
</RefSubLayout.Examples>
|
|
</RefSubLayout.EducationRow>
|