Skip to main content

Quickstart: Go

The following guide will get you up and running with Tigris. It covers data modeling, creating database and collection, writing queries to read and write data, and performing search all without touching any infrastructure.

Prerequisites

All you'll need is any computer running macOS or Linux, with Golang 1.18 or newer installed.

Install Tigris CLI

Use the command below to install the CLI on macOS

brew install tigrisdata/tigris/tigris-cli

Alternative ways of installation can be found here.

Sign up for Tigris Cloud

You will need to have a Tigris Cloud account to complete this quickstart. If you don't already have an account, you can sign up for Tigris Cloud.

Login and set up the application credentials

Login to your Tigris Cloud account

tigris login

The next step is to create the project for the application we will be building

tigris create project quickstart

Now we can generate credentials to be used in this quickstart

tigris create app_key default "default key" -p quickstart

Once the command is executed, it would create the application credentials (clientID and clientSecret) and print it on the screen. Make a note of id and secret from the output.

Output
{
"id": "your_client_id_here",
"name": "default",
"description": "default key",
"secret": "your_client_secret_here",
"created_at": 1666248043000,
"created_by": "google-oauth2|xxx",
"project": "quickstart"
}

Quickstart

Create and initialize project directory

mkdir tigris_quickstart
cd tigris_quickstart
go mod init

Code

Create main.go file with the following content:

main.go
package main

import (
"context"
"fmt"
"time"

"github.com/tigrisdata/tigris-client-go/fields"
"github.com/tigrisdata/tigris-client-go/filter"
"github.com/tigrisdata/tigris-client-go/search"
"github.com/tigrisdata/tigris-client-go/tigris"
)

// User is a declaration of the model for the collection
type User struct {
tigris.Model
Name string `tigris:"searchIndex"`
Balance float64
}

func main() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

// Connect to the Tigris backend, create the database and collection if they don't exist,
// otherwise update the schema of the collection if it already exists
cfg := &tigris.Config{
URL: "your_tigris_uri",
ClientID: "your_client_id_here",
ClientSecret: "your_client_secret_here",
Project: "quickstart",
}
db, err := tigris.OpenDatabase(ctx, cfg, &User{})
if err != nil {
panic(err)
}

// Get the collection object, all the CRUD operations on the collection will be performed
// through this collection object
users := tigris.GetCollection[User](db)

// Insert
userJ := User{Name: "Jania McGrory", Balance: 6045.7}
userB := User{Name: "Bunny Instone", Balance: 2948.87}
users.Insert(ctx, &userJ, &userB)

// Read
var user *User
user, err = users.ReadOne(ctx, filter.Eq("ID", userJ.ID)) // find user with primary key field
if err != nil {
panic(err)
}
fmt.Printf("Name: %v, Balance: %v\n", user.Name, user.Balance)

user, err = users.ReadOne(ctx, filter.Eq("Name", "Jania McGrory")) // find user with Name Jania McGrory
if err != nil {
panic(err)
}
fmt.Printf("Name: %v, Balance: %v\n", user.Name, user.Balance)

// Update - update user's name
users.Update(ctx, filter.Eq("ID", userJ.ID), fields.Set("Name", "Jania McGrover"))

// Transaction - transfer balance between users
db.Tx(ctx, func(txCtx context.Context) error {
// update their balance
if _, err = users.Update(txCtx, filter.Eq("ID", userJ.ID),
fields.Set("Balance", userJ.Balance-100)); err != nil {
return err
}
if _, err = users.Update(txCtx, filter.Eq("ID", userB.ID),
fields.Set("Balance", userB.Balance+100)); err != nil {
return err
}

return nil
})

// Search for users with `Name` like "bunny" and `Balance` greater than 500
searchRequest := search.NewRequestBuilder().
WithQuery("bunny").
WithSearchFields("Name").
WithFilter(filter.Gt("Balance", 500)).
Build()
iterator, err := users.Search(ctx, searchRequest)
if err != nil {
panic(err)
}

var result search.Result[User]
for iterator.Next(&result) {
for _, v := range result.Hits {
fmt.Printf("Name: %v, Balance: %v\n", v.Document.Name, v.Document.Balance)
}
}

// Delete - delete users with IDs 1 or 2
users.Delete(ctx, filter.Or(filter.Eq("ID", userJ.ID), filter.Eq("ID", userB.ID)))
}

Build and run

go mod tidy
go build .
./tigris_quickstart

Output

Name: Jania McGrory, Balance: 6045.7
Name: Jania McGrory, Balance: 6045.7
Name: Bunny Instone, Balance: 3048.87

Where to go from here?

🚀 Signup for Tigris Cloud - and start building your next application with it. We are here to help you get started. Join our Discord community to ask any questions you might have.