Documentation
¶
Index ¶
- Variables
- func And(filters ...string) string
- func Eq(field, value string) string
- func Gt(field, value string) string
- func Gte(field, value string) string
- func Lt(field, value string) string
- func Lte(field, value string) string
- func Neq(field, value string) string
- func Or(filters ...string) string
- func PrepareRequest(req *http.Request, hasBody bool, opts ...DoOption)
- type AuthenticatedClient
- type Client
- type ClientOption
- type Consumer
- type ConsumerOptions
- type Credentials
- type DoOption
- type HTTPError
- type IDOffsetProvider
- type KVStore
- func (s KVStore) Delete(ctx context.Context, key string) error
- func (s KVStore) Exists(ctx context.Context, key string) (bool, error)
- func (s KVStore) Get(ctx context.Context, key string) (json.RawMessage, error)
- func (s KVStore) List(ctx context.Context, prefix string) ([]string, error)
- func (s KVStore) Set(ctx context.Context, key string, value interface{}) error
- type ListOptions
- type ListResult
- type Message
- type OffsetProvider
- type Repository
- func (r *Repository[T]) Create(ctx context.Context, record T) (*T, error)
- func (r *Repository[T]) Delete(ctx context.Context, id string) error
- func (r *Repository[T]) Get(ctx context.Context, id string) (*T, error)
- func (r *Repository[T]) List(ctx context.Context, opts ListOptions) (*ListResult[T], error)
- func (r *Repository[T]) Update(ctx context.Context, id string, record T) (*T, error)
- type TimestampOffsetProvider
- type TypedKVStore
- func (s TypedKVStore[T]) Delete(ctx context.Context, key string) error
- func (s TypedKVStore[T]) Exists(ctx context.Context, key string) (bool, error)
- func (s TypedKVStore[T]) Get(ctx context.Context, key string) (T, error)
- func (s TypedKVStore[T]) List(ctx context.Context, prefix string) ([]string, error)
- func (s TypedKVStore[T]) Set(ctx context.Context, key string, value T) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrBadRequest = errors.New("bad request") ErrForbidden = errors.New("forbidden") ErrNotFound = errors.New("not found") ErrConflict = errors.New("conflict") ErrValidation = errors.New("validation failed") ErrRateLimited = errors.New("rate limited") ErrServer = errors.New("server error") )
Sentinel errors returned for common HTTP statuses.
Functions ¶
func PrepareRequest ¶ added in v1.0.0
PrepareRequest applies DoOptions to req. It is the single entry point used by external AuthenticatedClient implementations (such as the testpb adapters) so that custom Do impls produce the same on-the-wire shape as the canonical authenticatedClient.Do.
Types ¶
type AuthenticatedClient ¶ added in v0.0.3
type AuthenticatedClient interface {
Do(ctx context.Context, method, path string, body io.Reader, opts ...DoOption) (*http.Response, error)
}
AuthenticatedClient provides authenticated HTTP access to PocketBase.
Custom implementations should call PrepareRequest after setting Authorization on the request to apply DoOption semantics consistently with the canonical implementation.
type Client ¶
type Client interface {
AuthenticateUser(creds Credentials) (AuthenticatedClient, error)
AuthenticateSuperuser(creds Credentials) (AuthenticatedClient, error)
}
Client provides unauthenticated access to PocketBase and can create authenticated clients.
type ClientOption ¶
type ClientOption func(*client)
ClientOption configures optional Client settings.
func WithHTTPClient ¶
func WithHTTPClient(hc *http.Client) ClientOption
WithHTTPClient overrides the default http.Client.
func WithLogger ¶
func WithLogger(logger *slog.Logger) ClientOption
WithLogger attaches a logger used for debug information.
func WithRetry ¶
func WithRetry(maxRetries int, backoff time.Duration) ClientOption
WithRetry sets the maximum number of retries for transient errors and the base backoff.
func WithTimeout ¶
func WithTimeout(timeout time.Duration) ClientOption
WithTimeout sets the HTTP client timeout.
type Consumer ¶ added in v0.0.3
type Consumer[T any] struct { // contains filtered or unexported fields }
Consumer manages offset-tracked consumption from a PocketBase collection.
func NewConsumer ¶ added in v0.0.3
func NewConsumer[T any]( client AuthenticatedClient, collection string, opts ConsumerOptions, ) (*Consumer[T], error)
NewConsumer creates a consumer that reads from a collection with offset tracking.
type ConsumerOptions ¶ added in v0.0.3
type ConsumerOptions struct {
// ConsumerGroup identifies this consumer (REQUIRED).
// Multiple consumers with the same group name will share offset tracking.
//
// WARNING: Unlike Kafka, this does NOT provide load balancing.
// Multiple consumers in the same group will:
// - See and process the SAME messages (duplicates)
// - Have race conditions on offset commits
// - Risk losing at-least-once delivery guarantees
// Use shared groups only for high availability, not load distribution.
ConsumerGroup string
// OffsetProvider determines offset tracking strategy (REQUIRED).
// Use NewTimestampOffsetProvider() or NewIDOffsetProvider().
OffsetProvider OffsetProvider
// Filter to apply when fetching messages (optional).
Filter string
// BatchSize for polling messages (default: 100).
BatchSize int
// PollInterval between checks for new messages (default: 1s).
PollInterval time.Duration
// BufferSize for message channel (default: 100).
BufferSize int
// ErrorBufferSize for error channel (default: 10).
ErrorBufferSize int
// OffsetCollection is the collection name for storing offsets (default: "consumer_offsets").
OffsetCollection string
}
ConsumerOptions configures consumer behavior.
type Credentials ¶ added in v0.0.3
Credentials holds authentication credentials.
type DoOption ¶ added in v1.0.0
type DoOption func(*doConfig)
DoOption configures a single Do invocation.
func WithAuthStaleStatuses ¶ added in v1.0.0
WithAuthStaleStatuses replaces the set of HTTP statuses treated as a signal that the auth token is stale. Default is {401, 403}. Pass (401, 403, 404) to handle the PocketBase rule-check 404 quirk; pass no arguments to disable stale-token detection entirely.
func WithContentType ¶ added in v1.0.0
WithContentType sets the Content-Type header sent with the request. An empty string suppresses Content-Type entirely (no header on the wire). Equivalent to WithHeader("Content-Type", ct); both forms write to the same slot, so last call wins regardless of which form was used.
func WithHeader ¶ added in v1.0.0
WithHeader adds a header to the request. For reserved keys (Authorization, Content-Type) repeated calls collapse to the last value (Set semantics) and an empty value suppresses the header entirely. For other keys values accumulate (Add semantics). A WithHeader value for any key replaces whatever defaults the client had set for that key.
type IDOffsetProvider ¶ added in v0.0.3
type IDOffsetProvider struct{}
IDOffsetProvider tracks offsets using record IDs with lexicographic ordering. PocketBase record IDs are 15-character alphanumeric strings.
func (*IDOffsetProvider) BuildFilter ¶ added in v0.0.3
func (i *IDOffsetProvider) BuildFilter(lastOffset string, userFilter string) string
func (*IDOffsetProvider) ExtractOffset ¶ added in v0.0.3
func (i *IDOffsetProvider) ExtractOffset(record map[string]any) (string, error)
func (*IDOffsetProvider) InitialOffset ¶ added in v0.0.3
func (i *IDOffsetProvider) InitialOffset() string
func (*IDOffsetProvider) SortField ¶ added in v0.0.3
func (i *IDOffsetProvider) SortField() string
type KVStore ¶
type KVStore struct {
// contains filtered or unexported fields
}
KVStore offers simple key-value helpers backed by PocketBase.
func NewKVStore ¶
func NewKVStore(client AuthenticatedClient, collection string, appName string) KVStore
NewKVStore creates a key-value store backed by the provided collection. If collection is empty, a default "kv" collection is used. appName scopes keys when the backing collection includes an "appname" field.
func (KVStore) Delete ¶
Delete removes a key. It is idempotent and returns nil if the key does not exist.
func (KVStore) Get ¶
Get fetches a value for the given key as raw JSON bytes. For collections using a text field, the returned bytes contain the decoded JSON string.
type ListOptions ¶
ListOptions describes pagination and filtering options for list calls.
type ListResult ¶
ListResult contains a page of items with pagination metadata.
type Message ¶ added in v0.0.3
type Message[T any] struct { Data T // contains filtered or unexported fields }
Message wraps a record with commit capability.
type OffsetProvider ¶ added in v0.0.3
type OffsetProvider interface {
// BuildFilter creates a filter expression for records after the given offset.
// lastOffset is the last processed offset (empty string if starting fresh).
// userFilter is an optional additional filter to combine.
BuildFilter(lastOffset string, userFilter string) string
// ExtractOffset extracts the offset value from a record.
// Record is represented as a map for generic handling.
ExtractOffset(record map[string]any) (string, error)
// InitialOffset returns the offset to use when starting fresh (no saved offset).
InitialOffset() string
// SortField returns the field name to sort by for this offset provider.
// Example: "+created" or "+id"
SortField() string
}
OffsetProvider defines how offsets are tracked and queried for message consumption.
func NewIDOffsetProvider ¶ added in v0.0.3
func NewIDOffsetProvider() OffsetProvider
NewIDOffsetProvider creates an ID-based offset provider.
func NewTimestampOffsetProvider ¶ added in v0.0.3
func NewTimestampOffsetProvider() OffsetProvider
NewTimestampOffsetProvider creates a timestamp-based offset provider.
type Repository ¶
type Repository[T any] struct { // contains filtered or unexported fields }
Repository exposes CRUD helpers for PocketBase collections.
func NewRepository ¶
func NewRepository[T any](client AuthenticatedClient, collection string) *Repository[T]
NewRepository creates a repository bound to a PocketBase collection.
func (*Repository[T]) Create ¶
func (r *Repository[T]) Create(ctx context.Context, record T) (*T, error)
Create inserts a new record.
func (*Repository[T]) Delete ¶
func (r *Repository[T]) Delete(ctx context.Context, id string) error
Delete removes a record by ID.
func (*Repository[T]) Get ¶
func (r *Repository[T]) Get(ctx context.Context, id string) (*T, error)
Get fetches a single record by ID.
func (*Repository[T]) List ¶
func (r *Repository[T]) List(ctx context.Context, opts ListOptions) (*ListResult[T], error)
List returns a page of records using the provided options.
type TimestampOffsetProvider ¶ added in v0.0.3
type TimestampOffsetProvider struct{}
TimestampOffsetProvider tracks offsets using PocketBase's built-in created timestamp. This works with any collection without requiring schema changes.
func (*TimestampOffsetProvider) BuildFilter ¶ added in v0.0.3
func (t *TimestampOffsetProvider) BuildFilter(lastOffset string, userFilter string) string
func (*TimestampOffsetProvider) ExtractOffset ¶ added in v0.0.3
func (t *TimestampOffsetProvider) ExtractOffset(record map[string]any) (string, error)
func (*TimestampOffsetProvider) InitialOffset ¶ added in v0.0.3
func (t *TimestampOffsetProvider) InitialOffset() string
func (*TimestampOffsetProvider) SortField ¶ added in v0.0.3
func (t *TimestampOffsetProvider) SortField() string
type TypedKVStore ¶ added in v0.0.3
type TypedKVStore[T any] struct { // contains filtered or unexported fields }
TypedKVStore provides typed helpers built on top of KVStore.
func NewTypedKVStore ¶ added in v0.0.3
func NewTypedKVStore[T any](client AuthenticatedClient, collection string, appName string) TypedKVStore[T]
NewTypedKVStore creates a typed KV store bound to a PocketBase collection.
func (TypedKVStore[T]) Delete ¶ added in v0.0.3
func (s TypedKVStore[T]) Delete(ctx context.Context, key string) error
Delete removes a key. It is idempotent and returns nil if the key does not exist.
func (TypedKVStore[T]) Get ¶ added in v0.0.3
func (s TypedKVStore[T]) Get(ctx context.Context, key string) (T, error)
Get fetches a value for the given key.