Multi tenant implementation

So far I have try everything, but only this code share here in this reply are working well.

Hi @woutercouvaras ,

Just would like to know: how could I get the mongodb connection or Parse.Query/Parse.Object by app?

Thanks so much.

Hi @xeoshow,

If I’m understanding you correctly (seeing as you’re talking about “apps”), check out this comment I made early on: Multi tenant implementation - #15 by Manuel

You’ll see in that example that one can configure the apps separately, and because you defined it, you can connect to the relevant app - e.g:

{
      appName: 'Customer One',
      customerPath: 'customer-one',
      databaseURI: 'mongodb://localhost:27017/customerOne',
      serverURL: `${config.parse.serverUrl}/customer-one`,
      publicServerURL: `${config.parse.serverUrl}/customer-one`,
      masterKey: 'masterCustomerOne',
      appId: 'Customer One App',
      cloud: `${__dirname}/${config.parse.cloudFunctions}`
  }

Please bear in mind that I’ve never used the SDK on the client side…I only ever used REST. When using rest, you can simply make all your requests to the relevant app url - e.g. ${config.parse.serverUrl}/customer-one/Classes/Page?where={"slug":"page-one"}. This “just works”.

Having said that, in the threads above, you’ll also see that I mention that Cloud Code will only run against your last defined app. This is sad and means that this solution is really viable if you need any kind of custom logic. This is why we went for the tenant id approach.

If I’ve misunderstood you, please explain in a bit more detail what you mean.

Hi @woutercouvaras ,

Thank you v much for the reply. I have seen your solution which is great, and just think if we could set the relationship for the app instance to the cloud function context, should can resolve the problem of “Cloud Code will only run against your last defined app”, since our different apps will have different databases, make things seems more complex.

BTW, we are also using the rest api.

If we expose the cloud function behind the rest api with the customerPath as part of the rest url (via express), is it possible a feasible solution for Cloud Code multi-tenancy?

Thanks so much again.

Sadly it won’t work. Take a look at these two links. It’s got to do with the way that Cloud Functions was originally designed - i.e. as a singleton, so only one instance of it can and does exist. It would be PERFECT if this was not the case, and it worked as you (and I) were hoping it would :smiley:

I hope this helps.

Understood.
IMHO, in the Cloud Code, mostly we will just use the Parse.Object and Parse.Query, if we could get the db connection session, and if there is a way to create the new Parse.Query or Parse.Object with the db connection session parameter, seems we could resolve most cases for CRUD?
And if there is a way to get the db connection session by appId, then seems feasible? And since the standard parse server rest api could support the multiple instances with the correct db session, I think there should be a way doing this…
Thanks a lot.

If you’re only using the Parse.Object and Parse.Query, then you probably don’t need cloud code at all. In that case, the apps setup that I mentioned here will give you db separation and it all works with the rest api.

I hope you get it all working the way you want. Good luck!

Hi @woutercouvaras ,

Thanks so much, we will need do some custom logic in the Cloud Code. For example, query table A and judge from the results for querying table B. Seems the standard rest api could not meet such requirements.

The core thing is: is there any way to get the db connection session and with that, we could create the Parse.Object and Parse.Query? I am just start investigating the source code, while did not find the related info yet …

From everything I’ve read (which includes snippets by the core team), it doesn’t look like what you’re after is possible. I can think of potential hacks that involve writing your own middleware that runs before parse to determine a few initial bits of state/data and then initialize the relevant app, based on that. You’d have to weight it up to figure out if the cost is worth it.

You could try to use an aggregation query to achieve what you want, or just do two separate queries. I think making two network requests is should be fine (from a user experience point of view), esp if the result set is going to be small (which, at the very least, the result from first lookup should be, right?).

We’ve opted to use the tenant-id as we get all the flexibility that Parse offers and get to continue using Cloud Code, which we need for all our custom logic. Sure the data is not split into separate dbs, but in our case, that’s okay.

1 Like

Hi @woutercouvaras,

If we are going to replace all the cloud functions with the express router rest api, how should I do with the sessionToken validation things? Also, the beforeFind/afterFind, etc triggers, will they work as expected?

Thanks a lot.

Hi @xeoshow,

I’m not sure I quite follow you, but I’ll have a go anyway.

Even though it’s possible to run multiple Parse Apps on a single parse server, if the application you’re building relies on Cloud Code, then it’s best to think of your Parse App as a single app. i.e. You cannot make use of the “multiple parse apps” functionality.

So, either you’ll run a parse instance per app that you’re building (or tenant that you create) or you’ll need to implement the multi tenancy as detailed in the thread above.

There is one other thing you could try, something that one of the core members mentioned (I think it was @davimacedo) in another thread somewhere, but something that I did not try, which is…

You could run your own middleware that does some checks before the parse app gets instantiated. i.e. Determine the tenant based on headers, referrer, etc, then instantiate the relevant Parse app, based on this info. I opted not to do this because of the sheer amount of work at a time when I don’t know if people will want what I’m building :slight_smile:

Sorry if I’ve misunderstood you.

1 Like

@woutercouvaras Thanks so much !

Just would like to ask another question: if not considering the limitation of hardware, what is the max number of app could be supported in the Parse Server?

Thanks a lot.

You could run into problems with a single app on a single server if the app was getting too much traffic.

All of that aside, I don’t actually know if there are limits on the number of apps.

1 Like