NON OFFICIAL & EXPERIMENTAL C++ SDK for interacting with the PocketBase Web API.
Work still in progress, many breaking changes without notice as we try to match the official SDKs. Contributions are highly welcome.
- Admin Service
- Records Service (partially)
- Auth With Password
- Auth with OAuth2 (In Plans)
- Auth Refresh
- Request Email Verification
- Confirm Email Verification
- Request Password Reset
- Confirm Password Reset
- Request Email Change
- Confirm Email Change
- List auth methods (Not planned)
- List OAuth2 accounts (Not planned)
- Unlink OAuth2 account (Not planned)
- Crud Service
- Search/List
- GetList
- GetFullList
- GetFistListItem
- View (getOne)
- Create
- Update
- Delete
- Realtime
- Search/List
- Collections Service
- File Service
- Health Service
- Backup Service (Not planned)
- Log Service
- Realtime Service
- Settings Service
- Documentation
Add the library to your dependencies
:
git clone https://github.com/codeartstudios/pocketbase-cpp.git
Import it in your C++ code:
#include "client.h"
using namespace pb;
auto client = PocketBase('http://127.0.0.1:8090');
More detailed API docs and copy-paste examples could be found in the API documentation for each service or in the Services section below.
All services return a standard response, so the error handling is straightforward:
try {
auto result = client.collection('example')->getList(1, 50);
} catch (ClientResponseError e) {
qDebug() << e.what();
}
All response errors are normalized and wrapped as ClientException
with the following public members that you could use:
ClientException {
url QString // The address of the failed request
message QString // The string message
statusCode int // The status code of the failed request
response QMap // The JSON API error response
isAbort bool // Indicates whether the request was aborted/cancelled
originalError std::exception_ptr // The original response error
}
The SDK keeps track of the authenticated token and auth model for you via the pb.authStore()
service.
The default AuthStore
class has the following public members that you could use:
AuthStore {
token: String // Getter for the stored auth token
model: RecordModel|AdminModel|null // Getter for the stored auth RecordModel or AdminModel
isValid bool // Getter to loosely check if the store has an existing and unexpired token
onTokenChanged Signal // Stream that gets triggered on each auth store change
// methods
save(token, model) // update the store with the new auth data
clear() // clears the current auth store state
}
To "logout" an authenticated record or admin, you can just call pb.authStore.clear()
.
To "listen" for changes in the auth store, you can listen to the onTokenChanged
broadcast stream:
pb.authStore.onChange.listen((e) {
print(e.token);
print(e.model);
});
RecordService (API docs)
// Returns a paginated records list.
π client.collection(collectionIdOrName)->getList(/*page*/ 1, /*perPage*/ 30, /*skipTotal*/skipTotal, /*QJsonObject*/params);
// Returns a list with all records batch fetched at once.
π client.collection(collectionIdOrName)->getFullList(/*batch*/ 100, /*QJsonObject*/params);
// Returns the first found record matching the specified filter.
π client.collection(collectionIdOrName)->getFirstListItem(filter, /*QJsonObject*/params);
// Returns a single record by its id.
π client.collection(collectionIdOrName)->getOne(recordId, /*QJsonObject*/params);
// Creates (aka. register) a new record.
π client.collection(collectionIdOrName)->create(/*QJsonObject*/body, /*QJsonObject*/params);
// Updates an existing record by its id.
π client.collection(collectionIdOrName)->update(recordId, /*QJsonObject*/body, /*QJsonObject*/params);
// Deletes a single record by its id.
π client.collection(collectionIdOrName)->deleteOne(recordId);
// Subscribe to realtime changes to the specified topic ("*" or recordId).
//
// It is safe to subscribe multiple times to the same topic.
//
// If you want to remove all subscriptions related to the topic use unsubscribe(topic).
π client.collection("collectionIdOrName")->subscribe( [&](const Event& data) { /* Callback Function */ } );
// Unsubscribe from all registered subscriptions to the specified topic ("*" or recordId).
// If topic is not set, then it will remove all registered collection subscriptions.
π client.collection("collectionIdOrName")->unsubscribe();
π client.collection("collectionIdOrName")->unsubscribe("topic");
π client.collection("collectionIdOrName")->unsubscribe(["topic1", "topic2", ...]);
Available only for "auth" type collections.
// Returns all available application auth methods.
π client.collection(collectionIdOrName)->listAuthMethods(/*QJsonObject*/ params);
// Authenticates a record with their username/email and password.
π client.collection(collectionIdOrName)->authWithPassword(usernameOrEmail, password, /*QJsonObject*/ body, /*QJsonObject*/ query);
// Authenticates a record with OAuth2 code.
π client.collection(collectionIdOrName)->authWithOAuth2(provider, code, codeVerifier, redirectUrl, /*QJsonObject*/ createData, /*QJsonObject*/ body, /*QJsonObject*/ query);
// Refreshes the current authenticated record model and auth token.
π client.collection(collectionIdOrName)->authRefresh(/*QJsonObject*/body, /*QJsonObject*/ query);
// Sends a user password reset email.
π client.collection(collectionIdOrName)->requestPasswordReset(email, /*QJsonObject*/body, /*QJsonObject*/query);
// Confirms a record password reset request.
π client.collection(collectionIdOrName)->confirmPasswordReset(resetToken, newPassword, newPasswordConfirm, {expand?, fields?, query, body, headers});
// Sends a record verification email request.
π client.collection(collectionIdOrName)->requestVerification(email);
// Confirms a record email verification request.
π client.collection(collectionIdOrName)->confirmVerification(verificationToken);
// Sends a record email change request to the provider email.
π client.collection(collectionIdOrName)->requestEmailChange(newEmail);
// Confirms record new email address.
π client.collection(collectionIdOrName)->confirmEmailChange(emailChangeToken, userPassword);
AdminService (API docs)
// Authenticates an admin account by its email and password.
π client.admins()->authWithPassword(email, password, /*QJsonObject*/ params);
// Refreshes the current admin authenticated model and token.
π client.admins()->authRefresh(/*QJsonObject*/ params);
// Sends an admin password reset email.
π client.admins()->requestPasswordReset(email);
// Confirms an admin password reset request.
π client.admins()->confirmPasswordReset(resetToken, newPassword, newPasswordConfirm);
// Returns a paginated admins list.
π client.admins()->getList(/*page*/ 1, /*perPage*/ 30, /*skipTotal*/false, /*QJsonObject*/ params);
// Returns a list with all admins batch fetched at once.
π client.admins()->getFullList(/*batch*/ 100, /*QJsonObject*/ params);
// Returns the first found admin matching the specified filter.
π client.admins()->getFirstListItem(filter, /*QJsonObject*/ params);
// Returns a single admin by their id.
π client.admins()->getOne(id, /*QJsonObject*/ params);
// Creates a new admin.
π client.admins()->create(/*QJsonObject*/ body, /*QJsonObject*/ query);
// Updates an existing admin by their id.
π client.admins()->update(id, /*QJsonObject*/body, /*QJsonObject*/ query);
// Deletes a single admin by their id.
π client.admins()->deleteOne(id);
CollectionService (API docs)
// Returns a paginated collections list.
π client.collections()->getList(/*page*/ 1, /*perPage*/ 30, /*QJsonObject*/ params);
// Returns a list with all collections batch fetched at once.
π client.collections()->getFullList(/*batch*/ 100, /*QJsonObject*/ params);
// Returns the first found collection matching the specified filter.
π client.collections()->getFirstListItem(/*QString*/ filter, /*QJsonObject*/ query);
// Returns a single collection by its id.
π client.collections()->getOne(id, /*QJsonObject*/ params);
// Creates (aka. register) a new collection.
π client.collections()->create(/*QJsonObject*/ body, /*QJsonObject*/ query);
// Updates an existing collection by its id.
π client.collections()->update(id, /*QJsonObject*/ body, /*QJsonObject*/ query);
// Deletes a single collection by its id.
π client.collections()->deleteOne(id);
// Imports the provided collections.
π client.collections()->import(collections, /*deleteMissing*/ false, /*QJsonObject*/ params);
LogService (API docs)
// Returns a paginated logs list.
π client.logs()->getList(/*page*/ 1, /*perPage*/ 30, /*QJsonObject*/ params);
// Returns a single log by its id.
π client.logs()->getOne(id, /*QJsonObject*/ params);
// Returns logs statistics.
π client.logs()->getStats(/*QJsonObject*/ params);
SettingsService (API docs)
// Returns a map with all available app settings.
π client.settings()->getAll(/*QJsonObject*/params);
// Bulk updates app settings.
π client.settings()->update(/*QJsonObject*/params);
// Performs a S3 storage connection test.
π client.settings()->testS3(/*QJsonObject*/params);
// Sends a test email (verification, password-reset, email-change).
π client.settings()->testEmail(toEmail, template);
// Generates a new Apple OAuth2 client secret.
π client.settings()->generateAppleClientSecret(clientId, teamId, keyId, privateKey, duration);
This service is usually used with custom realtime actions. For records realtime subscriptions you can use the subscribe/unsubscribe methods available in the
pb.collection()
RecordService.
// Initialize the realtime connection (if not already) and register the subscription.
//
// You can subscribe to the `PB_CONNECT` event if you want to listen to the realtime connection connect/reconnect events.
π client.realtime()->subscribe("topic", [&](const Event& data) { /* Callback Function */ }, /*Optional params QJsonObject() */ );
// Unsubscribe from a subscription (If topic is empty, all subscriptions are removed).
π client.realtime()->unsubscribe("topic");
// Health check
QJsonObject health = client.health()->check(/*Optional QJsonObject{}*/);