Page Contents

Synopsis

Adds a new Repository class (or multiple backed by the same datasource) to a LoopBack application with one single command.

lb4 repository [options] [<name>]

Options

--datasource : (Optional) name of a valid datasource already created in src/datasources

--model : (Optional) name of a valid model already created in src/models

--id : (Optional) name of the property serving as ID in the selected model. If you supply this value, the CLI will not try to infer this value from the selected model file.

--repositoryBaseClass : (Optional) (Default: DefaultCrudRepository) name of the base class the repository will inherit. If no value was supplied, DefaultCrudRepository will be used.

Configuration file

This generator supports a config file with the following format, see the Standard options below to see different ways you can supply this configuration file.

{
  "name": "repositoryNameToBeGenerated",
  "datasource": "validDataSourceName",
  "model": "validDModelName",
  "id": "anOptionalNameForID",
  "repositoryBaseClass": "validRepositoryBaseClass"
}

Notes

Service oriented datasources such as REST or SOAP are not considered valid in this context and will not be presented to you in the selection list.

There should be at least one valid (KeyValue or Persisted) data source and one model already created in their respective directories.

Standard options

-h, --help
Print the generator’s options and usage.
--skip-cache
Do not remember prompt answers. Default is false.
--skip-install
Do not automatically install dependencies. Default is false.
-c, --config
JSON file name or value to configure options
--format
Format generated code using npm run lint:fix

For example,

lb4 app --config config.json
lb4 app --config '{"name":"my-app"}'
cat config.json | lb4 app --config stdin
lb4 app --config stdin < config.json
lb4 app --config stdin << EOF
> {"name":"my-app"}
> EOF
-y, --yes
Skip all confirmation prompts with default or provided value

Arguments

<name> - Optional argument specifying the repository name to be generated. In case you select multiple models, the first model will take this argument for its repository file name.

Interactive Prompts

The tool will prompt you for:

  • Please select the datasource. (name) If the name of the datasource had been supplied from the command line, the prompt is skipped, otherwise it will present you the list of available datasources to select one. It will use this datasource to check what kind of repository it will generate.

  • Select the model(s) you want to generate a repository. (model) If the name of the model had been supplied from the command line with --model option and it is a valid model, then the prompt is skipped, otherwise it will present the error Error: No models found in the console.

    If no --model is supplied, then the it will present you with a valid list of models from src/models directory and you will be able to select one or multiple models. The tool will generate a repository for each of the selected models.

    NOTE: The tool will inspect each of the selected models and try to find the name of the property serving as ID for the model.

  • Please select the repository base class. (repository) if the name of a valid base class has been supplied from the command line, the prompt is skipped, otherwise it will present you a list of repositories. The default repository is inferred from the datasource type.

    Any repository in the src/repositories folder with the file format *.repository.base.ts will be added to the list too.

  • Please enter the name of the ID property for modelName. (id) If the CLI cannot find the corresponding ID property name for the model, it will prompt you to enter a name here. If you don’t specify any name, it will use id as the default one.

Output

Once all the prompts have been answered, the CLI will do the following for each of the selected models.

  • Create a Repository class as follows: /src/repositories/${modelName}.repository.ts
  • Update /src/repositories/index.ts to export the newly created Repository class.