Go - NewKv()

Creates a new key value store to get, set, and delete key value pairs.

import (
	"fmt"

	"github.com/nitrictech/go-sdk/nitric"
)

func main() {
	profiles, err := nitric.NewKv("profiles").Allow(nitric.KvStoreGet, nitric.KvStoreSet, nitric.KvStoreDelete)

	if err != nil {
		return
	}

	if err := nitric.Run(); err != nil {
		fmt.Println(err)
	}
}

Parameters

  • Name
    name
    Required
    Required
    Type
    string
    Description

    The unique name of this key value store within the service. Subsequent calls to kv with the same name will return the same object.

Access

All Nitric resources provide access permissions you can use to specify the level of access your service needs to the resource. See here for details Access Control documentation.

Available permissions:


nitric.KvStoreGet

This permission allows your service to get values from the key value store.


nitric.KvStoreSet

This permission allows your service to set key value pairs in the key value store.


nitric.KvStoreDelete

This permission allows your service to delete key value pairs in the key value store.


Examples

Create a key value store

import (
	"fmt"

	"github.com/nitrictech/go-sdk/nitric"
)

func main() {
	profiles, err := nitric.NewKv("profiles").Allow(nitric.KvStoreGet, nitric.KvStoreSet, nitric.KvStoreDelete)

	if err != nil {
		return
	}

	if err := nitric.Run(); err != nil {
		fmt.Println(err)
	}
}

Get value from a key value store

import (
	"context"
	"fmt"

	"github.com/nitrictech/go-sdk/nitric"
)

func main() {
	// Initialize the KV service
	profiles, err := nitric.NewKv("profiles").Allow(nitric.KvStoreGet)
	if err != nil {
		return
	}

	profile, err := profiles.Get(context.Background(), "profile-1a2b3c")
	// do something with the profile

	if err := nitric.Run(); err != nil {
		fmt.Println(err)
	}
}

Set a key in a key value store

import (
	"context"
	"fmt"

	"github.com/nitrictech/go-sdk/nitric"
)

func main() {
	// Initialize the KV service
	profiles, err := nitric.NewKv("profiles").Allow(nitric.KvStoreSet)
	if err != nil {
		return
	}

	err = profiles.Set(context.Background(), "profile-1a2b3c", map[string]interface{}{
		"name": "John Smith",
	})
	if err != nil {
		fmt.Println(err)
	}

	if err := nitric.Run(); err != nil {
		fmt.Println(err)
	}
}

Delete a key from a key value store

import (
	"context"
	"fmt"

	"github.com/nitrictech/go-sdk/nitric"
)

func main() {
	// Initialize the KV service
	profiles, err := nitric.NewKv("profiles").Allow(nitric.KvStoreDelete)
	if err != nil {
		return
	}

	err = profiles.Delete(context.Background(), "profile-1a2b3c")
	if err != nil {
		fmt.Println(err)
	}

	if err := nitric.Run(); err != nil {
		fmt.Println(err)
	}
}

Get all keys from a key value store

import (
	"context"
	"fmt"
	"io"
	"github.com/nitrictech/go-sdk"
)

func main() {
	// Initialize the KV service
	profiles, err := nitric.NewKv("profiles").Allow(nitric.KvStoreGet, nitric.KvStoreSet, nitric.KvStoreDelete)
	if err != nil {
		return
	}

	// Get all keys from a key value store
	for {
		key, err := keys.Recv()
		if err != nil {
			// check if the stream has ended
			if err == io.EOF {
				break
			}
			return nil, err
		}
		// do something with the key
		fmt.Printf("Key: %s\n", key)
	}

}