mirror of
https://github.com/supabase/supabase.git
synced 2026-10-09 19:35:06 +03:00
Provide some clarity on how models are used with the C# client - addressing a discord questions
This commit is contained in:
2 files changed
+91
-1
No files matched your search
@@ -13,6 +13,10 @@ custom_edit_url: https://github.com/supabase/supabase/edit/master/web/spec/supab
|
||||
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>
|
||||
@@ -29,7 +33,7 @@ admin panel and pass it into your client initialization function.
|
||||
|
||||
var options = new Supabase.SupabaseOptions
|
||||
{
|
||||
AutoConnectRealtime = true
|
||||
AutoConnectRealtime = true
|
||||
};
|
||||
|
||||
var supabase = new Supabase.Client(url, key, options);
|
||||
@@ -61,6 +65,57 @@ admin panel and pass it into your client initialization function.
|
||||
```
|
||||
|
||||
</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>
|
||||
|
||||
@@ -285,6 +285,7 @@ functions:
|
||||
notes: |
|
||||
- **LINQ expressions do not currently support parsing embedded resource columns. For these cases, `string` will need to be used.**
|
||||
- **When using string Column Names to select, they must match names in database, not names specified on model properties.**
|
||||
- Additional information on modeling + querying Joins and Inner Joins can be found [in the `postgrest-csharp README`](https://github.com/supabase-community/postgrest-csharp/blob/master/README.md#foreign-keys-join-tables-and-relationships)
|
||||
- By default, Supabase projects will return a maximum of 1,000 rows. This setting can be changed in Project API Settings. It's recommended that you keep it low to limit the payload size of accidental or malicious requests. You can use `range()` queries to paginate through your data.
|
||||
- `From()` can be combined with [Modifiers](/docs/reference/csharp/using-modifiers)
|
||||
- `From()` can be combined with [Filters](/docs/reference/csharp/using-filters)
|
||||
@@ -295,6 +296,23 @@ functions:
|
||||
isSpotlight: true
|
||||
code: |
|
||||
```c#
|
||||
// Given the following Model (City.cs)
|
||||
[Table("cities")]
|
||||
class City : BaseModel
|
||||
{
|
||||
[PrimaryKey("id")]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Column("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Column("country_id")]
|
||||
public int CountryId { get; set; }
|
||||
|
||||
//... etc.
|
||||
}
|
||||
|
||||
// A result can be fetched like so.
|
||||
var result = await supabase.From<City>().Get();
|
||||
var cities = result.Models
|
||||
```
|
||||
@@ -303,6 +321,23 @@ functions:
|
||||
description: You can select specific fields from your tables.
|
||||
code: |
|
||||
```c#
|
||||
// Given the following Model (Movie.cs)
|
||||
[Table("movies")]
|
||||
class Movie : BaseModel
|
||||
{
|
||||
[PrimaryKey("id")]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Column("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Column("created_at")]
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
//... etc.
|
||||
}
|
||||
|
||||
// A result can be fetched like so.
|
||||
var result = await supabase
|
||||
.From<Movie>()
|
||||
.Select(x => new object[] {x.Name, x.CreatedAt})
|
||||
|
||||
Reference in new issue
Block a user