tigris-client-go

module
v1.0.0-alpha.12 Latest Latest
Warning

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

Go to latest
Published: May 5, 2022 License: Apache-2.0

README

Tigris Golang client

Go Report Build Status Build Status codecov Go Reference

Installation

go get github.com/tigrisdata/tigris-client-go@latest

Getting started

For fully functional getting-started application, please check Tigris Starter Go application, or install the CLI and scaffold you project by running:

tigris scaffold go Company1 Project1 DbName1 Coll1 Coll2

Client API example

package main

import (
	"context"
	"fmt"

	"github.com/tigrisdata/tigris-client-go/config"
	"github.com/tigrisdata/tigris-client-go/filter"
	"github.com/tigrisdata/tigris-client-go/tigris"
	"github.com/tigrisdata/tigris-client-go/update"
)

type User struct {
	Id      int `tigris:"primary_key"`
	Name    string
	Balance int
}

func main() {
	ctx := context.TODO()

	// Config database and connection. Optional for local development.
	cfg := &config.Database{Driver: config.Driver{URL: "localhost:8081"}}

	db, err := tigris.OpenDatabase(ctx, cfg, "OrderDB", &User{})

	users := tigris.GetCollection[User](db)

	_, err = users.Insert(ctx, &User{Id: 1, Name: "Jane", Balance: 100},
		&User{Id: 2, Name: "John", Balance: 100})
	if err != nil {
		panic(err)
	}

	_, err = users.Update(ctx, filter.Eq("Id", "1"), update.Set("Balance", 200))
	if err != nil {
		panic(err)
	}

	it, err := users.Read(ctx, filter.Or(filter.Eq("Id", 1), filter.Eq("Id", 2)))
	if err != nil {
		panic(err)
	}

	var user User
	for it.Next(&user) {
		fmt.Printf("%+v\n", user)
	}

	if it.Err() != nil {
		panic(it.Err())
	}

	if _, err := users.Delete(ctx, filter.Eq("Id", "1")); err != nil {
		panic(err)
	}

	// When the closure returns no error, the changes from all operations
	// executed in it will be applied to the database.
	// Changes will be discarded when the closure returns an error.
	// In this example if Balance is lower than 100 transaction will be rolled back
	err = db.Tx(ctx, func(ctx context.Context, tx *tigris.Tx) error {
		c := tigris.GetTxCollection[User](tx)
		
		u, err := c.ReadOne(ctx, filter.Eq("Id", 1))
		if err != nil {
			return err
		}

		if u.Balance < 100 {
			return fmt.Errorf("low balance")
		}
		
		// ...
		// Same API as in non-transactional case can be used here
		return nil
	})
	if err != nil {
		panic(err)
	}
}

Integration API

Low level integration API

Development

sh scripts/install_build_deps.sh
sh scripts/install_test_deps.sh
make test

License

This software is licensed under the Apache 2.0.

Directories

Path Synopsis
api
Package api contains code which is autogenerated from the OpenAPI and Protobuf API specifications
Package api contains code which is autogenerated from the OpenAPI and Protobuf API specifications
client/v1/api
Package api provides primitives to interact with the openapi HTTP API.
Package api provides primitives to interact with the openapi HTTP API.
server/v1
Package api is a reverse proxy.
Package api is a reverse proxy.
Package config contains configuration related structures and functions
Package config contains configuration related structures and functions
Package driver provides access to the low level Tigris API.
Package driver provides access to the low level Tigris API.
Package filter provides methods to build logical filters.
Package filter provides methods to build logical filters.
Package mock contains autogenerated, test related code.
Package mock contains autogenerated, test related code.
api
Package api is a generated GoMock package.
Package api is a generated GoMock package.
Package projection provides helpers to construct "field projections".
Package projection provides helpers to construct "field projections".
Package schema provides methods to build JSON schema of the collections from user defined models.
Package schema provides methods to build JSON schema of the collections from user defined models.
Package test contains testing helpers
Package test contains testing helpers
Package tigris provides an interface for accessing Tigris data-platform.
Package tigris provides an interface for accessing Tigris data-platform.
Package update package provides a builder to construct update mutation of the Update API.
Package update package provides a builder to construct update mutation of the Update API.

Jump to

Keyboard shortcuts

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