forked from supabase-community/supabase-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
78 lines (64 loc) · 1.69 KB
/
client.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
67
68
69
70
71
72
73
74
75
76
77
78
package supabase
import (
"errors"
storage_go "github.com/supabase-community/storage-go"
"github.com/supabase/postgrest-go"
)
const (
REST_URL = "/rest/v1"
STORGAGE_URL = "/storage/v1"
)
type Client struct {
rest *postgrest.Client
Storage *storage_go.Client
}
type RestOptions struct {
Schema string
}
type ClientOptions struct {
Headers map[string]string
Db *RestOptions
}
// NewClient creates a new Supabase client.
// url is the Supabase URL.
// key is the Supabase API key.
// options is the Supabase client options.
func NewClient(url, key string, options *ClientOptions) (*Client, error) {
if url == "" || key == "" {
return nil, errors.New("url and key are required")
}
headers := map[string]string{
"Authorization": "Bearer " + key,
"apikey": key,
}
if options != nil && options.Headers != nil {
for k, v := range options.Headers {
headers[k] = v
}
}
var schema string
if options != nil && options.Db != nil && options.Db.Schema != "" {
schema = options.Db.Schema
} else {
schema = "public"
}
if options != nil && options.Headers != nil {
for k, v := range options.Headers {
headers[k] = v
}
}
client := &Client{}
client.rest = postgrest.NewClient(url+REST_URL, schema, headers)
client.Storage = storage_go.NewClient(url+STORGAGE_URL, key, headers)
return client, nil
}
// Wrap postgrest From method
// From returns a QueryBuilder for the specified table.
func (c *Client) From(table string) *postgrest.QueryBuilder {
return c.rest.From(table)
}
// Wrap postgrest Rpc method
// Rpc returns a string for the specified function.
func (c *Client) Rpc(name, count string, rpcBody interface{}) string {
return c.rest.Rpc(name, count, rpcBody)
}