datastore

package
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	// DefaultProjectID is the default project ID for the Datastore container.
	DefaultProjectID = "test-project"
)

Variables

View Source
var WithProjectID = shared.WithProjectID

WithProjectID re-exports the common GCloud WithProjectID option

Functions

This section is empty.

Types

type Container

type Container struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

Container represents the Datastore container type used in the module

func Run

Run creates an instance of the Datastore GCloud container type. The URI uses the empty string as the protocol.

Example
// runDatastoreContainer {
ctx := context.Background()

datastoreContainer, err := tcdatastore.Run(
	ctx,
	"gcr.io/google.com/cloudsdktool/cloud-sdk:367.0.0-emulators",
	tcdatastore.WithProjectID("datastore-project"),
)
defer func() {
	if err := testcontainers.TerminateContainer(datastoreContainer); err != nil {
		log.Printf("failed to terminate container: %s", err)
	}
}()
if err != nil {
	log.Printf("failed to run container: %v", err)
	return
}
// }

// datastoreClient {
projectID := datastoreContainer.ProjectID()

options := []option.ClientOption{
	option.WithEndpoint(datastoreContainer.URI()),
	option.WithoutAuthentication(),
	option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
}

dsClient, err := datastore.NewClient(ctx, projectID, options...)
if err != nil {
	log.Printf("failed to create client: %v", err)
	return
}
defer dsClient.Close()
// }

type Task struct {
	Description string
}

k := datastore.NameKey("Task", "sample", nil)
data := Task{
	Description: "my description",
}
_, err = dsClient.Put(ctx, k, &data)
if err != nil {
	log.Printf("failed to put data: %v", err)
	return
}

saved := Task{}
err = dsClient.Get(ctx, k, &saved)
if err != nil {
	log.Printf("failed to get data: %v", err)
	return
}

fmt.Println(saved.Description)
Output:

my description

func (*Container) ProjectID

func (c *Container) ProjectID() string

ProjectID returns the project ID of the Datastore container.

func (*Container) URI

func (c *Container) URI() string

URI returns the URI of the Datastore container.

type Option

type Option = shared.Option

Option aliases the common GCloud option type

Jump to

Keyboard shortcuts

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