Documentation
¶
Overview ¶
Package dsquery provides helpers for composing and executing Google Cloud Datastore queries. It wraps datastore.Query objects with logical operators such as AND/OR and supports optional caching of results.
Index ¶
- func DSKeyMapMergeAnd(m map[string]*datastore.Key, keys []*datastore.Key) map[string]*datastore.Key
- func DSKeyMapMergeNot(m map[string]*datastore.Key, keys []*datastore.Key) map[string]*datastore.Key
- func ExtractMapStringKeysKey(m map[string]*datastore.Key) []*datastore.Key
- type And
- type Base
- type Cached
- type DatastoreClient
- type Ident
- type Not
- type Or
- type Query
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DSKeyMapMergeAnd ¶
Helper function to merge a set with an array, and produce a set of datastore keys.
func DSKeyMapMergeNot ¶
Helper function to remove keys from a set and produce a new set of datastore keys.
Types ¶
type And ¶
type And Base
AND Query
Example ¶
ExampleAnd demonstrates using And with subqueries.
ds := &staticDS{results: [][]*datastore.Key{
{datastore.NameKey("Fruit", "1", nil), datastore.NameKey("Fruit", "2", nil), datastore.NameKey("Fruit", "3", nil)},
{datastore.NameKey("Fruit", "2", nil), datastore.NameKey("Fruit", "3", nil), datastore.NameKey("Fruit", "4", nil)},
}}
q := &And{Queries: []*datastore.Query{
datastore.NewQuery("Fruit").FilterField("Color", "=", "Red"),
datastore.NewQuery("Fruit").FilterField("Producers", "=", "USA"),
}}
keys, _ := q.Query(ds, context.Background())
sort.Slice(keys, func(i, j int) bool { return keys[i].Name < keys[j].Name })
for _, k := range keys {
fmt.Println(k.Name)
}
Output: 2 3
type Base ¶
type Base struct {
// Data store queries to run
Queries []*datastore.Query
// Sub queries after that to run
SubQueries []Query
// Provided for your convenience when debugging
Name string
}
Base entity
type Cached ¶
type Cached struct {
StoredQuery Query
StoredResults []*datastore.Key
Name string
TTL time.Duration
Expiration time.Time
sync.RWMutex
}
An object that contains just a single query, provides thread safe caching
Example ¶
ExampleCached shows how Cached prevents a query from running more than once.
q := &countQuery{keys: []*datastore.Key{datastore.NameKey("Fruit", "1", nil)}}
cached := &Cached{StoredQuery: q}
_, _ = cached.Query(nil, context.Background())
_, _ = cached.Query(nil, context.Background())
fmt.Println(q.n)
Output: 1
type DatastoreClient ¶
type DatastoreClient interface {
// GetAll should perform `q` against the underlying datastore and return
// every matching key. Implementations may ignore `dst` as queries in
// this package are executed in keys-only mode.
GetAll(ctx context.Context, q *datastore.Query, dst interface{}) (keys []*datastore.Key, err error)
}
DatastoreClient exposes the datastore operations required by this package. The real *datastore.Client type already satisfies this interface. Custom or test implementations should execute the provided query and return all resulting keys. The `dst` parameter is kept for signature compatibility with datastore.Client.GetAll and may be nil.
type Ident ¶
An object that contains just a single query, provided for debugging or intentinally ordering queries in a particular way
type Or ¶
type Or Base
The OR query
Example ¶
ExampleOr demonstrates combining multiple queries with Or.
ds := &staticDS{results: [][]*datastore.Key{
{datastore.NameKey("Fruit", "1", nil), datastore.NameKey("Fruit", "2", nil)},
{datastore.NameKey("Fruit", "3", nil)},
}}
q := &Or{Queries: []*datastore.Query{
datastore.NewQuery("Fruit").FilterField("Color", "=", "Brown"),
datastore.NewQuery("Fruit").FilterField("Color", "=", "Orange"),
}}
keys, _ := q.Query(ds, context.Background())
sort.Slice(keys, func(i, j int) bool { return keys[i].Name < keys[j].Name })
for _, k := range keys {
fmt.Println(k.Name)
}
Output: 1 2 3