Important: Before running this generator, make sure the models, datasource, and repositories involved in this relation exist. Then, inside your LoopBack application, run the command from the root directory.
Adds a new
Relation between existing source and target models in a LoopBack
lb4 relation [options]
--help: Print the generator’s options and usage.
--skip-cache: Do not remember prompt answers. Default:
--skip-install: Do not automatically install dependencies. Default:
--force-install: Fail on install dependencies error. Default:
--relationType: Relation type.
--sourceModel: Source model.
--destinationModel: Destination model.
--foreignKeyName: Destination model foreign key name.
--relationName: Relation name.
--config: JSON file name or value to configure options.
--yes: Skip all confirmation prompts with default or provided value.
--format: Format generated code using
npm run lint:fix.
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 the source 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 the destination model.
<relationName>- Name of the relation that will be created.
After setting up the definition for the relation, you need to confirm if you want to register the inclusion resolver for this relation:
? Allow <sourceModel> queries to include data from related <destinationModel> instances? (Y/n)
The tool will prompt you for:
typebetween models. (relationBaseClass) Prompts a list of available relations to choose from as the type of the relation between the source model and the target model. Supported relation types:
Name of the
sourcemodel. (sourceModel) Prompts a list of available models to choose from as the source model of the relation.
Name of the
targetmodel. (targetModel) Prompts a list of available models to choose from as the target model of the relation.
Name of the
Source property. (relationName) Prompts for the Source property name. Note: Leave blank to use the default.
- plural form of
Name of Foreign key (foreignKeyName) to be created in target model. For hasMany relation type only, default:
<sourceModel><sourceModelPrimaryKey>. Note: Leave blank to use the default.
Once all the prompts have been answered, the CLI will update or create source files for the Entities involved in the relation.
- Update source Model class as follows:
- Update target Model class as follows:
- Update source Model Repository class as follows:
- Update target Model Repository class as follows:
- Create a Controller for the new relation as follows:
/src/controllers/index.tsto export the newly created Controller class.