r--w / pocketbase

PocketBase Go SDK
MIT License
83 stars 30 forks source link

Check & test & build 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.

Status

I'm looking for passionate and dedicated individuals to help maintain this project.

UPDATE: actively maintained fork: https://github.com/pluja/pocketbase

Compatibility

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:

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

Contributing