Skip to content

Commit

Permalink
feat: Introduce client object
Browse files Browse the repository at this point in the history
  • Loading branch information
efirs committed Jul 6, 2022
1 parent fe9f68b commit 9719ae2
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 3 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ type User struct {
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

// Connect to the Tigris server
client, err := tigris.NewClient(ctx, &config.Database{Driver: config.Driver{URL: "localhost:8081"}})
if err != nil {
panic(err)
}
defer client.Close()

// Connect to the Tigris backend, create the database and collection if they don't exist,
// Create the database and collection if they don't exist,
// otherwise update the schema of the collection if it already exists
db, err := tigris.OpenDatabase(ctx, &config.Database{Driver: config.Driver{URL: "localhost:8081"}},
"hello_db", &User{})
db, err := client.OpenDatabase(ctx, "hello_db", &User{})
if err != nil {
panic(err)
}
Expand Down
50 changes: 50 additions & 0 deletions tigris/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tigris

import (
"context"

"github.com/tigrisdata/tigris-client-go/config"
"github.com/tigrisdata/tigris-client-go/driver"
"github.com/tigrisdata/tigris-client-go/schema"
)

// Client responsible for connecting to the server and opening a database
type Client struct {
driver driver.Driver
config *config.Database
}

// NewClient creates a connection to the Tigris server
func NewClient(ctx context.Context, cfg *config.Database) (*Client, error) {
d, err := driver.NewDriver(ctx, &cfg.Driver)
if err != nil {
return nil, err
}

return &Client{driver: d, config: cfg}, nil
}

// Close terminates client connections and release resources
func (c *Client) Close() error {
return c.driver.Close()
}

// OpenDatabase initializes Database from given collection models.
// It creates Database if necessary.
// Creates and migrates schemas of the collections which constitutes the Database
func (c *Client) OpenDatabase(ctx context.Context, dbName string, model schema.Model, models ...schema.Model) (*Database, error) {
if getTxCtx(ctx) != nil {
return nil, ErrNotTransactional
}

return openDatabaseFromModels(ctx, c.driver, c.config, dbName, model, models...)
}

// DropDatabase deletes the database and all collections in it
func (c *Client) DropDatabase(ctx context.Context, dbName string) error {
if getTxCtx(ctx) != nil {
return ErrNotTransactional
}

return c.driver.DropDatabase(ctx, dbName)
}
84 changes: 84 additions & 0 deletions tigris/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package tigris

import (
"context"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
api "github.com/tigrisdata/tigris-client-go/api/server/v1"
"github.com/tigrisdata/tigris-client-go/config"
"github.com/tigrisdata/tigris-client-go/test"
)

func TestClient(t *testing.T) {
mc, cancel := test.SetupTests(t, 8)
defer cancel()

ctx, cancel1 := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel1()

cfg := &config.Database{Driver: config.Driver{URL: test.GRPCURL(8)}}
cfg.TLS = test.SetupTLS(t)

type Coll1 struct {
Key1 string `tigris:"primary_key"`
}

txCtx := &api.TransactionCtx{Id: "tx_id1", Origin: "origin_id1"}

mc.EXPECT().CreateDatabase(gomock.Any(),
pm(&api.CreateDatabaseRequest{
Db: "db1",
Options: &api.DatabaseOptions{},
})).Return(&api.CreateDatabaseResponse{}, nil)

mc.EXPECT().BeginTransaction(gomock.Any(),
pm(&api.BeginTransactionRequest{
Db: "db1",
Options: &api.TransactionOptions{},
})).Return(&api.BeginTransactionResponse{TxCtx: txCtx}, nil)

mc.EXPECT().CreateOrUpdateCollection(gomock.Any(),
pm(&api.CreateOrUpdateCollectionRequest{
Db: "db1", Collection: "coll_1",
Schema: []byte(`{"title":"coll_1","properties":{"Key1":{"type":"string"}},"primary_key":["Key1"]}`),
Options: &api.CollectionOptions{},
})).Do(func(ctx context.Context, r *api.CreateOrUpdateCollectionRequest) {
}).Return(&api.CreateOrUpdateCollectionResponse{}, nil)

mc.EXPECT().CommitTransaction(gomock.Any(),
pm(&api.CommitTransactionRequest{
Db: "db1",
})).Return(&api.CommitTransactionResponse{}, nil)

c, err := NewClient(ctx, cfg)
require.NoError(t, err)

db, err := c.OpenDatabase(ctx, "db1", &Coll1{})
require.NoError(t, err)
require.NotNil(t, db)

_, err = c.OpenDatabase(setTxCtx(ctx, &Tx{}), "db1", &Coll1{})
require.Error(t, err)

mc.EXPECT().DropDatabase(gomock.Any(),
pm(&api.DropDatabaseRequest{
Db: "db1",
Options: &api.DatabaseOptions{},
})).Return(&api.DropDatabaseResponse{}, nil)

err = c.DropDatabase(ctx, "db1")
require.NoError(t, err)

err = c.DropDatabase(setTxCtx(ctx, &Tx{}), "db1")
require.Error(t, err)

err = c.Close()
require.NoError(t, err)

cfg.URL = "http://invalid"
_, err = NewClient(ctx, cfg)
require.Error(t, err)
}

0 comments on commit 9719ae2

Please sign in to comment.