All Projects → supabase-community → postgrest-csharp

supabase-community / postgrest-csharp

Licence: MIT license
A C# Client library for Postgrest

Programming Languages

C#
18002 projects
PLpgSQL
1095 projects

Projects that are alternatives of or similar to postgrest-csharp

postgrest-swift
Swift client for PostgREST
Stars: ✭ 23 (-62.9%)
Mutual labels:  postgrest, supabase
Supabase
The open source Firebase alternative. Follow to stay updated about our public Beta.
Stars: ✭ 25,142 (+40451.61%)
Mutual labels:  postgrest, supabase
madewithsupabase
A collection of projects made with Supabase – Websites, Mobile Apps, SaaS, Plugins and more!
Stars: ✭ 84 (+35.48%)
Mutual labels:  supabase
react-supabase
React Hooks library for Supabase
Stars: ✭ 168 (+170.97%)
Mutual labels:  supabase
spot
Open source geo based video sharing social app created with Flutter, Supabase and lots of love 💙💙💙
Stars: ✭ 224 (+261.29%)
Mutual labels:  supabase
hot
🍕The site that recommends the hottest projects on GitHub.
Stars: ✭ 57 (-8.06%)
Mutual labels:  supabase
ra-data-postgrest
react admin client for postgREST
Stars: ✭ 80 (+29.03%)
Mutual labels:  postgrest
vue-supabase
A supa simple wrapper around Supabase.js to enable usage within Vue.
Stars: ✭ 57 (-8.06%)
Mutual labels:  supabase
general-angular
Realtime Angular Admin/CRUD Front End App
Stars: ✭ 24 (-61.29%)
Mutual labels:  postgrest
wrapped
GitHub Wrapped, inspired by Spotify Wrapped
Stars: ✭ 159 (+156.45%)
Mutual labels:  supabase
swappy-one
swappy.one
Stars: ✭ 24 (-61.29%)
Mutual labels:  supabase
top-nuxt3
Full stack Nuxt 3 Template starter with Supabase and Tailwindcss
Stars: ✭ 59 (-4.84%)
Mutual labels:  supabase
simple-rest-api
😎 A simple RESTful API in three easy steps.
Stars: ✭ 25 (-59.68%)
Mutual labels:  postgrest
supaflare
URL shortener / redirection service powered by Supabase, Cloudflare Workers, Workers KV and Cloudflare Pages.
Stars: ✭ 51 (-17.74%)
Mutual labels:  supabase
PostgREST-writeAPI
Translate your OpenAPI specification into a NGinx config-file to implement your PostgREST system
Stars: ✭ 31 (-50%)
Mutual labels:  postgrest
svelte-starter-kit
Svelte with brilliant bells and useful whistles
Stars: ✭ 384 (+519.35%)
Mutual labels:  supabase
Postgrest
REST API for any Postgres database
Stars: ✭ 18,166 (+29200%)
Mutual labels:  postgrest
github-chat
A chat room for every GitHub repository. Real-time.
Stars: ✭ 25 (-59.68%)
Mutual labels:  supabase
effective
Effective: end-to-end encrypted project management for activists and human rights organizations. Making activists 10x more powerful via ultra effective communities of action and autonomous software. [deprecated]
Stars: ✭ 75 (+20.97%)
Mutual labels:  postgrest
docker-postgrest
REST API for any Postgres database (PostgREST Docker Image)
Stars: ✭ 22 (-64.52%)
Mutual labels:  postgrest


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.

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

Utitilizing 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", false)]
    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",false)]
    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 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: "[email protected]",
                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: "[email protected]",
                    created_at: "2022-08-20T00:30:33.72443"
                }
            }
        ]
    },
    // ...
]

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:

acupofjose elrhomariyounes

Contributing

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].