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 new between existing source and target models in a LoopBackapplication.

  • -h, —help: Print the generator’s options and usage.
  • —skip-cache: Do not remember prompt answers. Default: false.
  • —skip-install: Do not automatically install dependencies. Default: false.
  • —force-install: Fail on install dependencies error. Default: false.
  • —relationType: Relation type.
  • —sourceModel: Source model.
  • —destinationModel: Destination model.
  • —relationName: Relation name.
  • -c, : JSON file name or value to configure options.
  • -y, —yes: Skip all confirmation prompts with default or provided value.
  • —format: Format generated code using npm run lint:fix.

Arguments

Defining lb4 relation in one command line interface (cli):

  1. lb4 relation --sourceModel=<sourceModel>
  2. --destinationModel=<destinationModel> --foreignKeyName=<foreignKeyName>
  3. --relationType=<hasMany|belongsTo> [--relationName=<relationName>] [--format]
  • <relationType> - Type of the relation that will be created between thesource and target models.

  • <destinationModel> - Name of the model to create a relationship with.

  • <foreignKeyName> - Property that references the primary key property of thedestination model.

  • <relationName> - Name of the relation that will be created.

The tool will prompt you for:

  • Name of the source model.(sourceModel) Prompts a list of availablemodels to choose from as the source model of the relation.

  • Name of the model.(targetModel) Prompts a list of availablemodels to choose from as the target model of the relation.

  • Name of the Source property.(relationName) Prompts for the Sourceproperty name. Note: Leave blank to use the default.

Default values:

  • <targetModel><targetModelPrimaryKey> for belongsTo relations, e.g.categoryId
  • plural form of <targetModel> for hasMany relations, e.g. products
    • Name of Foreign key(foreignKeyName) to be created in target model. ForhasMany relation type only, default: <sourceModel><sourceModelPrimaryKey>.Note: Leave blank to use the default.

Output

  • Update source Model class as follows:/src/models/${sourceModel-Name}.model.ts
  • Update target Model class as follows:/src/models/${targetModel-Name}.model.ts
  • Update source Model Repository class as follows:/src/repositories/${sourceModel-Repository-Name}.repository.ts
  • Update target Model Repository class as follows:/src/repositories/${targetModel-Repository-Name}.repository.ts
  • Update /src/controllers/index.ts to export the newly created Controllerclass.