driver

package
v1.0.0-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2022 License: Apache-2.0 Imports: 23 Imported by: 9

Documentation

Overview

Package driver provides access to the low level Tigris API. It abstracts underlying transport protocol.

Index

Examples

Constants

View Source
const (
	DefaultHTTPPort    = 443
	SetCookieHeaderKey = "Set-Cookie"
	CookieHeaderKey    = "Cookie"
)
View Source
const (
	GRPC  = "GRPC"
	HTTP  = "HTTP"
	HTTPS = "HTTPS"

	ApplicationID     = "TIGRIS_APPLICATION_ID"
	ApplicationSecret = "TIGRIS_APPLICATION_SECRET"
	Token             = "TIGRIS_TOKEN"
	Protocol          = "TIGRIS_PROTOCOL"

	Version   = "v1.0.0"
	UserAgent = "tigris-client-go/" + Version
)
View Source
const (
	DefaultGRPCPort = 443
)

Variables

View Source
var (
	DefaultProtocol = GRPC

	// TokenURLOverride Only used in tests to point auth to proper HTTP port in GRPC tests
	TokenURLOverride string
)

Functions

func GRPCError

func GRPCError(err error) error

func HTTPError

func HTTPError(err error, resp *http.Response) error

HTTPError parses HTTP error into TigrisError Returns nil, if HTTP status is OK

func PtrToBool

func PtrToBool(b *bool) bool

func PtrToBytes

func PtrToBytes(b *[]byte) []byte

func PtrToInt64

func PtrToInt64(b *int64) int64

func PtrToString

func PtrToString(s *string) string

Types

type Application

type Application api.Application

type CRUDWithOptions

type CRUDWithOptions interface {
	// contains filtered or unexported methods
}

type Collation

type Collation api.Collation

type CollectionOptions

type CollectionOptions api.CollectionOptions

type Database

type Database interface {
	// Insert array of documents into specified database and collection.
	Insert(ctx context.Context, collection string, docs []Document, options ...*InsertOptions) (*InsertResponse, error)

	// Replace array of documents into specified database and collection
	// Creates document if it doesn't exist.
	Replace(ctx context.Context, collection string, docs []Document, options ...*ReplaceOptions) (*ReplaceResponse, error)

	// Read documents from the collection matching the specified filter.
	Read(ctx context.Context, collection string, filter Filter, fields Projection, options ...*ReadOptions) (Iterator, error)

	//Search for documents in the collection matching specified query
	Search(ctx context.Context, collection string, request *SearchRequest) (SearchResultIterator, error)

	// Update documents in the collection matching the specified filter.
	Update(ctx context.Context, collection string, filter Filter, fields Update, options ...*UpdateOptions) (*UpdateResponse, error)

	// Delete documents from the collection matching specified filter.
	Delete(ctx context.Context, collection string, filter Filter, options ...*DeleteOptions) (*DeleteResponse, error)

	// CreateOrUpdateCollection either creates a collection or update the collection with the new schema
	// There are three categories of data types supported:
	//   Primitive: Strings, Numbers, Binary Data, Booleans, UUIDs, DateTime
	//   Complex: Arrays
	//   Objects: A container data type defined by the user that stores fields of primitive types,
	//   complex types as well as other Objects
	//
	//  The data types are derived from the types defined in the JSON schema specification
	//  with extensions that enable support for richer semantics.
	//  As an example, the string is defined like this,
	//   {
	//     "name": {
	//       "type": "string"
	//     }
	//   }
	// More detailed information here: https://docs.tigrisdata.com/datamodels/types
	CreateOrUpdateCollection(ctx context.Context, collection string, schema Schema, options ...*CollectionOptions) error

	// DropCollection deletes the collection and all documents it contains.
	DropCollection(ctx context.Context, collection string, options ...*CollectionOptions) error

	// ListCollections lists collections in the database.
	ListCollections(ctx context.Context, options ...*CollectionOptions) ([]string, error)

	// DescribeCollection returns metadata of the collection in the database
	DescribeCollection(ctx context.Context, collection string, options ...*CollectionOptions) (*DescribeCollectionResponse, error)

	Publish(ctx context.Context, collection string, docs []Message, options ...*PublishOptions) (*PublishResponse, error)

	Subscribe(ctx context.Context, collection string, filter Filter, options ...*SubscribeOptions) (Iterator, error)
}

Database is the interface that encapsulates the CRUD portions of the transaction API.

type DatabaseOptions

type DatabaseOptions api.DatabaseOptions

type DeleteOptions

type DeleteOptions api.DeleteRequestOptions

type DeleteResponse

type DeleteResponse api.DeleteResponse

type DescribeCollectionResponse

type DescribeCollectionResponse api.DescribeCollectionResponse

type DescribeDatabaseResponse

type DescribeDatabaseResponse api.DescribeDatabaseResponse

type Document

type Document json.RawMessage

type Driver

type Driver interface {
	// Info returns server information
	Info(ctx context.Context) (*InfoResponse, error)

	// UseDatabase returns and interface for collections and documents management
	// of the database
	UseDatabase(name string) Database

	// ListDatabases in the current namespace
	ListDatabases(ctx context.Context) ([]string, error)
	// DescribeDatabase returns database metadata
	DescribeDatabase(ctx context.Context, db string) (*DescribeDatabaseResponse, error)

	// CreateDatabase creates new database
	CreateDatabase(ctx context.Context, db string, options ...*DatabaseOptions) error
	// DropDatabase deletes the database and all collections it contains
	DropDatabase(ctx context.Context, db string, options ...*DatabaseOptions) error

	// BeginTx starts new transaction
	BeginTx(ctx context.Context, db string, options ...*TxOptions) (Tx, error)

	// Close releases resources of the driver
	Close() error
}

Driver implements Tigris API

Example
ctx := context.TODO()

c, _ := NewDriver(ctx, &config.Driver{URL: "localhost"})

_ = c.CreateDatabase(ctx, "db1", &DatabaseOptions{})

db := c.UseDatabase("db1")

_ = db.CreateOrUpdateCollection(ctx, "coll1",
	Schema(`{ "properties": { "F1": { "type": "string" }, "F2": { "type": "string" } }, "primary_key": ["F1"] }`))

_, _ = db.Insert(ctx, "c1", []Document{Document(`{"F1":"V1"}`)})

it, _ := db.Read(ctx, "c1", Filter(`{"F1":"V1"}`), Projection(`{}`))

var doc Document
for it.Next(&doc) {
	fmt.Printf("doc: %v\n", doc)
}

_ = it.Err()

_, _ = db.Delete(ctx, "c1", Filter(`{"F1":"V1"}`))

tx, _ := c.BeginTx(ctx, "db1", nil)
defer func() { _ = tx.Rollback(ctx) }()

_, _ = tx.Insert(ctx, "c1", []Document{Document(`{"F1":"V1"}`)})

it, _ = tx.Read(ctx, "c1", Filter(`{"F1":"V1"}`), Projection("{}"))

for it.Next(&doc) {
	fmt.Printf("doc: %v\n", doc)
}

err := it.Err()
var e Error
if errors.As(err, &e) && e.Code == api.Code_ALREADY_EXISTS {
	// handle already exists error
}

_, _ = tx.Update(ctx, "c1", Filter(`{"F1":"V1"}`),
	Update(`{"$set" : { "F2" : "V2"}}`), &UpdateOptions{})

_, _ = tx.Delete(ctx, "c1", Filter(`{"F1":"V1"}`), &DeleteOptions{})

_ = tx.Commit(ctx)

_ = c.DropDatabase(ctx, "db1", &DatabaseOptions{})
Output:

func NewDriver

func NewDriver(ctx context.Context, cfg *config.Driver) (Driver, error)

NewDriver connect to the Tigris instance at the specified URL. URL should be in the form: {hostname}:{port}

type Error

type Error struct {
	*api.TigrisError
}

func (*Error) As

func (e *Error) As(i any) bool

As converts driver.Error the error which implements AsTigrisError interface

type Facet

type Facet json.RawMessage

type Filter

type Filter json.RawMessage

type InfoResponse

type InfoResponse api.GetInfoResponse

type InsertOptions

type InsertOptions api.InsertRequestOptions

type InsertResponse

type InsertResponse api.InsertResponse

type Iterator

type Iterator interface {
	Next(d *Document) bool
	Err() error
	Close()
}

type Management

type Management interface {
	CreateApplication(ctx context.Context, name string, description string) (*Application, error)
	DeleteApplication(ctx context.Context, id string) error
	UpdateApplication(ctx context.Context, id string, name string, description string) (*Application, error)
	ListApplications(ctx context.Context) ([]*Application, error)
	RotateApplicationSecret(ctx context.Context, id string) (*Application, error)
	GetAccessToken(ctx context.Context, applicationID string, applicationSecret string, refreshToken string) (*TokenResponse, error)

	Close() error
}

Management declares Tigris Management APIs

func NewManagement

func NewManagement(ctx context.Context, cfg *config.Driver) (Management, error)

NewManagement instantiates authentication API client

type Message

type Message json.RawMessage

type Projection

type Projection json.RawMessage

type PublishOptions

type PublishOptions api.PublishRequestOptions

type PublishResponse

type PublishResponse api.PublishResponse

type ReadOptions

type ReadOptions api.ReadRequestOptions

type ReplaceOptions

type ReplaceOptions api.ReplaceRequestOptions

type ReplaceResponse

type ReplaceResponse api.ReplaceResponse

type Schema

type Schema json.RawMessage

type SearchRequest

type SearchRequest struct {
	Q             string
	SearchFields  []string
	Filter        Filter
	Facet         Facet
	Sort          SortOrder
	IncludeFields []string
	ExcludeFields []string
	Page          int32
	PageSize      int32
}

type SearchResponse

type SearchResponse *api.SearchResponse

type SearchResultIterator

type SearchResultIterator interface {
	Next(r *SearchResponse) bool
	Err() error
	Close()
}

type SortOrder

type SortOrder json.RawMessage

type SubscribeOptions

type SubscribeOptions api.SubscribeRequestOptions

type SubscribeResponse

type SubscribeResponse api.SubscribeResponse

type TokenResponse

type TokenResponse api.GetAccessTokenResponse

type Tx

type Tx interface {
	// Commit all the modification of the transaction
	Commit(ctx context.Context) error

	// Rollback discard all the modification made by the transaction
	Rollback(ctx context.Context) error

	Database
}

Tx object is used to atomically modify documents. This object is returned by BeginTx

type TxOptions

type TxOptions api.TransactionOptions

type Update

type Update json.RawMessage

type UpdateOptions

type UpdateOptions api.UpdateRequestOptions

type UpdateResponse

type UpdateResponse api.UpdateResponse

type WriteOptions

type WriteOptions api.WriteOptions

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL