parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Expose Parse Server config in Cloud Code

Open mtrezza opened this issue 3 years ago • 12 comments
trafficstars

New Feature / Enhancement Checklist

Current Limitation

Customization is a key characteristic of Parse Server. There have been scenarios in the past in which developers need to access adapters / controllers of Parse Server, which is done via the server config. The suggested way to access the config is:

const Config = require('../node_modules/parse-server/lib/Config');

That is not a stable reference and may break if server files are refactored.

See:

  • https://github.com/parse-community/parse-server/issues/1023#issuecomment-839870413
  • https://github.com/parse-community/parse-server/issues/1859#issuecomment-853847607

Feature / Enhancement Description

Expose the server config via an official method, for example:

const Parse = require('parse/node');
const Config = Parse.config;

// or considering multitenency:
const Config = Parse.getConfig(Parse.applicationId);

Example Use Case

const config = Parse.config;
const url = config.filesController.adapter.getFileLocation(config, filename);

Alternatives / Workarounds

n/a

3rd Party References

n/a

mtrezza avatar Mar 20 '22 19:03 mtrezza

Thanks for opening this issue!

  • 🎉 We are excited about your ideas for improvement!

@mtrezza i can suggest another implementation. What about just adding a config key under the request object used in all triggers? A lot easier to implement and follow the internal architecture of the parse server. Currently, the config is transferred through function args, not by a method from the Parse singleton :)

Moumouls avatar Jun 24 '22 08:06 Moumouls

magic will happen here: https://github.com/parse-community/parse-server/blob/42c954318926823446326c95188b844e19954711/src/triggers.js#L248

Moumouls avatar Jun 24 '22 08:06 Moumouls

That means config would only be accessible in a request. If config is static per Parse instance and doesn't change per request, why would you tie it to a request? And how would you access config outside of a request?

mtrezza avatar Jun 24 '22 14:06 mtrezza

If config is static per Parse instance and doesn't change per request

For DX purposes. Parse Server internally heavily use dependency injection by passing config during request execution. This is smart, easier for testing purposes and there is no reason to not expose the config through the request object. Also in a multi-tenancy config config/request makes sense.

NOTE: for example on the graphql API the full Parse Server context, with config and auth is available in the GraphQL resolver context arg

And how would you access config outside of a request?

Not supported in my PR, it could be an additional implementation. But not currently motivated to look into it. I've not seen this use case on my side since I use Parse Server.

So it sounds that const Config = Parse.getConfig(Parse.applicationId); is still up for grabs.

Moumouls avatar Jun 24 '22 14:06 Moumouls

What's the point of adding code to every request route and add tests for each individual route, if exposing the config via Parse is a solution that requires less code, less tests and covers even more use cases? This seems to be blowing up the code unnecessarily?

Not supported in my PR, it could be an additional implementation.

Why would that be an additional implementation, wouldn't that make your PR obsolete?

mtrezza avatar Jun 25 '22 18:06 mtrezza

@mtrezza I suggested a quick implementation without any tradeoff. This implementation follow dependency injection strategy well know to facilitate tests (like unit test on cloud function etc...) and also flexibility.

Feel free to send a PR for the Parse.Config strategy.

My PR will not be outdated or useless. Some properties of the config are already used in each request. There is no reason to not extend the request and allow developers to access to the full config through the request interface.

Moumouls avatar Jun 25 '22 23:06 Moumouls

My PR will not be outdated or useless.

How so? If config is accessible via Parse, why would it be useful to provide the config additionally in a request?

mtrezza avatar Jun 26 '22 09:06 mtrezza

why would it be useful to provide the config additionally in a request?

Because dependency injection is another way to consume data. I agree that Parse.Config is also useful but it's also another way to consume the config through a singleton. These 2 solutions can easily coexist and make sense.

Dependency injection strategy is easier also to use in a multi-tenant app. User will not need to care about which config to get in his cloud function/hooks.

Moumouls avatar Jun 26 '22 11:06 Moumouls

These 2 solutions can easily coexist and make sense.

Why does it make sense for them to co-exist?

in a multi-tenant app

Does Parse Server currently support multi-tenancy?

mtrezza avatar Jun 26 '22 20:06 mtrezza

Why does it make sense for them to co-exist?

Sure, one use dependency injection strategy with it's own benefits and the other use the singleton/import strategy with also it's own benefits

Does Parse Server currently support multi-tenancy?

It seems that some users started to succeed to use Parse in multi tenancy config, so it make sense to still implement code "multi tenancy friendly" (ie: https://github.com/parse-community/parse-server/pull/7951)

Moumouls avatar Jun 27 '22 07:06 Moumouls

It seems that some users started to succeed to use Parse in multi tenancy

We don't consider unofficial / hacked-in features. For Parse Server to support multi-tenancy more internal restructuring is needed. Just like there is Parse.applicationId, there can be Parse.config. If multi-tenancy ever becomes an official feature, both (and more) need to be reconsidered. Multi-tenancy is an issue that is discussed since Parse Server was open sourced and never made it so far. There have also been contrary efforts to remove the app ID in order to simplify the codebase. Both is on the table, neither can reasonably influence our current decision making.

So far I don't see any benefits for config as part of a request; but I do see downsides, as it requires more code, more maintenance and more tests. We want to reduce code. In every request, the Parse object is accessible and can provide a single path and logic to obtain the config.

mtrezza avatar Jun 27 '22 11:06 mtrezza

I think there is a naming clash between this and Parse.Config, which could lead to some confusion:

https://docs.parseplatform.org/js/guide/#config

Perhaps we could have Parse.Server (available in cloud code only) which is an alias for Config.get(appId)

dblythy avatar Oct 17 '22 01:10 dblythy

🎉 This change has been released in version 5.3.0-alpha.32

parseplatformorg avatar Oct 29 '22 17:10 parseplatformorg

🎉 This change has been released in version 5.4.0-beta.1

parseplatformorg avatar Oct 29 '22 20:10 parseplatformorg

🎉 This change has been released in version 5.4.0-alpha.1

parseplatformorg avatar Oct 31 '22 14:10 parseplatformorg

🎉 This change has been released in version 5.4.0

parseplatformorg avatar Nov 19 '22 03:11 parseplatformorg

🎉 This change has been released in version 5.4.0

parseplatformorg avatar Nov 19 '22 11:11 parseplatformorg