Skip to content

george2giga/PocketBaseCore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PocketBaseCore

A .NET Core and Standard 2.0 wrapper for the PocketBase API.

Installation

Install the package via NuGet:

dotnet add package PocketBaseCore

Usage

Authentication

Before making any requests, authenticate with the API:

var client = new PocketBaseClient("https://your-pocketbase-url");
var authResponse = await client.AuthenticateAsync("your-identity", "your-password");

// or you can pass a custom entity (inheriting from PocketBaseUser) if there are custom fields in the User table (ie: name, avatar, etc)
var authResponse = await client.AuthenticateAsync<MyPocketUser>("your-identity", "your-password");

Creating Records

To create a record using a custom POCO class:

var company = new Company
{
    Name = "Acme Inc",
    Website = "https://acme.com",
    Sector = "Technology"
};

company = await client.CreateRecordAsync<Company>("company", company);

If you prefer not to use a custom POCO class, you can use JsonNode instead:

var company = new JsonObject
{
    ["name"] = "Acme Inc",
    ["website"] = "https://acme.com",
    ["sector"] = "Technology"
};

company = await client.CreateRecordAsync<JsonNode>("company", company);

Updating a Record

To update a record:

var updateData = new Dictionary<string, object>
{
    { "name", "New Acme Inc" }
};

company = await client.UpdateRecordAsync<Company>("company", company.Id, updateData);

Alternatively, using JsonNode:

var updateData = new JsonObject
{
    ["name"] = "New Acme Inc"
};

company = await client.UpdateRecordAsync<JsonNode>("company", company["id"].ToString(), updateData);

Retrieving a Record

To retrieve a record:

var retrievedCompany = await client.GetRecordAsync<Company>("company", company.Id);

Or with JsonNode:

var retrievedCompany = await client.GetRecordAsync<JsonNode>("company", company["id"].ToString());

Working with Relationships

To create records with relationships:

// Create a company
var company = new Company
{
    Name = "Acme Inc",
    Website = "https://acme.com",
    Sector = "Technology"
};
company = await client.CreateRecordAsync<Company>("company", company);

// Create an employee
var employee = new Employee
{
    FullName = "John Doe",
    Email = "johndoe@acme.com",
    PhoneNumber = "1234567890",
    Company = company.Id
};

employee = await client.CreateRecordAsync<Employee>("employee", employee, expand: "company");

Or using JsonNode for both:

// Create a company
var company = new JsonObject
{
    ["name"] = "Acme Inc",
    ["website"] = "https://acme.com",
    ["sector"] = "Technology"
};
company = await client.CreateRecordAsync<JsonNode>("company", company);

// Create an employee
var employee = new JsonObject
{
    ["fullName"] = "John Doe",
    ["email"] = "johndoe@acme.com",
    ["phoneNumber"] = "1234567890",
    ["company"] = company["id"].ToString()
};

employee = await client.CreateRecordAsync<JsonNode>("employee", employee, expand: "company");

Filtering and Sorting

To filter and sort a collection:

var companiesSorted = await client.GetRecordsAsync<Company>("company", fields: "id,name,sector", sort: "-name");
var companiesFiltered = await client.GetRecordsAsync<Company>("company", filter: "(name = 'Company 5')");

Or using JsonNode:

var companiesSorted = await client.GetRecordsAsync<JsonNode>("company", fields: "id,name,sector", sort: "-name");
var companiesFiltered = await client.GetRecordsAsync<JsonNode>("company", filter: "(name = 'Company 5')");

Deleting Records

To delete records, use the following code:

// Delete a company
await client.DeleteRecordAsync("company", company.Id);

// Delete an employee
await client.DeleteRecordAsync("employee", employee.Id);

If using JsonNode:

// Delete a company
await client.DeleteRecordAsync("company", company["id"].ToString());

// Delete an employee
await client.DeleteRecordAsync("employee", employee["id"].ToString());

License

This project is licensed under the MIT License.

About

.Net Core nuget client for PocketBase

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages