lb4 repository tool error if datasource is using injection
I have a simple data source, whos config is loaded via injection:
export class MainDataSource extends juggler.DataSource
implements LifeCycleObserver {
static dataSourceName = 'Main';
constructor(
@config()
dsConfig: object,
) {
super(dsConfig);
}
}
Application.ts:
this.configure('datasources.Main').to(options.datasources?.Main || {});
When running the repository scaffolding tool the following error occurs:
(node:24064) UnhandledPromiseRejectionWarning: Error: Cannot load C:\lb4\src\datasources\main.datasource.config.json: ENOENT: no such file or directory, open 'C:\lb4\src\datasources\main.datasource.config.json'
I am temporarily suppressing the error by adding a main.datasource.config.json file in the source directory.
However, this scaffolding tool should not require a fully configured data source to generate simple repositories. If the configuration cannot be found, it should just emit a warning, but still allow the tool to run and generate a non-customized class using the basic boilerplate.
Currently, the CLI does assume that either the "legacy" JSON or "current" TypeScript-style config is available:
-
JSON-style expects
*.datasource.config.json, where*is the filename of the equivalent DataSource TypeScript file. -
TypeScript-style expects a top-level
configvariable (exportnot required) in the DataSource TypeScript file.
(Related pull request: https://github.com/loopbackio/loopback-next/pull/5000)
So adding a const config = {} in the DataSource TypeScript file should be suffice.
The OpenAPI generator also seems to assume a DataSource config being present, so we may also need to check that as well.
From the Repository generator template, the generated Repository class isn't quite large, and the DataSource class is a vital part (i.e. it will lead to errors during runtime if a generic DataSource is used). This may be problematic as some users may not be aware of the manual tweaking needed (even with a warning).
One method may be to generate generic templates which won't compile and with #TODOs to inform the user as early as possible on what's not set up, and to prevent the generic template from being executed.
Are other directories searched for the *.datasource.config.json file?
If the tool requires this configured, assuming for things like relations, then the flat file will be useful. However, keeping this file in the /src/datasources directory is not ideal.