AppEngine NDB: How to apply ancestors correctly?

99 views Asked by At

I'm considering GAE + NDB for a new project. I'm still a bit confused about ancestors so I could use some advice on how to use them correctly.

My case: The application will handle production orders for factories, we plan to have different clients. In order to reduce administration tasks, we will have all clients use the same app and the same datastore (having a separate app + datastore would be a fine chinese wall between clients but a nightmare to admnistrate).

I need to implement a way to isolate data between clients. Client A should not be able to access any data of any other clients on this app.

So is it advisable to use ancestors in the datastore to separate data from the different clients? I think in this case I could have ancestor keys like ClientA-Products, ClientA-Orders, ClientB-Products, ClientB-Orders and so on. Or perhaps even have all transactions keyed by client: like ClientA, ClientB..?

Or is it better to have a property in each entity associating the entity with the Client? In this case "products" and "orders" entities both will have a property "Company" which would have to be filled in by the app on every write and included in every query.

Thanks in advance for your thoughts!

1

There are 1 answers

1
Dmytro Sadovnychyi On BEST ANSWER

Multitenancy will work perfect for your usecase.

from google.appengine.api import users

def namespace_manager_default_namespace_for_request():
    # assumes the user is logged in.
    return 'client_id'

Read more about it here.