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.
var pb = new PocketBaseClient(new Uri("http://127.0.0.1:8090"));
bool isLogged = await pb.Auth.LoginAsync("[email protected]", "testtest");
await pb.Auth.LoginAsync("[email protected]", "testtesttest", "_superusers");
Details about the collection used in these examples.
To be especially exhaustive, here is the collection "posts" in my PocketBase instance :
And the C# class 'Post' that I use in these examples :
public class Post
{
public PocketBaseFileUpload attachment { get; set; }
public string message { get; set; }
}
QueryResult<Post> result = await pb.Collection("posts").GetAsync<Post>();
result.items.ForEach(post => Console.WriteLine(post.message));
QueryResult<Post> result = await pb.Collection("posts")
.Filter("message ?~ 'test'")
.Page(1,10)
.GetAsync<Post>();
string resultRawJson = await pb.Collection("posts").GetAsync();
bool success = await pb.Collection("posts").InsertAsync(new Post(){message = "simple", attachment = new PocketBaseFileUpload(new MemoryStream(File.ReadAllBytes(@"C:\2a.png")), "image.png")});
await pb.Collection("posts")
.NewItem()
.With("message", "fluent method")
.With("attachment", new PocketBaseFileUpload(new MemoryStream(File.ReadAllBytes(@"C:\2a.png"))))
.InsertAsync();
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.
pb.Collection("posts").ListenToChanges<Post>(realtimeaction => Console.WriteLine(realtimeaction.action) );
var realtimecollection = new RealTimeCollection<Post>("http://127.0.0.1:8090");
realtimecollection.OnMessage += action =>
{
Console.WriteLine(action.record.message);
};
realtimecollection.StartListening("posts");
var logs = await pb.ListLogs();
foreach (var log in logs.items)
Console.WriteLine(log.message);