Documentation
¶
Overview ¶
Package datastore provides a zero-dependency Google Cloud Datastore client.
It uses only the Go standard library and makes direct REST API calls to the Datastore API. Authentication is handled via the GCP metadata server when running on GCP, or via Application Default Credentials.
Index ¶
- Variables
- func TestConfig(ctx context.Context, metadataURL, apiURL string) context.Context
- func WithConfig(ctx context.Context, cfg *Config) context.Context
- type Client
- func (c *Client) AllKeys(ctx context.Context, q *Query) ([]*Key, error)
- func (c *Client) AllocateIDs(ctx context.Context, keys []*Key) ([]*Key, error)
- func (*Client) Close() error
- func (c *Client) Count(ctx context.Context, q *Query) (int, error)
- func (c *Client) Delete(ctx context.Context, key *Key) error
- func (c *Client) DeleteAllByKind(ctx context.Context, kind string) error
- func (c *Client) DeleteMulti(ctx context.Context, keys []*Key) error
- func (c *Client) Get(ctx context.Context, key *Key, dst any) error
- func (c *Client) GetAll(ctx context.Context, query *Query, dst any) ([]*Key, error)
- func (c *Client) GetMulti(ctx context.Context, keys []*Key, dst any) error
- func (c *Client) Mutate(ctx context.Context, muts ...*Mutation) ([]*Key, error)
- func (c *Client) NewTransaction(ctx context.Context, opts ...TransactionOption) (*Transaction, error)
- func (c *Client) Put(ctx context.Context, key *Key, src any) (*Key, error)
- func (c *Client) PutMulti(ctx context.Context, keys []*Key, src any) ([]*Key, error)
- func (c *Client) Run(ctx context.Context, q *Query) *Iterator
- func (c *Client) RunInTransaction(ctx context.Context, f func(*Transaction) error, opts ...TransactionOption) (*Commit, error)
- type Commit
- type Config
- type Cursor
- type Iterator
- type Key
- type MultiError
- type Mutation
- type MutationOp
- type PendingKey
- type Query
- func (q *Query) Ancestor(ancestor *Key) *Query
- func (q *Query) Distinct() *Query
- func (q *Query) DistinctOn(fieldNames ...string) *Query
- func (q *Query) End(c Cursor) *Query
- func (q *Query) Filter(filterStr string, value any) *Query
- func (q *Query) FilterField(fieldName, operator string, value any) *Query
- func (q *Query) KeysOnly() *Query
- func (q *Query) Limit(limit int) *Query
- func (q *Query) Namespace(ns string) *Query
- func (q *Query) Offset(offset int) *Query
- func (q *Query) Order(fieldName string) *Query
- func (q *Query) Project(fieldNames ...string) *Query
- func (q *Query) Start(c Cursor) *Query
- type Transaction
- func (tx *Transaction) Commit() (*Commit, error)
- func (tx *Transaction) Delete(key *Key) error
- func (tx *Transaction) DeleteMulti(keys []*Key) error
- func (tx *Transaction) Get(key *Key, dst any) error
- func (tx *Transaction) GetMulti(keys []*Key, dst any) error
- func (tx *Transaction) Mutate(muts ...*Mutation) ([]*PendingKey, error)
- func (tx *Transaction) Put(key *Key, src any) (*Key, error)
- func (tx *Transaction) PutMulti(keys []*Key, src any) ([]*Key, error)
- func (tx *Transaction) Rollback() error
- type TransactionOption
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidEntityType is returned when functions like Get or Next are // passed a dst or src argument of invalid type. ErrInvalidEntityType = errors.New("datastore: invalid entity type") // ErrInvalidKey is returned when an invalid key is presented. ErrInvalidKey = errors.New("datastore: invalid key") // ErrNoSuchEntity is returned when no entity was found for a given key. ErrNoSuchEntity = errors.New("datastore: no such entity") // ErrConcurrentTransaction is returned when a transaction is used concurrently. ErrConcurrentTransaction = errors.New("datastore: concurrent transaction") // Done is returned by Iterator.Next when no more results are available. // This matches the official cloud.google.com/go/datastore API. // //nolint:revive,errname,staticcheck // Name must match official API (iterator.Done) Done = errors.New("datastore: no more items in iterator") )
Functions ¶
func TestConfig ¶ added in v0.6.0
TestConfig creates a context with test configuration for the given URLs. This is a helper for tests to easily configure mock servers. Use this with context.Background() or any existing context.
Example:
ctx := datastore.TestConfig(context.Background(), metadataURL, apiURL) client, err := datastore.NewClient(ctx, "test-project")
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a Google Cloud Datastore client.
func NewClient ¶
NewClient creates a new Datastore client. If projectID is empty, it will be fetched from the GCP metadata server. Configuration can be provided via WithConfig in the context.
func NewClientWithDatabase ¶
NewClientWithDatabase creates a new Datastore client with a specific database. Configuration can be provided via WithConfig in the context.
func NewMockClient ¶
NewMockClient creates a datastore client connected to mock servers with in-memory storage. This is a convenience wrapper for testing. Returns the client and a cleanup function that should be deferred.
func (*Client) AllKeys ¶
AllKeys returns all keys matching the query. This is a convenience method for KeysOnly queries.
func (*Client) AllocateIDs ¶
AllocateIDs allocates IDs for incomplete keys. Returns keys with IDs filled in. Complete keys are returned unchanged. API compatible with cloud.google.com/go/datastore.
func (*Client) Close ¶
Close closes the client connection. This is a no-op for ds9 since it uses a shared HTTP client with connection pooling, but is provided for API compatibility with cloud.google.com/go/datastore.
func (*Client) Count ¶
Count returns the number of entities matching the query. Deprecated: Use aggregation queries with RunAggregationQuery instead. API compatible with cloud.google.com/go/datastore.
func (*Client) DeleteAllByKind ¶
DeleteAllByKind deletes all entities of a given kind. This method queries for all keys and then deletes them in batches.
func (*Client) DeleteMulti ¶
DeleteMulti deletes multiple entities with their keys. Returns MultiError if any keys are invalid. This matches the API of cloud.google.com/go/datastore.
func (*Client) Get ¶
Get retrieves an entity by key and stores it in dst. dst must be a pointer to a struct. Returns ErrNoSuchEntity if the key is not found.
func (*Client) GetAll ¶
GetAll retrieves all entities matching the query and stores them in dst. dst must be a pointer to a slice of structs, or nil for KeysOnly queries. Returns the keys of the retrieved entities and any error. This matches the API of cloud.google.com/go/datastore.
func (*Client) GetMulti ¶
GetMulti retrieves multiple entities by their keys. dst must be a pointer to a slice of structs. Returns MultiError with ErrNoSuchEntity for missing keys, or other errors for specific items. This matches the API of cloud.google.com/go/datastore.
func (*Client) Mutate ¶
Mutate applies one or more mutations atomically. API compatible with cloud.google.com/go/datastore.
func (*Client) NewTransaction ¶
func (c *Client) NewTransaction(ctx context.Context, opts ...TransactionOption) (*Transaction, error)
NewTransaction creates a new transaction. The caller must call Commit or Rollback when done. API compatible with cloud.google.com/go/datastore.
func (*Client) Put ¶
Put stores an entity with the given key. src must be a struct or pointer to struct. Returns the key (useful for auto-generated IDs in the future).
func (*Client) PutMulti ¶
PutMulti stores multiple entities with their keys. keys and src must have the same length. Returns the keys (same as input) and MultiError if any operations failed. This matches the API of cloud.google.com/go/datastore.
func (*Client) Run ¶
Run executes the query and returns an iterator for the results. API compatible with cloud.google.com/go/datastore.
func (*Client) RunInTransaction ¶
func (c *Client) RunInTransaction(ctx context.Context, f func(*Transaction) error, opts ...TransactionOption) (*Commit, error)
RunInTransaction runs a function in a transaction. The function should use the transaction's Get and Put methods. API compatible with cloud.google.com/go/datastore.
type Commit ¶
type Commit struct{}
Commit represents the result of a committed transaction. This is provided for API compatibility with cloud.google.com/go/datastore.
type Config ¶ added in v0.6.0
type Config struct {
// AuthConfig is passed to the auth package for authentication.
// Can be nil to use defaults.
AuthConfig *auth.Config
// APIURL is the base URL for the Datastore API.
// Defaults to production if empty.
APIURL string
}
Config holds datastore client configuration.
type Cursor ¶
type Cursor string
Cursor represents a query cursor for pagination. API compatible with cloud.google.com/go/datastore.
func DecodeCursor ¶
DecodeCursor decodes a cursor string. API compatible with cloud.google.com/go/datastore.
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Iterator is an iterator for query results. API compatible with cloud.google.com/go/datastore.
type Key ¶
type Key struct {
Namespace string
Parent *Key // Parent key for hierarchical keys
Kind string
Name string // For string keys
ID int64 // For numeric keys
}
Key represents a Datastore key.
func DecodeKey ¶
DecodeKey decodes a key from its opaque representation. API compatible with cloud.google.com/go/datastore.
func IDKey ¶
IDKey creates a new key with a numeric ID. The parent parameter can be nil for top-level keys. This matches the API of cloud.google.com/go/datastore.
func IncompleteKey ¶
IncompleteKey creates a new incomplete key. The key will be completed (assigned an ID) when the entity is saved. API compatible with cloud.google.com/go/datastore.
func NameKey ¶
NameKey creates a new key with a string name. The parent parameter can be nil for top-level keys. This matches the API of cloud.google.com/go/datastore.
func (*Key) Encode ¶
Encode returns an opaque representation of the key. API compatible with cloud.google.com/go/datastore.
func (*Key) Equal ¶
Equal returns true if this key is equal to the other key. API compatible with cloud.google.com/go/datastore.
func (*Key) Incomplete ¶
Incomplete returns true if the key does not have an ID or Name. API compatible with cloud.google.com/go/datastore.
type MultiError ¶ added in v0.7.0
type MultiError []error
MultiError is returned by batch operations when there are errors with particular elements. Errors will be in a one-to-one correspondence with the input elements; successful elements will have a nil entry.
func (MultiError) Error ¶ added in v0.7.0
func (m MultiError) Error() string
type Mutation ¶
type Mutation struct {
// contains filtered or unexported fields
}
Mutation represents a pending datastore mutation.
func NewDelete ¶
NewDelete creates a delete mutation. API compatible with cloud.google.com/go/datastore.
func NewInsert ¶
NewInsert creates an insert mutation. API compatible with cloud.google.com/go/datastore.
type MutationOp ¶
type MutationOp string
MutationOp represents the type of mutation operation.
const ( // MutationInsert represents an insert operation. MutationInsert MutationOp = "insert" // MutationUpdate represents an update operation. MutationUpdate MutationOp = "update" // MutationUpsert represents an upsert operation. MutationUpsert MutationOp = "upsert" // MutationDelete represents a delete operation. MutationDelete MutationOp = "delete" )
type PendingKey ¶
type PendingKey struct {
// contains filtered or unexported fields
}
PendingKey represents a key that will be resolved after a transaction commit. API compatible with cloud.google.com/go/datastore.
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query represents a Datastore query.
func (*Query) Ancestor ¶
Ancestor sets an ancestor filter for the query. Only entities with the given ancestor will be returned. API compatible with cloud.google.com/go/datastore.
func (*Query) Distinct ¶
Distinct marks the query to return only distinct results. This is equivalent to DistinctOn with all projected fields. API compatible with cloud.google.com/go/datastore.
func (*Query) DistinctOn ¶
DistinctOn returns a query that removes duplicates based on the given field names. API compatible with cloud.google.com/go/datastore.
func (*Query) End ¶
End sets the ending cursor for the query results. API compatible with cloud.google.com/go/datastore.
func (*Query) Filter ¶
Filter adds a property filter to the query. The filterStr should be in the format "Property Operator" (e.g., "Count >", "Name ="). Deprecated: Use FilterField instead. API compatible with cloud.google.com/go/datastore.
func (*Query) FilterField ¶
FilterField adds a property filter to the query with explicit operator. API compatible with cloud.google.com/go/datastore.
func (*Query) Namespace ¶
Namespace sets the namespace for the query. API compatible with cloud.google.com/go/datastore.
func (*Query) Offset ¶
Offset sets the number of results to skip before returning. API compatible with cloud.google.com/go/datastore.
func (*Query) Order ¶
Order sets the order in which results are returned. Prefix the property name with "-" for descending order (e.g., "-Created"). API compatible with cloud.google.com/go/datastore.
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
Transaction represents a Datastore transaction. Note: This struct stores context for API compatibility with Google's official cloud.google.com/go/datastore library, which uses the same pattern.
func (*Transaction) Commit ¶
func (tx *Transaction) Commit() (*Commit, error)
Commit applies the transaction's mutations. API compatible with cloud.google.com/go/datastore.
func (*Transaction) Delete ¶
func (tx *Transaction) Delete(key *Key) error
Delete deletes an entity within the transaction. API compatible with cloud.google.com/go/datastore.
func (*Transaction) DeleteMulti ¶
func (tx *Transaction) DeleteMulti(keys []*Key) error
DeleteMulti deletes multiple entities within the transaction. API compatible with cloud.google.com/go/datastore.
func (*Transaction) Get ¶
func (tx *Transaction) Get(key *Key, dst any) error
Get retrieves an entity within the transaction. API compatible with cloud.google.com/go/datastore.
func (*Transaction) GetMulti ¶
func (tx *Transaction) GetMulti(keys []*Key, dst any) error
GetMulti retrieves multiple entities within the transaction. API compatible with cloud.google.com/go/datastore.
func (*Transaction) Mutate ¶
func (tx *Transaction) Mutate(muts ...*Mutation) ([]*PendingKey, error)
Mutate adds one or more mutations to the transaction. API compatible with cloud.google.com/go/datastore.
func (*Transaction) Put ¶
func (tx *Transaction) Put(key *Key, src any) (*Key, error)
Put stores an entity within the transaction.
func (*Transaction) PutMulti ¶
func (tx *Transaction) PutMulti(keys []*Key, src any) ([]*Key, error)
PutMulti stores multiple entities within the transaction. API compatible with cloud.google.com/go/datastore.
func (*Transaction) Rollback ¶
func (tx *Transaction) Rollback() error
Rollback abandons the transaction. API compatible with cloud.google.com/go/datastore.
type TransactionOption ¶
type TransactionOption interface {
// contains filtered or unexported methods
}
TransactionOption configures transaction behavior.
func MaxAttempts ¶
func MaxAttempts(n int) TransactionOption
MaxAttempts returns a TransactionOption that specifies the maximum number of times a transaction should be attempted before giving up.
func WithReadTime ¶
func WithReadTime(t time.Time) TransactionOption
WithReadTime returns a TransactionOption that sets a specific timestamp at which to read data, enabling reading from a particular snapshot in time.