Skip to content

kikipoulet/PocketBaseDotnetClient

Repository files navigation

Simple C# Client for PocketBase. WIP.

PocketBase is weirdly pleasant to work with, so I focus on giving as simple as possible APIs in this library.

Right now, this library can :

  • Auth with mail & password
  • List Collections
  • List Items in a Collection, and use Filters
  • Insert Item in a Collection
  • List Logs
  • Use Real-Time to listen changes in a Collection

Probably more to come obviously, especially on CRUD.


Auth


var pb = new PocketBaseClient(new Uri("http://127.0.0.1:8090"));

bool isLogged = await pb.Auth.LoginAsync("[email protected]", "testtest");

Auth in a custom collection

await pb.Auth.LoginAsync("[email protected]", "testtesttest", "_superusers");

Collections


Details about the collection used in these examples.

To be especially exhaustive, here is the collection "posts" in my PocketBase instance :

{8C290699-8066-4540-9E52-96B244F68A14}


And the C# class 'Post' that I use in these examples :

public class Post
{
        public PocketBaseFileUpload attachment { get; set; } 
        
        public string message { get; set; }
}

List Items


QueryResult<Post> result = await pb.Collection("posts").GetAsync<Post>();

result.items.ForEach(post => Console.WriteLine(post.message));

Filters

QueryResult<Post> result = await pb.Collection("posts")
                                   .Filter("message ?~ 'test'")
                                   .Page(1,10) 
                                   .GetAsync<Post>();

RAW Json Output

 string resultRawJson = await pb.Collection("posts").GetAsync();

Insert Item


bool success = await pb.Collection("posts").InsertAsync(new Post(){message = "simple", attachment = new PocketBaseFileUpload(new MemoryStream(File.ReadAllBytes(@"C:\2a.png")), "image.png")}); 

Fluent API Method

await pb.Collection("posts")
             .NewItem()
             .With("message", "fluent method")
             .With("attachment",  new PocketBaseFileUpload(new MemoryStream(File.ReadAllBytes(@"C:\2a.png"))))
             .InsertAsync();

List Collections


var collections = await pb.ListCollections();

foreach (var collection in collections.items)
    Console.WriteLine(collection.name);

NB : You probably need to log in as superuser to do that.


Real-Time


Using PocketBaseClient

pb.Collection("posts").ListenToChanges<Post>(realtimeaction => Console.WriteLine(realtimeaction.action) );

Alternative

var realtimecollection = new RealTimeCollection<Post>("http://127.0.0.1:8090");

realtimecollection.OnMessage += action =>
{
    Console.WriteLine(action.record.message);
};

realtimecollection.StartListening("posts");

Logs


var logs = await pb.ListLogs();

foreach (var log in logs.items)
    Console.WriteLine(log.message);

About

Simple C# client for PocketBase.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages