postgrest-csharp 3.5.1

Suggested Alternatives

Supabase.Postgrest

Additional Details

Supabase is migrating its packages to match the Nuget naming conventions.

dotnet add package postgrest-csharp --version 3.5.1
NuGet\Install-Package postgrest-csharp -Version 3.5.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="postgrest-csharp" Version="3.5.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add postgrest-csharp --version 3.5.1
#r "nuget: postgrest-csharp, 3.5.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install postgrest-csharp as a Cake Addin
#addin nuget:?package=postgrest-csharp&version=3.5.1

// Install postgrest-csharp as a Cake Tool
#tool nuget:?package=postgrest-csharp&version=3.5.1

<p align="center"> <img width="300" src=".github/logo.png"/> </p>

<p align="center"> <img src="https://github.com/supabase/postgrest-csharp/workflows/Build%20And%20Test/badge.svg"/> <a href="https://www.nuget.org/packages/postgrest-csharp/"> <img src="https://img.shields.io/badge/dynamic/json?color=green&label=Nuget%20Release&query=data[0].version&url=https%3A%2F%2Fazuresearch-usnc.nuget.org%2Fquery%3Fq%3Dpackageid%3Apostgrest-csharp"/> </a> </p>


Now supporting (many) LINQ expressions!

await client.Table<Movie>()
            .Select(x => new object[] { x.Id, x.Name, x.Tags, x.ReleaseDate })
            .Where(x => x.Tags.Contains("Action") || x.Tags.Contains("Adventure"))
            .Order(x => x.ReleaseDate, Ordering.Descending)
            .Get();

await client.Table<Movie>()
            .Set(x => x.WatchedAt, DateTime.Now)
            .Where(x => x.Id == "11111-22222-33333-44444")
            // Or .Filter(x => x.Id, Operator.Equals, "11111-22222-33333-44444")
            .Update();


Documentation can be found here.

Postgrest-csharp is written primarily as a helper library for supabase/supabase-csharp, however, it should be easy enough to use outside of the supabase ecosystem.

The bulk of this library is a translation and c-sharp-ification of the supabase/postgrest-js library.

Getting Started

Postgrest-csharp is heavily dependent on Models deriving from BaseModel. To interact with the API, one must have the associated model specified.

To use this library on the Supabase Hosted service but separately from the supabase-csharp, you'll need to specify your url and public key like so:

var auth = new Supabase.Gotrue.Client(new ClientOptions<Session>
{
    Url = "https://PROJECT_ID.supabase.co/auth/v1",
    Headers = new Dictionary<string, string>
    {
        { "apikey", SUPABASE_PUBLIC_KEY },
        { "Authorization", $"Bearer {SUPABASE_USER_TOKEN}" }
    }
})

Leverage Table,PrimaryKey, and Column attributes to specify names of classes/properties that are different from their C# Versions.

[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);
    }
}

Utilizing the client is then just a matter of instantiating it and specifying the Model one is working with.

void Initialize()
{
    var client = new Client("http://localhost:3000");

    // 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();
}

Foreign Keys, Join Tables, and Relationships

The Postgrest server does introspection on relationships between tables and supports returning query data from tables with these included. Foreign key constrains are required for postgrest to detect these relationships.

This library implements the attribute, Reference to specify on a model when a relationship should be included in a query.

  • One-to-one Relationships: One-to-one relationships are detected if there’s an unique constraint on a foreign key.
  • One-to-many Relationships: The inverse one-to-many relationship between two tables is detected based on the foreign key reference.
  • Many-to-many Relationships: Many-to-many relationships are detected based on the join table. The join table must contain foreign keys to other two tables and they must be part of its composite key.

Given the following schema:

example schema

We can define the following models:

[Table("movie")]
public class Movie : BaseModel
{
    [PrimaryKey("id")]
    public int Id { get; set; }

    [Column("name")]
    public string Name { get; set; }

    [Reference(typeof(Person))]
    public List<Person> Persons { get; set; }

    [Column("created_at")]
    public DateTime CreatedAt { get; set; }
}

[Table("person")]
public class Person : BaseModel
{
    [PrimaryKey("id")]
    public int Id { get; set; }

    [Column("first_name")]
    public string FirstName { get; set; }

    [Column("last_name")]
    public string LastName { get; set; }

    [Reference(typeof(Profile))]
    public Profile Profile { get; set; }

    [Column("created_at")]
    public DateTime CreatedAt { get; set; }
}

[Table("profile")]
public class Profile : BaseModel
{
    [Column("email")]
    public string Email { get; set; }
}

Note that each related model should inherit BaseModel and specify its Table and Column attributes as usual.

The Reference Attribute by default will include the referenced model in all GET queries on the table (this can be disabled in its constructor).

As such, a query on the Movie model (given the above) would return something like:

[
    {
        id: 1,
        created_at: "2022-08-20T00:29:45.400188",
        name: "Top Gun: Maverick",
        person: [
            {
                id: 1,
                created_at: "2022-08-20T00:30:02.120528",
                first_name: "Tom",
                last_name: "Cruise",
                profile: {
                    profile_id: 1,
                    email: "tom.cruise@supabase.io",
                    created_at: "2022-08-20T00:30:33.72443"
                }
            },
            {
                id: 3,
                created_at: "2022-08-20T00:30:33.72443",
                first_name: "Bob",
                last_name: "Saggett",
                profile: {
                    profile_id: 3,
                    email: "bob.saggett@supabase.io",
                    created_at: "2022-08-20T00:30:33.72443"
                }
            }
        ]
    },
    // ...
]

Circular References

Circular relations can be added between models, however, circular relations should only be parsed one level deep for models. For example, given the models here, a raw response would look like the following (note that the Person object returns the root Movie and the Person->Profile returns its root Person object).

If desired, this can be avoided by making specific join models that do not have the circular references.

[
  {
    "id": "68722a22-6a6b-4410-a955-b4eb8ca7953f",
    "created_at": "0001-01-01T05:51:00",
    "name": "Supabase in Action",
    "person": [
      {
        "id": "6aa849d8-dd09-4932-bc6f-6fe3b585e87f",
        "first_name": "John",
        "last_name": "Doe",
        "created_at": "0001-01-01T05:51:00",
        "movie": [
          {
            "id": "68722a22-6a6b-4410-a955-b4eb8ca7953f",
            "name": "Supabase in Action",
            "created_at": "0001-01-01T05:51:00"
          }
        ],
        "profile": {
          "person_id": "6aa849d8-dd09-4932-bc6f-6fe3b585e87f",
          "email": "john.doe@email.com",
          "created_at": "0001-01-01T05:51:00",
          "person": {
            "id": "6aa849d8-dd09-4932-bc6f-6fe3b585e87f",
            "first_name": "John",
            "last_name": "Doe",
            "created_at": "0001-01-01T05:51:00"
          }
        }
      },
      {
        "id": "07abc67f-bf7d-4865-b2c0-76013dc2811f",
        "first_name": "Jane",
        "last_name": "Buck",
        "created_at": "0001-01-01T05:51:00",
        "movie": [
          {
            "id": "68722a22-6a6b-4410-a955-b4eb8ca7953f",
            "name": "Supabase in Action",
            "created_at": "0001-01-01T05:51:00"
          }
        ],
        "profile": {
          "person_id": "07abc67f-bf7d-4865-b2c0-76013dc2811f",
          "email": "jane.buck@email.com",
          "created_at": "0001-01-01T05:51:00",
          "person": {
            "id": "07abc67f-bf7d-4865-b2c0-76013dc2811f",
            "first_name": "Jane",
            "last_name": "Buck",
            "created_at": "0001-01-01T05:51:00"
          }
        }
      }
    ]
  }
]

Top Level Filtering

By default relations expect to be used as top level filters on a query. If following the models above, this would mean that a Movie with no Person relations on it would not return on a query unless the Relation has useInnerJoin set to false:

The following model would return any movie, even if there are no Person models associated with it:

[Table("movie")]
public class Movie : BaseModel
{
    [PrimaryKey("id")] 
    public string Id { get; set; }

    [Column("name")] 
    public string? Name { get; set; }

    [Reference(typeof(Person), useInnerJoin: false)]
    public List<Person> People { get; set; } = new();
}

Further Notes:

  • Postgrest does not support nested inserts or upserts. Relational keys on models will be ignored when attempting to insert or upsert on a root model.
  • The Relation attribute uses reflection to only select the attributes specified on the Class Model (i.e. the Profile model has a property only for email, only the property will be requested in the query).

Status

  • Connects to PostgREST Server
  • Authentication
  • Basic Query Features
    • CRUD
    • Single
    • Range (to & from)
    • Limit
    • Limit w/ Foreign Key
    • Offset
    • Offset w/ Foreign Key
  • Advanced Query Features
    • Filters
    • Ordering
  • Custom Serializers
    • Postgres Range
      • int4range, int8range
      • numrange
      • tsrange, tstzrange, daterange
  • Models
    • BaseModel to derive from
    • Coercion of data into Models
  • Unit Testing
  • Nuget Package and Release

Package made possible through the efforts of:

<img src="https://github.com/acupofjose.png" width="150" height="150"> <img src="https://github.com/elrhomariyounes.png" width="150" height="150">
acupofjose elrhomariyounes

Contributing

We are more than happy to have contributions! Please submit a PR.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on postgrest-csharp:

Package Downloads
supabase-csharp

A C# implementation of the Supabase client

realtime-csharp

Realtime-csharp is written as a client library for supabase/realtime.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on postgrest-csharp:

Repository Stars
supabase-community/supabase-csharp
A C# Client library for Supabase
Version Downloads Last updated
3.5.1 3,468 3/15/2024
3.5.0 1,501 1/14/2024
3.4.1 6,842 1/8/2024
3.4.0 315 1/3/2024
3.3.0 2,264 11/29/2023
3.2.10 4,176 11/13/2023
3.2.9 5,422 10/9/2023
3.2.8 124 10/9/2023
3.2.7 2,686 9/15/2023
3.2.6 171 9/5/2023
3.2.5 5,711 7/13/2023
3.2.4 2,819 6/29/2023
3.2.3 508 6/25/2023
3.2.2 1,646 6/10/2023
3.2.1 168 6/10/2023
3.2.0 2,923 5/24/2023
3.1.3 17,212 1/31/2023
3.1.2 834 1/27/2023
3.1.1 1,363 1/17/2023
3.1.0 534 1/16/2023
3.0.4 5,652 11/22/2022
3.0.3 374 11/22/2022
3.0.2 5,579 11/13/2022
3.0.1 439 11/11/2022
3.0.0 431 11/9/2022
2.1.1 1,331 10/19/2022
2.1.0 1,821 10/11/2022
2.0.12 1,411 9/13/2022
2.0.11 5,231 8/2/2022
2.0.10 462 8/1/2022
2.0.9 1,168 7/17/2022
2.0.8 2,660 5/24/2022
2.0.7 6,695 4/9/2022
2.0.6 4,320 12/30/2021
2.0.5 629 12/27/2021
2.0.4 342 12/27/2021
2.0.3 3,305 11/26/2021
2.0.2 1,429 10/23/2021
2.0.1 1,670 9/15/2021
2.0.0 738 9/15/2021
1.0.8 2,046 8/25/2021
1.0.7 508 8/24/2021
1.0.6 2,198 2/4/2021
1.0.5 480 2/3/2021
1.0.4 561 2/2/2021
1.0.3 543 2/1/2021
1.0.2 531 1/29/2021
1.0.1 444 12/31/2020
1.0.0 549 12/20/2020