Skip to content

Commit

Permalink
psdbconnect client
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrobenolt committed Feb 26, 2023
1 parent 9a28d3e commit a22f45c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
25 changes: 25 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package psdb

import (
"sync"

"github.com/planetscale/psdb/auth"
"github.com/planetscale/psdb/types/psdbconnect/v1/psdbconnectv1connect"
)

// Client is a PlanetScale Database client
type Client struct {
host string
auth *auth.Authorization

connectclientOnce sync.Once
connectclient psdbconnectv1connect.ConnectClient
}

// New creates a new Client with the provided Config
func New(cfg Config) *Client {
return &Client{
host: cfg.Host,
auth: auth.NewBasicAuth(cfg.User, cfg.Password),
}
}
20 changes: 20 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package psdb

import "net/url"

// Config is a PlanetScale connection configuration
type Config struct {
Host string
User string
Password string
}

func ConfigFromURL(rawURL string) Config {
var cfg Config
if u, err := url.Parse(rawURL); err == nil {
cfg.Host = u.Host
cfg.User = u.User.Username()
cfg.Password, _ = u.User.Password()
}
return cfg
}
27 changes: 27 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package psdb

import (
"reflect"
"testing"
)

func TestConfigFromURL(t *testing.T) {
cases := []struct {
in string
cfg Config
}{
{"mysql://foo:[email protected]/", Config{"example.com", "foo", "bar"}},
{"mysql://[email protected]/", Config{"example.com", "foo", ""}},
{"mysql://foo:[email protected]:9999", Config{"example.com:9999", "foo", "bar"}},
{"", Config{}},
}

for _, c := range cases {
t.Run(c.in, func(t *testing.T) {
cfg := ConfigFromURL(c.in)
if !reflect.DeepEqual(c.cfg, cfg) {
t.Errorf("expected %v, got %v", c.cfg, cfg)
}
})
}
}
34 changes: 34 additions & 0 deletions connectclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package psdb

import (
"context"

"github.com/bufbuild/connect-go"
coreclient "github.com/planetscale/psdb/core/client"
psdbconnectv1 "github.com/planetscale/psdb/types/psdbconnect/v1"
connectprotoconnect "github.com/planetscale/psdb/types/psdbconnect/v1/psdbconnectv1connect"
)

type (
TableCursor = psdbconnectv1.TableCursor
SyncStream = connect.ServerStreamForClient[psdbconnectv1.SyncResponse]
SyncRequest = psdbconnectv1.SyncRequest
)

const (
Primary = psdbconnectv1.TabletType_primary
Replica = psdbconnectv1.TabletType_replica
)

func (c *Client) initConnect() {
c.connectclient = coreclient.New(
c.host,
connectprotoconnect.NewConnectClient,
c.auth,
)
}

func (c *Client) Sync(ctx context.Context, r *SyncRequest) (*SyncStream, error) {
c.connectclientOnce.Do(c.initConnect)
return c.connectclient.Sync(ctx, connect.NewRequest(r))
}

0 comments on commit a22f45c

Please sign in to comment.