Repository navigation
Closed
Labels
Milestone
Description
Description / Steps to reproduce / Feature proposal
- CLI command lb4 relation or the likes to wire up related models (think of LB3)
- Creating CLI templates which expose relation related CRUD methods during controller method generation
- Adding CLI prompts which create/decorate relational properties etc.
See also #695
LoopBack 3 has models relations definition in JSON file or in JavaScript code
We would like to define models relations in LoopBack 4 as well. Definition of relations need to auto generate the api services.
cc @b-admike
Acceptance Criteria (@virkt25)
- Add a new
lb4 relationcommand - Prompt the user to select relation type (current support is:
hasMany,belongsToin progress). - For
hasManyask the following prompts:- Model to create a relationship FROM: Provide list of available models.
- Model to create a relationship TO: Provide list of available models (minus the one selected in the above).
- Property name for the relation: Recommend the plural form of the TO model.
- Enter name of custom foreign key (optional).
- Prompt the user to see if they want to create / update a Repository (see [CLI] add
lb4 repositorycommand #1588 for prompts you should ask / need to generate a repository) - Prompt the user to see if they want to create a controller to expose the relation over API
- Add the new property to the FROM model that is decorated with the
hasManydecorator. Will need to import the TO model here. - If a custom foreign key is defined, check to see if it exists in the TO model. If not, add an optional property.
- If the want to create / update repository: Check if a related repository exists. If not, create one which has the
HasManyRepositoryFactoryand it's implementation. If it exists, use AST to add the new property and code related to power theHasManyrelation. See: https://github.com/strongloop/loopback-next/blob/master/examples/todo-list/src/repositories/todo-list.repository.ts for details. - If a new controller is wanted, create a new controller
fromModel-toModel.controller.tsthat exposes GET, POST, PATCH, DEL. Template can be based on https://github.com/strongloop/loopback-next/blob/master/examples/todo-list/src/controllers/todo-list-todo.controller.ts. - Tests + Docs +
--configsupport (JSON Definition of Model) and--yessupport (Skip relation property name, foreign key, assume they want to create/update repo, create controller). - Should also support the following CLI Syntax:
lb4 relation fromModel hasMany toModel... with--yesthis is 🔥 - Blog
Note For Estimation
CHALLENGE: Updating a pre-defined Model is tricky if we are manipulating the file as text ... user could've defined multiple Classes in a single file, etc. The best approach will be to use Abstract Syntax Tree (AST).
Activity
[-][CLI] Relation [/-][+][CLI] Relations[/+]bajtos commentedon Aug 13, 2018
As for the controller generator, our current approach is to have one controller for each relation. For example, when
Customerhas many orders, there will be three controllers:CustomerController,OrderControllerandCustomerOrderController.The reason for this split is to keep application code clean and avoid controllers with too many methods. Consider the case where a model has 3-5 relations. If we kept all relation endpoints in a single controller, the controller could easily end up with more than 20 methods.
As a nice side effect, our code generator will be also simpler to implement because adding a relation means adding a new controller file instead of editing existing controller classes.
[-][CLI] Relations[/-][+][CLI] `lb4 relation` command[/+][-][CLI] `lb4 relation` command[/-][+][CLI] add `lb4 relation` command[/+]virkt25 commentedon Aug 28, 2018
Rejecting for now. While the task is well groomed, we need consensus on if we are going with AST Approach, Declarative Support, etc. Also need a spike on AST to figure out feasibility and build expertise. Also, this task should be broken down into smaller tasks.
raymondfeng commentedon Aug 29, 2018
We can possibly leverage
prettierfor TypeScript source code manipulation. I built a quick PoC as follows:You'll now get:
marioestradarosa commentedon Aug 29, 2018
How about these ones ?
https://github.com/SBoudrias/AST-query
https://github.com/estools/esquery
7 remaining items
dhmlau commentedon Oct 27, 2018
Per the discussion in our previous estimation meeting, this task would involve changing of the ts file. Therefore, we've created the spike #1656 to see what's the best approach. With this, I'm moving this task back to
Needs Priority.orshlom commentedon Dec 16, 2018
Hi @dhmlau,
I did some research on creating additional relations for models and repositories using TypeScript AST manipulation, following #1656 spike, and made a comment with my findings.
I am more than confident I can implement the CLI command for relations following your guidelines.
I would be happy to be contribute and be assigned on this issue.
orshlom commentedon Dec 27, 2018
@dhmlau
If a user wants to specify in the
FROMmodel a property other than itsIDproperty (keyFromattribute), should it be supported in the CLI as well?bajtos commentedon Jan 7, 2019
Sounds great, assigning the issue to you :)
Personally, I prefer to work in small increments and start with the simplest possible solution. I think that means no user-configurable FROM/
keyFromproperty, what do you think?For longer term, I think it makes sense to eventually extend
lb4 relationcommand to allow callers to customize all aspects of the relation to be created, including a customkeyFromsetting.TomerSalton commentedon Jan 10, 2019
Thanks for assigning me into this task :)
I have a question to have a better understanding of the relationships design.
From the documentation and from some tests I performed in my environment I realized:
I guess my question is why hasMany is different from hasOne in the aspect of requiring the belongsTo in the target model.
elv1s commentedon Jan 29, 2019
What is special about
belongsTothat it is a requirement ofhasOne? I'm pretty sure that wasn't a requirement in LB3.bajtos commentedon May 24, 2019
Closing as done via #2426