Prerequisites
Important: Before running this generator, make sure the models, datasource, andrepositories involved in this relation exist. Then, inside your LoopBackapplication, run the command from the root directory.
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 Relation
between existing source and target models in a LoopBackapplication.
lb4 relation [options]
Options
-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.—foreignKeyName
: Destination model foreign key name.—relationName
: Relation name.-c
,—config
: JSON file name or value to configure options.-y
,—yes
: Skip all confirmation prompts with default or provided value.—format
: Format generated code usingnpm run lint:fix
.
Arguments
Defining lb4 relation in one command line interface (cli):
lb4 relation --sourceModel=<sourceModel>
--destinationModel=<destinationModel> --foreignKeyName=<foreignKeyName>
--relationType=<hasMany|belongsTo> [--relationName=<relationName>] [--format]
<relationType>
- Type of the relation that will be created between thesource and target models.<sourceModel>
- Name of the model to create the relationship from.<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.
Interactive Prompts
The tool will prompt you for:
Relation
type
between models.(relationBaseClass) Prompts a list ofavailable relations to choose from as the type of the relation between thesource model and the target model. Supported relation types:Name of the
source
model.(sourceModel) Prompts a list of availablemodels to choose from as the source model of the relation.Name of the
target
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>
forbelongsTo
relations, e.g.categoryId
- plural form of
<targetModel>
forhasMany
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.
- Name of Foreign key(foreignKeyName) to be created in target model. ForhasMany relation type only, default:
Output
Once all the prompts have been answered, the CLI will update or create sourcefiles for the Entities involved in the relation.
- 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
- Create a Controller for the new relation as follows:
/src/controllers/{sourceModel-Name}-{targetModel-Name}.controller.ts
- Update
/src/controllers/index.ts
to export the newly created Controllerclass.