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
lb4 relation commandhasMany, belongsTo in progress).hasMany ask the following prompts:hasMany decorator. Will need to import the TO model here.HasManyRepositoryFactory and it's implementation. If it exists, use AST to add the new property and code related to power the HasMany relation. See: https://github.com/strongloop/loopback-next/blob/master/examples/todo-list/src/repositories/todo-list.repository.ts for details.fromModel-toModel.controller.ts that 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.--config support (JSON Definition of Model) and --yes support (Skip relation property name, foreign key, assume they want to create/update repo, create controller).lb4 relation fromModel hasMany toModel ... with --yes this is 馃敟 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).
As for the controller generator, our current approach is to have one controller for each relation. For example, when Customer has many orders, there will be three controllers: CustomerController, OrderController and CustomerOrderController.
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.
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.
We can possibly leverage prettier for TypeScript source code manipulation. I built a quick PoC as follows:
const prettier = require('prettier');
const util = require('util');
async function transform() {
const prog = await prettier.format(
`/** class */
class X {
a: string;
b(): string {
return '1';
}
}`,
{
parser(text, {typescript}) {
const ast = typescript(text);
console.log(util.inspect(ast, {depth: 5}));
ast.body[0].id.name = 'Y';
return ast;
},
},
);
console.log(prog);
}
transform();
You'll now get:
/** class */
class Y {
a: string;
b(): string {
return "1";
}
}
How about these ones ?
https://github.com/SBoudrias/AST-query
https://github.com/estools/esquery
@marioestradarosa Thank you for chiming in. The ones you found seem to be only for JavaScript, not TypeScript.
@raymondfeng there are ports for TS also https://github.com/phenomnomnominal/tsquery
I want to learn from you :-) , why the current typescript (with a linter) would not be suitable for this purpose? or any other package like tsquery as opposed to the excellent POC you mentioned above with prettier?.
As we discussed in the estimation meeting, this will be a post-ga story and let's create a new issue to document how to update the relation repository as a temporary solution.
I think that we should have this done in three separate Pull Requests.
I propose the initial PR as follows
lb4 relation that asks for the following 3 prompts.Example:
Select the first Model:
Order
Item
_Customer <_
Select a relationship type
Belongs To
HasMany <
Select the second Model or we can display something like this: Customers has many ?
Orders <
items
The generator will update the two models in src/models and add the necessary sintax to support it.
Advanced functionality using AST
Note: The other two PRs proposed should be done in the lb4 repository to support the relationship and in the lb4 controller to generate the appropriate controllers to support the defined relationship.
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.
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.
@dhmlau
If a user wants to specify in the FROM model a property other than its ID property (keyFrom attribute), should it be supported in the CLI as well?
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.
Sounds great, assigning the issue to you :)
If a user wants to specify in the
FROMmodel a property other than itsIDproperty (keyFromattribute), should it be supported in the CLI as well?
Personally, I prefer to work in small increments and start with the simplest possible solution. I think that means no user-configurable FROM/keyFrom property, what do you think?
For longer term, I think it makes sense to eventually extend lb4 relation command to allow callers to customize all aspects of the relation to be created, including a custom keyFrom setting.
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.
What is special about belongsTo that it is a requirement of hasOne? I'm pretty sure that wasn't a requirement in LB3.
Closing as done via https://github.com/strongloop/loopback-next/pull/2426
Most helpful comment
@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?