forked from r--w/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
66 lines (53 loc) · 1.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"errors"
"log"
"time"
"github.com/mitchellh/mapstructure"
"github.com/pluja/pocketbase"
)
type Post struct {
Field string `json:"field"`
ID string `json:"id"`
}
func main() {
// REMEMBER to start the Pocketbase before running this example with `make serve` command
var errs error
client := pocketbase.NewClient("http://localhost:8090")
// Other configuration options:
// pocketbase.WithAdminEmailPassword("[email protected]", "[email protected]")
// pocketbase.WithUserEmailPassword("[email protected]", "[email protected]")
// pocketbase.WithUserToken(token)
// pocketbase.WithAdminToken(token)
// pocketbase.WithDebug()
response, err := client.List("posts_public", pocketbase.ParamsList{
Size: 1,
Page: 1,
Sort: "-created",
Filters: "field~'test'",
})
errs = errors.Join(errs, err)
log.Printf("Total items: %d, total pages: %d\n", response.TotalItems, response.TotalPages)
for _, item := range response.Items {
var test Post
err := mapstructure.Decode(item, &test)
errs = errors.Join(errs, err)
log.Printf("Item: %#v\n", test)
}
log.Println("Inserting new item")
// you can use struct type - just make sure it has JSON tags
_, err = client.Create("posts_public", Post{
Field: "test_" + time.Now().Format(time.Stamp),
})
errs = errors.Join(errs, err)
// or you can use simple map[string]any
r, err := client.Create("posts_public", map[string]any{
"field": "test_" + time.Now().Format(time.Stamp),
})
errs = errors.Join(errs, err)
err = client.Delete("posts_public", r.ID)
errs = errors.Join(errs, err)
if errs != nil {
log.Fatal(errs)
}
}