We are developing a library that manages the access to our Google Cloud Datastore project. Our datastore project consists out of multiple namespaces (one for development, one for testing, etc...). To access the stored data we use the Query builder provided by Google. We need a 'Kind Query' for a certain namespace but in their docs they explain
Queries of this type are implicitly restricted to the current namespace.
We tried changing the current namespace by using the NamespaceManager but since this isn't an Google App Engine application, that didn't work. Is it possible to change the current namespace?
You can (optionally) set a namespace on a RunQueryRequest
. For your example (a metadata query on kind), the Java code would be:
Query.Builder query = Query.newBuilder();
query.addKindBuilder().setName("__kind__");
query.addProjectionBuilder().setProperty(makePropertyReference("__key__"));
RunQueryRequest.Builder req = RunQueryRequest.newBuilder()
.setQuery(query);
req.getPartitionIdBuilder().setNamespace("my_namespace");
datastore.runQuery(req.build());
and in Python:
req = datastore.RunQueryRequest()
req.partition_id.namespace = 'my_namespace'
query = req.query
query.kind.add().name = '__kind__'
query.projection.add().property.name = '__key__'
datastore.run_query(req)