pocketbase

package module
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2023 License: MIT Imports: 14 Imported by: 3

README

PocketBase

Project

This repository contains community-maintained Go SDK for Pocketbase API. It's well-tested and used in production in Coinpaprika, but not all endpoints are covered yet.

Compatibility

  • v0.13.0 version of SDK is compatible with Pocketbase v0.13.x and higher
  • v0.12.0 version of SDK is compatible with Pocketbase v0.12.x
  • v0.11.0 version of SDK is compatible with Pocketbase v0.11.x
  • v0.10.1 version of SDK is compatible with Pocketbase v0.10.x
  • v0.9.2 version of SDK is compatible with Pocketbase v0.9.x (SSE & generics support introduced)
  • v0.8.0 version of SDK is compatible with Pocketbase v0.8.x

PocketBase

Pocketbase is a simple, self-hosted, open-source, no-code, database for your personal data. It's a great alternative to Airtable, Notion, and Google Sheets. Source code is available on GitHub

Currently supported operations

This SDK doesn't have feature parity with official SDKs and supports the following operations:

  • Authentication - anonymous, admin and user via email/password
  • Create
  • Update
  • Delete
  • List - with pagination, filtering, sorting
  • Other - feel free to create an issue or contribute

Usage & examples

Simple list example without authentication (assuming your collections are public):

package main

import (
	"log"

	"github.com/r--w/pocketbase"
)

func main() {
	client := pocketbase.NewClient("http://localhost:8090")
	response, err := client.List("posts_public", pocketbase.ParamsList{
		Page: 1, Size: 10, Sort: "-created", Filters: "field~'test'",
	})
	if err != nil {
		log.Fatal(err)
	}
	log.Print(response.TotalItems)
}

Creating an item with admin user (auth via email/pass). Please note that you can pass map[string]any or struct with JSON tags as a payload:

package main

import (
	"log"

	"github.com/r--w/pocketbase"
)

func main() {
	client := pocketbase.NewClient("http://localhost:8090", 
		pocketbase.WithAdminEmailPassword("admin@admin.com", "admin@admin.com"))
	response, err := client.Create("posts_admin", map[string]any{
		"field": "test",
	})
	if err != nil {
		log.Fatal(err)
	}
	log.Print(response.ID)
}

For even easier interaction with collection results as user-defined types, you can go with CollectionSet:

package main

import (
	"log"

	"github.com/r--w/pocketbase"
)

type post struct {
	ID      string
	Field   string
	Created string
}

func main() {
	client := pocketbase.NewClient("http://localhost:8090")
	collection := pocketbase.CollectionSet[post](client, "posts_public")
	response, err := collection.List(pocketbase.ParamsList{
		Page: 1, Size: 10, Sort: "-created", Filters: "field~'test'",
	})
	if err != nil {
		log.Fatal(err)
	}
	
    log.Printf("%+v", response.Items)
}

Realtime API via Server-Sent Events (SSE) is also supported:

package main

import (
	"log"

	"github.com/r--w/pocketbase"
)

type post struct {
	ID      string
	Field   string
	Created string
}

func main() {
	client := pocketbase.NewClient("http://localhost:8090")
	collection := pocketbase.CollectionSet[post](client, "posts_public")
	response, err := collection.List(pocketbase.ParamsList{
		Page: 1, Size: 10, Sort: "-created", Filters: "field~'test'",
	})
	if err != nil {
		log.Fatal(err)
	}
	
	stream, err := collection.Subscribe()
	if err != nil {
		log.Fatal(err)
	}
	defer stream.Unsubscribe()
	<-stream.Ready()
	for ev := range stream.Events() {
		log.Print(ev.Action, ev.Record)
	}
}

More examples can be found in:

Development

Makefile targets

  • make serve - builds all binaries and runs local PocketBase server, it will create collections and sample data based on migration files
  • make test - runs tests (make sure that PocketBase server is running - make serve before)
  • make check - runs linters and security checks (run this before commit)
  • make build - builds all binaries (examples and PocketBase server)
  • make help - shows help and other targets

Contributing

  • Go 1.20+ (for making changes in the Go code)
  • While developing use WithDebug() client option to see HTTP requests and responses
  • Make sure that all checks are green (run make check before commit)
  • Make sure that all tests pass (run make test before commit)
  • Create a PR with your changes and wait for review

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidResponse = errors.New("invalid response")

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(url string, opts ...ClientOption) *Client

func (*Client) Authorize

func (c *Client) Authorize() error

func (*Client) Create

func (c *Client) Create(collection string, body any) (ResponseCreate, error)

func (*Client) Delete

func (c *Client) Delete(collection string, id string) error

func (*Client) List

func (c *Client) List(collection string, params ParamsList) (ResponseList[map[string]any], error)

func (*Client) Update

func (c *Client) Update(collection string, id string, body any) error

type ClientOption

type ClientOption func(*Client)

func WithAdminEmailPassword

func WithAdminEmailPassword(email, password string) ClientOption

func WithDebug

func WithDebug() ClientOption

func WithUserEmailPassword

func WithUserEmailPassword(email, password string) ClientOption

type Collection added in v0.9.2

type Collection[T any] struct {
	*Client
	Name string
}

func CollectionSet added in v0.9.2

func CollectionSet[T any](client *Client, collection string) Collection[T]

func (Collection[T]) Create added in v0.9.2

func (c Collection[T]) Create(body T) (ResponseCreate, error)

func (Collection[T]) Delete added in v0.9.2

func (c Collection[T]) Delete(id string) error

func (Collection[T]) List added in v0.9.2

func (c Collection[T]) List(params ParamsList) (ResponseList[T], error)

func (Collection[T]) One added in v0.10.3

func (c Collection[T]) One(id string) (T, error)

func (Collection[T]) Subscribe added in v0.9.2

func (c Collection[T]) Subscribe(targets ...string) (*Stream[T], error)

func (Collection[T]) SubscribeWith added in v0.10.3

func (c Collection[T]) SubscribeWith(opts SubscribeOptions, targets ...string) (*Stream[T], error)

func (Collection[T]) Update added in v0.9.2

func (c Collection[T]) Update(id string, body T) error

type Event added in v0.9.2

type Event[T any] struct {
	Action string `json:"action"`
	Record T      `json:"record"`
	Error  error  `json:"-"`
}

type ParamsList

type ParamsList struct {
	Page    int
	Size    int
	Filters string
	Sort    string
	// contains filtered or unexported fields
}

type ResponseCreate

type ResponseCreate struct {
	ID      string `json:"id"`
	Created string `json:"created"`
	Field   string `json:"field"`
	Updated string `json:"updated"`
}

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"`
}

type Stream added in v0.9.2

type Stream[T any] struct {
	// contains filtered or unexported fields
}

func (*Stream[T]) Events added in v0.9.2

func (s *Stream[T]) Events() <-chan Event[T]

func (*Stream[T]) Ready added in v0.10.3

func (s *Stream[T]) Ready() <-chan struct{}

func (*Stream[T]) Unsubscribe added in v0.9.2

func (s *Stream[T]) Unsubscribe()

func (*Stream[T]) WaitAuthReady deprecated added in v0.9.2

func (s *Stream[T]) WaitAuthReady() error

Deprecated: use <-stream.Ready() instead of

type SubscribeOptions added in v0.10.3

type SubscribeOptions struct {
	ReconnectStrategy backoff.BackOff
}

type SubscriptionsSet added in v0.9.2

type SubscriptionsSet struct {
	ClientID      string   `json:"clientId"`
	Subscriptions []string `json:"subscriptions"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL