loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

lb4 repository tool error if datasource is using injection

Open clewisln opened this issue 4 years ago • 2 comments

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.

clewisln avatar Oct 15 '21 16:10 clewisln

Currently, the CLI does assume that either the "legacy" JSON or "current" TypeScript-style config is available:

  1. JSON-style expects *.datasource.config.json, where * is the filename of the equivalent DataSource TypeScript file.
  2. TypeScript-style expects a top-level config variable (export not 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.

achrinza avatar Oct 16 '21 08:10 achrinza

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.

clewisln avatar Oct 18 '21 13:10 clewisln