Documentation
¶
Overview ¶
Package pocketbase provides a Go SDK for interacting with PocketBase APIs.
This package offers type-safe, idiomatic Go interfaces for PocketBase operations including authentication, CRUD operations, real-time subscriptions, and backup management.
Example usage:
client := pocketbase.NewClient("http://localhost:8090")
records, err := client.List("posts", pocketbase.ParamsList{})
For type-safe operations, use CollectionSet:
collection := pocketbase.CollectionSet[MyStruct](client, "posts")
records, err := collection.List(pocketbase.ParamsList{})
Index ¶
- Variables
- func EnvIsTruthy(key string) bool
- type AuthMethod
- type AuthMethodsResponse
- type AuthProvider
- type AuthRefreshResponse
- type AuthWithOauth2Response
- type AuthWithPasswordResponse
- type Backup
- func (b Backup) Create(key ...string) error
- func (b Backup) Delete(key string) error
- func (b Backup) FullList() ([]ResponseBackupFullList, error)
- func (b Backup) GetDownloadURL(token string, key string) (string, error)
- func (b Backup) Restore(key string) error
- func (b Backup) Upload(key string, reader io.Reader) error
- type Client
- func (c *Client) AuthStore() authStore
- func (c *Client) Authorize() error
- func (c *Client) Backup() Backup
- func (c *Client) Create(collection string, body any) (ResponseCreate, error)
- func (c *Client) Delete(collection string, id string) error
- func (c *Client) Files() Files
- func (c *Client) FullList(collection string, params ParamsList) (ResponseList[map[string]any], error)
- func (c *Client) Get(path string, result any, onRequest func(*resty.Request), ...) error
- func (c *Client) List(collection string, params ParamsList) (ResponseList[map[string]any], error)
- func (c *Client) One(collection string, id string) (map[string]any, error)
- func (c *Client) OneTo(collection string, id string, result any) error
- func (c *Client) Update(collection string, id string, body any) error
- type ClientOption
- func WithAdminEmailPassword(email, password string) ClientOption
- func WithAdminEmailPassword22(email, password string) ClientOption
- func WithAdminToken(token string) ClientOption
- func WithAdminToken22(token string) ClientOption
- func WithRestDebug() ClientOption
- func WithRetry(count int, waitTime, maxWaitTime time.Duration) ClientOption
- func WithSseDebug() ClientOption
- func WithTimeout(timeout time.Duration) ClientOption
- func WithUserEmailPassword(email, password string) ClientOption
- func WithUserEmailPasswordAndCollection(email, password, collection string) ClientOption
- func WithUserToken(token string) ClientOption
- type Collection
- func (c *Collection[T]) AuthRefresh() (AuthRefreshResponse, error)
- func (c *Collection[T]) AuthWithOAuth2Code(provider string, code string, codeVerifier string, redirectURL string) (AuthWithOauth2Response, error)
- func (c *Collection[T]) AuthWithPassword(username string, password string) (AuthWithPasswordResponse, error)
- func (c *Collection[T]) ConfirmEmailChange(emailChangeToken string, password string) error
- func (c *Collection[T]) ConfirmPasswordReset(passwordResetToken string, password string, passwordConfirm string) error
- func (c *Collection[T]) ConfirmVerification(verificationToken string) error
- func (c *Collection[T]) Create(body T) (ResponseCreate, error)
- func (c *Collection[T]) Delete(id string) error
- func (c *Collection[T]) FullList(params ParamsList) (ResponseList[T], error)
- func (c *Collection[T]) List(params ParamsList) (ResponseList[T], error)
- func (c *Collection[T]) ListAuthMethods() (AuthMethodsResponse, error)
- func (c *Collection[T]) ListAuthMethods22() (AuthMethod, error)
- func (c *Collection[T]) ListExternalAuths22(recordID string) ([]ExternalAuthRequest, error)
- func (c *Collection[T]) One(id string) (T, error)
- func (c *Collection[T]) OneWithParams(id string, params ParamsList) (T, error)
- func (c *Collection[T]) RequestEmailChange(newEmail string) error
- func (c *Collection[T]) RequestPasswordReset(email string) error
- func (c *Collection[T]) RequestVerification(email string) error
- func (c *Collection[T]) Subscribe(targets ...string) (*Stream[T], error)
- func (c *Collection[T]) SubscribeWith(opts SubscribeOptions, targets ...string) (*Stream[T], error)
- func (c *Collection[T]) UnlinkExternalAuth22(recordID string, provider string) error
- func (c *Collection[T]) Update(id string, body T) error
- type CreateRequest
- type Event
- type ExternalAuthRequest
- type Files
- type ParamsList
- type Record
- type ResponseBackupFullList
- type ResponseCreate
- type ResponseGetToken
- type ResponseList
- type Stream
- type SubscribeOptions
- type SubscriptionsSet
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidResponse = errors.New("invalid response")
ErrInvalidResponse is returned when PocketBase returns an invalid response.
Functions ¶
func EnvIsTruthy ¶
EnvIsTruthy checks if an environment variable is set to a truthy value (1, true, yes).
Types ¶
type AuthMethod ¶
type AuthMethod struct {
AuthProviders []AuthProvider `json:"authProviders"`
UsernamePassword bool `json:"usernamePassword"`
EmailPassword bool `json:"emailPassword"`
OnlyVerified bool `json:"onlyVerified"`
}
AuthMethod represents the available authentication methods for a collection.
type AuthMethodsResponse ¶
type AuthMethodsResponse struct {
Password passwordResponse `json:"password"`
OAuth2 oauth2Response `json:"oauth2"`
MFA mfaResponse `json:"mfa"`
OTP otpResponse `json:"otp"`
// legacy fields
// @todo remove after dropping v0.22 support
AuthProviders []providerInfo `json:"authProviders"`
UsernamePassword bool `json:"usernamePassword"`
EmailPassword bool `json:"emailPassword"`
}
AuthMethodsResponse represents the response structure for authentication methods. Borrowed from https://github.com/pocketbase/pocketbase/blob/844f18cac379fc749493dc4dd73638caa89167a1/apis/record_auth_methods.go#L52
type AuthProvider ¶
type AuthProvider struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
State string `json:"state"`
AuthURL string `json:"authUrl"`
CodeVerifier string `json:"codeVerifier"`
CodeChallenge string `json:"codeChallenge"`
CodeChallengeMethod string `json:"codeChallengeMethod"`
}
AuthProvider represents an OAuth2 authentication provider configuration.
type AuthRefreshResponse ¶
type AuthRefreshResponse struct {
Record struct {
Avatar string `json:"avatar"`
CollectionID string `json:"collectionId"`
CollectionName string `json:"collectionName"`
Created string `json:"created"`
Email string `json:"email"`
EmailVisibility bool `json:"emailVisibility"`
ID string `json:"id"`
Name string `json:"name"`
Updated string `json:"updated"`
Username string `json:"username"`
Verified bool `json:"verified"`
} `json:"record"`
Token string `json:"token"`
}
AuthRefreshResponse represents the response from authentication token refresh.
type AuthWithOauth2Response ¶
type AuthWithOauth2Response struct {
Token string `json:"token"`
}
AuthWithOauth2Response represents the response from OAuth2 authentication.
type AuthWithPasswordResponse ¶
AuthWithPasswordResponse represents the response from password authentication.
type Backup ¶
type Backup struct {
*Client
}
Backup provides methods for managing PocketBase backup operations.
func (Backup) Delete ¶
Delete deletes a single backup file.
Example:
file, _ := os.Open("./backups/pb_backup.zip")
defer file.Close()
_ = defaultClient.Backup().Upload("mybackup.zip", file)
func (Backup) FullList ¶
func (b Backup) FullList() ([]ResponseBackupFullList, error)
FullList returns list with all available backup files.
func (Backup) GetDownloadURL ¶
GetDownloadURL builds a download url for a single existing backup using an admin file token and the backup file key.
The file token can be generated via `client.Files().GetToken()`.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a PocketBase API client with authentication and HTTP capabilities.
func NewClient ¶
func NewClient(url string, opts ...ClientOption) *Client
NewClient creates a new PocketBase API client with the specified URL and options.
func (*Client) AuthStore ¶
func (c *Client) AuthStore() authStore
AuthStore returns the client's authentication store.
func (*Client) Authorize ¶
Authorize performs authentication using the configured authorization method.
func (*Client) Create ¶
func (c *Client) Create(collection string, body any) (ResponseCreate, error)
Create creates a new record in the specified collection.
func (*Client) FullList ¶
func (c *Client) FullList(collection string, params ParamsList) (ResponseList[map[string]any], error)
FullList retrieves all records from the specified collection without pagination.
func (*Client) Get ¶
func (c *Client) Get(path string, result any, onRequest func(*resty.Request), onResponse func(*resty.Response)) error
Get performs a GET request to the specified path with optional request/response hooks.
func (*Client) List ¶
func (c *Client) List(collection string, params ParamsList) (ResponseList[map[string]any], error)
List retrieves a paginated list of records from the specified collection.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption is a function type for configuring Client instances.
func WithAdminEmailPassword ¶
func WithAdminEmailPassword(email, password string) ClientOption
WithAdminEmailPassword configures admin authentication using email and password.
func WithAdminEmailPassword22 ¶
func WithAdminEmailPassword22(email, password string) ClientOption
WithAdminEmailPassword22 configures admin authentication using email and password (legacy version).
func WithAdminToken ¶
func WithAdminToken(token string) ClientOption
WithAdminToken configures admin authentication using a token.
func WithAdminToken22 ¶
func WithAdminToken22(token string) ClientOption
WithAdminToken22 configures admin authentication using a token (legacy version).
func WithRestDebug ¶
func WithRestDebug() ClientOption
WithRestDebug enables REST API debug logging for the client.
func WithRetry ¶
func WithRetry(count int, waitTime, maxWaitTime time.Duration) ClientOption
WithRetry set the retry settings for requests (defaults: count=3, waitTime=3s, maxWaitTime=10s)
func WithSseDebug ¶
func WithSseDebug() ClientOption
WithSseDebug enables Server-Sent Events debug logging for the client.
func WithTimeout ¶
func WithTimeout(timeout time.Duration) ClientOption
WithTimeout set the timeout for requests
func WithUserEmailPassword ¶
func WithUserEmailPassword(email, password string) ClientOption
WithUserEmailPassword configures user authentication using email and password.
func WithUserEmailPasswordAndCollection ¶
func WithUserEmailPasswordAndCollection(email, password, collection string) ClientOption
WithUserEmailPasswordAndCollection configures user authentication for a specific collection.
func WithUserToken ¶
func WithUserToken(token string) ClientOption
WithUserToken configures user authentication using a token.
type Collection ¶
Collection represents a type-safe wrapper around a PocketBase collection.
func CollectionSet ¶
func CollectionSet[T any](client *Client, collection string) *Collection[T]
CollectionSet creates a new type-safe collection wrapper for the specified collection.
func (*Collection[T]) AuthRefresh ¶
func (c *Collection[T]) AuthRefresh() (AuthRefreshResponse, error)
AuthRefresh refreshes the current authenticated record instance and * returns a new token and record data.
func (*Collection[T]) AuthWithOAuth2Code ¶
func (c *Collection[T]) AuthWithOAuth2Code(provider string, code string, codeVerifier string, redirectURL string) (AuthWithOauth2Response, error)
AuthWithOAuth2Code authenticate a single auth collection record with OAuth2 code.
If you don't have an OAuth2 code you may also want to check `authWithOAuth2` method.
On success, this method also automatically updates the client's AuthStore data and returns: - the authentication token via the model - the authenticated record model - the OAuth2 account data (eg. name, email, avatar, etc.)
func (*Collection[T]) AuthWithPassword ¶
func (c *Collection[T]) AuthWithPassword(username string, password string) (AuthWithPasswordResponse, error)
AuthWithPassword authenticate a single auth collection record via its username/email and password.
On success, this method also automatically updates the client's AuthStore data and returns: - the authentication token via the AuthWithPasswordResponse - the authenticated record model
func (*Collection[T]) ConfirmEmailChange ¶
func (c *Collection[T]) ConfirmEmailChange(emailChangeToken string, password string) error
ConfirmEmailChange confirms auth record's new email address.
func (*Collection[T]) ConfirmPasswordReset ¶
func (c *Collection[T]) ConfirmPasswordReset(passwordResetToken string, password string, passwordConfirm string) error
ConfirmPasswordReset confirms auth record password reset request.
func (*Collection[T]) ConfirmVerification ¶
func (c *Collection[T]) ConfirmVerification(verificationToken string) error
ConfirmVerification confirms auth record email verification request.
If the current `client.authStore.model` matches with the auth record from the token, then on success the `client.authStore.model.verified` will be updated to `true`.
func (*Collection[T]) Create ¶
func (c *Collection[T]) Create(body T) (ResponseCreate, error)
Create creates a new record in the collection.
func (*Collection[T]) Delete ¶
func (c *Collection[T]) Delete(id string) error
Delete removes a record from the collection by ID.
func (*Collection[T]) FullList ¶
func (c *Collection[T]) FullList(params ParamsList) (ResponseList[T], error)
FullList retrieves all records from the collection without pagination.
func (*Collection[T]) List ¶
func (c *Collection[T]) List(params ParamsList) (ResponseList[T], error)
List retrieves a paginated list of records from the collection.
func (*Collection[T]) ListAuthMethods ¶
func (c *Collection[T]) ListAuthMethods() (AuthMethodsResponse, error)
ListAuthMethods returns all available collection auth methods.
func (*Collection[T]) ListAuthMethods22 ¶
func (c *Collection[T]) ListAuthMethods22() (AuthMethod, error)
ListAuthMethods22 returns all available collection auth methods (legacy version).
func (*Collection[T]) ListExternalAuths22 ¶
func (c *Collection[T]) ListExternalAuths22(recordID string) ([]ExternalAuthRequest, error)
ListExternalAuths22 lists all linked external auth providers for the specified auth record.
func (*Collection[T]) One ¶
func (c *Collection[T]) One(id string) (T, error)
One retrieves a single record from the collection by ID.
func (*Collection[T]) OneWithParams ¶
func (c *Collection[T]) OneWithParams(id string, params ParamsList) (T, error)
OneWithParams retrieves a single record from the collection by ID with additional parameters. Only fields and expand parameters are supported.
func (*Collection[T]) RequestEmailChange ¶
func (c *Collection[T]) RequestEmailChange(newEmail string) error
RequestEmailChange sends an email change request to the authenticated record model.
func (*Collection[T]) RequestPasswordReset ¶
func (c *Collection[T]) RequestPasswordReset(email string) error
RequestPasswordReset sends auth record password reset request
func (*Collection[T]) RequestVerification ¶
func (c *Collection[T]) RequestVerification(email string) error
RequestVerification sends auth record verification email request.
func (*Collection[T]) Subscribe ¶
func (c *Collection[T]) Subscribe(targets ...string) (*Stream[T], error)
Subscribe creates a real-time subscription to the collection with default options.
func (*Collection[T]) SubscribeWith ¶
func (c *Collection[T]) SubscribeWith(opts SubscribeOptions, targets ...string) (*Stream[T], error)
SubscribeWith creates a real-time subscription with custom options and target collections.
func (*Collection[T]) UnlinkExternalAuth22 ¶
func (c *Collection[T]) UnlinkExternalAuth22(recordID string, provider string) error
UnlinkExternalAuth22 unlinks a single external auth provider from the specified auth record.
func (*Collection[T]) Update ¶
func (c *Collection[T]) Update(id string, body T) error
Update updates a record in the collection with the specified ID.
type CreateRequest ¶
type CreateRequest struct {
Name string `json:"name"`
}
CreateRequest represents the request structure for creating a backup.
type Event ¶
type Event[T any] struct { Action string `json:"action"` Record T `json:"record"` Error error `json:"-"` }
Event represents a real-time event from PocketBase with action, record data, and optional error.
type ExternalAuthRequest ¶
type ExternalAuthRequest struct {
ID string `json:"id"`
Created string `json:"created"`
Updated string `json:"updated"`
RecordID string `json:"recordId"`
CollectionID string `json:"collectionId"`
Provider string `json:"provider"`
ProviderID string `json:"providerId"`
}
ExternalAuthRequest represents an external authentication provider link.
type Files ¶
type Files struct {
*Client
}
Files provides methods for managing PocketBase file operations.
type ParamsList ¶
type ParamsList struct {
Page int
Size int
Filters string
Sort string
Expand string
Fields string
// contains filtered or unexported fields
}
ParamsList represents query parameters for PocketBase API requests including pagination, filtering, and sorting.
type Record ¶
type Record struct {
Avatar string `json:"avatar"`
CollectionID string `json:"collectionId"`
CollectionName string `json:"collectionName"`
Created string `json:"created"`
Email string `json:"email"`
EmailVisibility bool `json:"emailVisibility"`
ID string `json:"id"`
Name string `json:"name"`
Updated string `json:"updated"`
Username string `json:"username"`
Verified bool `json:"verified"`
}
Record represents a PocketBase record with common fields.
type ResponseBackupFullList ¶
type ResponseBackupFullList struct {
Key string `json:"key"`
Size int `json:"size"`
Modified string `json:"modified"`
}
ResponseBackupFullList represents a backup file in the backup list response.
type ResponseCreate ¶
type ResponseCreate struct {
ID string `json:"id"`
Created string `json:"created"`
Field string `json:"field"`
Updated string `json:"updated"`
}
ResponseCreate represents the response from creating a new record.
type ResponseGetToken ¶
type ResponseGetToken struct {
Token string `json:"token"`
}
ResponseGetToken represents the response from requesting a file access token.
type ResponseList ¶
type ResponseList[T any] struct { Page int `json:"page"` PerPage int `json:"perPage"` TotalItems int `json:"totalItems"` TotalPages int `json:"totalPages"` Items []T `json:"items"` }
ResponseList represents a paginated list response from PocketBase.
type Stream ¶
type Stream[T any] struct { // contains filtered or unexported fields }
Stream represents a real-time event stream with subscription management capabilities.
func (*Stream[T]) Ready ¶
func (s *Stream[T]) Ready() <-chan struct{}
Ready returns a channel that closes when the stream is ready to receive events.
func (*Stream[T]) Unsubscribe ¶
func (s *Stream[T]) Unsubscribe()
Unsubscribe closes the stream and cleans up resources.
func (*Stream[T]) WaitAuthReady ¶
WaitAuthReady waits for the stream to be ready for authentication. Deprecated: use <-stream.Ready() instead.
type SubscribeOptions ¶
SubscribeOptions configures real-time subscription behavior including reconnection strategy.
type SubscriptionsSet ¶
type SubscriptionsSet struct {
ClientID string `json:"clientId"`
Subscriptions []string `json:"subscriptions"`
}
SubscriptionsSet represents the subscription configuration sent to PocketBase.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
pocketbase
command
Package main provides the PocketBase server executable.
|
Package main provides the PocketBase server executable. |
|
Package main provides usage examples for the PocketBase Go SDK.
|
Package main provides usage examples for the PocketBase Go SDK. |
|
Package migrations provides database migration scripts for PocketBase collections.
|
Package migrations provides database migration scripts for PocketBase collections. |