Important:Before running this generator, you must create an application using the application generator.Then you must run the command from the root directory of the application.

Synopsis

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

  1. lb4 repository [options] [<name>]

Options

—datasource : (Optional) name of a valid datasource already created insrc/datasources

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

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

—repositoryBaseClass : (Optional)__(Default: DefaultCrudRepository) nameof 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 theStandard options below to see different ways you can supply this configurationfile.

  1. {
  2. "name": "repositoryNameToBeGenerated",
  3. "datasource": "validDataSourceName",
  4. "model": "validDModelName",
  5. "id": "anOptionalNameForID",
  6. "repositoryBaseClass": "validRepositoryBaseClass"
  7. }

Notes

Service oriented datasources such as REST or SOAP are not considered valid inthis 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 onemodel 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:fixFor example,
  1. lb4 app --config config.json
  2. lb4 app --config '{"name":"my-app"}'
  3. cat config.json | lb4 app --config stdin
  4. lb4 app --config stdin < config.json
  5. lb4 app --config stdin << EOF
  6. > {"name":"my-app"}
  7. > EOF
  • -y, —yes
  • Skip all confirmation prompts with default or provided value

Arguments

<name> - Optional argument specifyng the respository name to be generated. Incase you select multiple models, the first model will take this argument for itsrepository file name.

Interactive Prompts

The tool will prompt you for:

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

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

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

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

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

Any repository in the src/repository 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 theCLI cannot find the corresponding ID property name for the model, it willprompt you to enter a name here. If you don’t specify any name, it will useid as the default one.

Output

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

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