-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9a28d3e
commit a22f45c
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |