You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We need to allow DI and Boot to support Environment Based Config. Example would be to boot config for a datasource based on the environment (dev, stage, prod, etc.). Also need support for loading production credentials from an external service / environment. If not, at the very least we need to have docs on this.
In your proposal, the configuration (loaded from JSON) is hard-coded in the generated (base) class. At the same time, we need to support different configurations for different environments (dev/test/production) and load configuration from environment variables (https://12factor.net).
It was proposed to parse process.env variables inside datasource files. I consider that as an anti-pattern, because it couples high-level datasources with low/system-level configuration code. It makes the code difficult to test, because tests would have to manipulate global process.env. Instead, we should find a way how to inject ENV variables in such way that the actual usage of process.env stays in the top-level index.ts or application.ts file only.
On a similar topic, @raymondfeng proposed a sort of a registry holding datasource configuration for all environments in the app-level Context. I don't think this is a good solution - consider the case where an application is deployed to different geo location and where each geo location requires different connection strings (to use a colocated database instance). In other words, a production config is not a single object, but a set of location-specific objects.
My conclusion is that we should decouple datasource configuration from datasource implementation class and leverage dependency injection to provide arbitrary runtime-specific configuration from the application level. IMO, this addressed both needs 1) have different datasource config depending on dev/test/prod environment 2) build datasource configuration dynamically, either from process.env or perhaps external config providers like https://www.consul.io.
A mock-up app:
classMyAppextendsRestApplication{asyncboot(){constdbConfig=// Figure out what config to use. @loopback/boot can provide// helpers to load config from ENV variablesthis.bind('datasources.db$config').to(dbConfig);// etc.}}
A mock-up datasource:
// default config is used in testsconstdefaultConfig=require('./db.datasource.json');classDbDataSourceextendsjuggler.DataSource{constructor(
@inject('datasources.db$config')config: DataSourceOptions=defaultConfig){super(config);}}
Acceptance Criteria
TBD
The text was updated successfully, but these errors were encountered:
Description / Steps to reproduce / Feature proposal
From #441
We need to allow DI and Boot to support Environment Based Config. Example would be to boot config for a datasource based on the environment (dev, stage, prod, etc.). Also need support for loading production credentials from an external service / environment. If not, at the very least we need to have docs on this.
Related Discussion
#441 (comment) #4
A mock-up app:
A mock-up datasource:
Acceptance Criteria
TBD
The text was updated successfully, but these errors were encountered: