Are datastore indexes same across multiple namespa

2019-08-01 10:59发布

问题:

I'm working on developing a multi-tenant SaaS system and I'm storing all the data in Datastore. I have a separate namespace for every client, but the same set of "kinds" in all namespaces.

This is my question: If I build custom index for one entity kind, will this index be served across all namespaces? or should I specify the namespace also somewhere?

This is one of my custom indices:

- kind: loginTrack  
  ancestor: no
  properties:
  - name: logDate
  - name: username
  - name: timeStamp

I deployed this as a custom index in Datastore, but I'm not sure if it works for all namespaces.

How does Datastore handle developer-supplied-index?

回答1:

The index itself, as you observed, has no room for namespace information, it operates the same way across all namespaces inside an app's domain.

What's different is the datastore partition mapped to each namespace. From Multitenancy and partitioned data (emphasis mine):

Cloud Datastore uses partitions to silo data for each tenant. The combination of a project ID and a namespace ID forms a partition ID, which identifies each partition. An entity belongs to a single partition, and queries are scoped to a single partition.

But note that operating on a certain tenant's data is your app's responsability: all operations related to a certain tenant must be executed in that tenant's partition/namespace context (i.e. the namespace must be specified as argument to the datastore-related calls).

For the particular case of using the (shared) index in a tenant's domain you have to specify the tenant's namespace when building the query. Otherwise you won't get the results from that tenant's partition.