firestore

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 Firestore 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 Firestore container type used in the module

func Run

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

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

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

// firestoreClient {
projectID := firestoreContainer.ProjectID()

conn, err := grpc.NewClient(firestoreContainer.URI(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithPerRPCCredentials(emulatorCreds{}))
if err != nil {
	log.Printf("failed to dial: %v", err)
	return
}

options := []option.ClientOption{option.WithGRPCConn(conn)}
client, err := firestore.NewClient(ctx, projectID, options...)
if err != nil {
	log.Printf("failed to create client: %v", err)
	return
}
defer client.Close()
// }

users := client.Collection("users")
docRef := users.Doc("alovelace")

type Person struct {
	Firstname string `json:"firstname"`
	Lastname  string `json:"lastname"`
}

data := Person{
	Firstname: "Ada",
	Lastname:  "Lovelace",
}
_, err = docRef.Create(ctx, data)
if err != nil {
	log.Printf("failed to create document: %v", err)
	return
}

docsnap, err := docRef.Get(ctx)
if err != nil {
	log.Printf("failed to get document: %v", err)
	return
}

var saved Person
if err := docsnap.DataTo(&saved); err != nil {
	log.Printf("failed to convert data: %v", err)
	return
}

fmt.Println(saved.Firstname, saved.Lastname)
Output:

Ada Lovelace
Example (DatastoreMode)
ctx := context.Background()

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

projectID := firestoreContainer.ProjectID()

conn, err := grpc.NewClient(firestoreContainer.URI(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithPerRPCCredentials(emulatorCreds{}))
if err != nil {
	log.Printf("failed to dial: %v", err)
	return
}

options := []option.ClientOption{option.WithGRPCConn(conn)}
client, err := datastore.NewClient(ctx, projectID, options...)
if err != nil {
	log.Printf("failed to create client: %v", err)
	return
}
defer client.Close()

userKey := datastore.NameKey("users", "alovelace", nil)

type Person struct {
	Firstname string `json:"firstname"`
	Lastname  string `json:"lastname"`
}

data := Person{
	Firstname: "Ada",
	Lastname:  "Lovelace",
}

_, err = client.Put(ctx, userKey, &data)
if err != nil {
	log.Printf("failed to create entity: %v", err)
	return
}

saved := Person{}
err = client.Get(ctx, userKey, &saved)
if err != nil {
	log.Printf("failed to get entity: %v", err)
	return
}

fmt.Println(saved.Firstname, saved.Lastname)
Output:

Ada Lovelace

func (*Container) ProjectID

func (c *Container) ProjectID() string

ProjectID returns the project ID of the Firestore container.

func (*Container) URI

func (c *Container) URI() string

URI returns the URI of the Firestore container.

type Option

type Option func(o *options) error

func WithDatastoreMode

func WithDatastoreMode() Option

WithDatastoreMode sets the firestore emulator to run in datastore mode. Requires a cloud-sdk image with version 465.0.0 or higher

func (Option) Customize

Customize is a NOOP. It's defined to satisfy the testcontainers.ContainerCustomizer interface.

Jump to

Keyboard shortcuts

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