Documentation
¶
Overview ¶
Package datastore provides shared utilities for Google Cloud Datastore backends.
Index ¶
- Constants
- func CombineIndexesToYAML(providers ...IndexProvider) string
- func IndexFileName(serviceName string) string
- func IndexesToYAML(serviceName string, indexes []DatastoreIndex) string
- func PrintIndexCommand(serviceName string) string
- func ValidateAndWriteIndexes(ctx context.Context, client *datastore.Client, namespace string, ...) error
- func ValidateIndexes(ctx context.Context, client *datastore.Client, namespace string, ...) error
- func ValidateMultipleServices(ctx context.Context, client *datastore.Client, namespace string, ...) error
- func ValidateWithMode(ctx context.Context, client *datastore.Client, namespace string, ...) error
- func WriteAllIndexFiles(outputDir string, providers ...IndexProvider) error
- func WriteCombinedIndexFile(path string, providers ...IndexProvider) error
- func WriteIndexFile(path string, serviceName string, indexes []DatastoreIndex) error
- type DatastoreIndex
- type IndexProperty
- type IndexProvider
- type IndexValidationError
- type ServiceOption
- type ServiceOptions
- type ValidationMode
Constants ¶
const CombinedIndexFileName = "index.yaml"
CombinedIndexFileName is the standard name expected by gcloud.
Variables ¶
This section is empty.
Functions ¶
func CombineIndexesToYAML ¶
func CombineIndexesToYAML(providers ...IndexProvider) string
CombineIndexesToYAML combines indexes from multiple providers into one YAML.
func IndexFileName ¶
IndexFileName returns the default index file name for a service.
func IndexesToYAML ¶
func IndexesToYAML(serviceName string, indexes []DatastoreIndex) string
IndexesToYAML converts indexes to YAML format suitable for index.yaml.
func PrintIndexCommand ¶
PrintIndexCommand returns the gcloud command to create indexes. Includes --project flag if DATASTORE_PROJECT_ID is set. Note: Datastore indexes are project-wide (not namespace-specific). The file is copied to /tmp/index.yaml because gcloud requires the file to be named index.yaml.
func ValidateAndWriteIndexes ¶
func ValidateAndWriteIndexes(ctx context.Context, client *datastore.Client, namespace string, provider IndexProvider) error
ValidateAndWriteIndexes validates indexes and writes the index file if validation fails. Returns a user-friendly error with deployment instructions.
func ValidateIndexes ¶
func ValidateIndexes(ctx context.Context, client *datastore.Client, namespace string, provider IndexProvider) error
ValidateIndexes checks if all required indexes exist by running test queries. Returns nil if all indexes exist, or an IndexValidationError with details about missing indexes.
func ValidateMultipleServices ¶
func ValidateMultipleServices(ctx context.Context, client *datastore.Client, namespace string, providers ...IndexProvider) error
ValidateMultipleServices validates indexes for multiple services and collects all errors. Returns a combined error message if any services have missing indexes.
func ValidateWithMode ¶
func ValidateWithMode(ctx context.Context, client *datastore.Client, namespace string, provider IndexProvider, mode ValidationMode) error
ValidateWithMode validates indexes with the specified mode. - ValidationNone: skips validation entirely - ValidationWarn: prints warning to stderr but returns nil - ValidationError: returns error if indexes are missing
func WriteAllIndexFiles ¶
func WriteAllIndexFiles(outputDir string, providers ...IndexProvider) error
WriteAllIndexFiles writes index files for multiple services.
func WriteCombinedIndexFile ¶
func WriteCombinedIndexFile(path string, providers ...IndexProvider) error
WriteCombinedIndexFile writes a combined index.yaml for multiple services.
func WriteIndexFile ¶
func WriteIndexFile(path string, serviceName string, indexes []DatastoreIndex) error
WriteIndexFile writes the indexes to a YAML file.
Types ¶
type DatastoreIndex ¶
type DatastoreIndex struct {
Kind string
Properties []IndexProperty
}
DatastoreIndex defines a composite index for Datastore.
type IndexProperty ¶
type IndexProperty struct {
Name string
Direction string // "asc", "desc", or "" for default ascending
}
IndexProperty defines a property in a composite index.
type IndexProvider ¶
type IndexProvider interface {
// ServiceName returns the name of the service (e.g., "likes", "tags").
ServiceName() string
// RequiredIndexes returns the composite indexes required by this service.
RequiredIndexes() []DatastoreIndex
// TestQueries returns queries that exercise each required index.
// These are used to validate that indexes exist.
TestQueries() []*datastore.Query
}
IndexProvider is implemented by Datastore services that require composite indexes.
type IndexValidationError ¶
type IndexValidationError struct {
ServiceName string
MissingIndexes []DatastoreIndex
Errors []error
}
IndexValidationError contains information about missing indexes.
func (*IndexValidationError) Error ¶
func (e *IndexValidationError) Error() string
type ServiceOption ¶
type ServiceOption func(*ServiceOptions)
ServiceOption is a functional option for configuring Datastore services.
func WithKindNames ¶
func WithKindNames(names map[string]string) ServiceOption
WithKindNames allows overriding default Datastore kind (table) names. Useful for multi-tenant scenarios or avoiding naming conflicts. Example: WithKindNames(map[string]string{"Tag": "MyApp_Tag"})
func WithValidation ¶
func WithValidation(ctx context.Context) ServiceOption
WithValidation enables index validation during service construction. Uses ValidationWarn mode by default (prints warning but doesn't fail).
func WithValidationMode ¶
func WithValidationMode(mode ValidationMode) ServiceOption
WithValidationMode sets the validation mode for index checking.
type ServiceOptions ¶
type ServiceOptions struct {
// ValidateCtx, if non-nil, triggers index validation during construction.
ValidateCtx context.Context
// ValidationMode controls behavior when indexes are missing.
// Default is ValidationWarn (print warning but don't fail).
ValidationMode ValidationMode
// KindNames allows overriding default kind (table) names.
// Keys are default kind names, values are custom names.
// Example: {"Tag": "MyApp_Tag", "EntityTag": "MyApp_EntityTag"}
KindNames map[string]string
}
ServiceOptions configures a Datastore service.
func ApplyOptions ¶
func ApplyOptions(options ...ServiceOption) *ServiceOptions
ApplyOptions applies functional options and returns the resulting config.
type ValidationMode ¶
type ValidationMode int
ValidationMode controls how missing indexes are handled.
const ( // ValidationNone skips index validation entirely. ValidationNone ValidationMode = iota // ValidationWarn prints a warning if indexes are missing but doesn't fail (default). ValidationWarn // ValidationError returns an error if indexes are missing. ValidationError )