@@ -6,8 +6,75 @@ sidebar: lb4_sidebar
6
6
permalink : /doc/en/lb4/migration-models-relations.html
7
7
---
8
8
9
- {% include note.html content="
10
- This is a placeholder page, the task of adding content is tracked by the
11
- following GitHub issue:
12
- [ loopback-next #3948 ] ( https://github.com/strongloop/loopback-next/issues/3948 ) .
13
- " %}
9
+ When you define a relation in a LoopBack 3 model JSON file, the framework will
10
+ create the following artifacts for you automatically:
11
+
12
+ - Relation metadata defining the target model, foreign key column/property, and
13
+ so on.
14
+ - Repository-like methods for accessing related model instance(s), for example
15
+ ` Category.prototype.products ` .
16
+ - An inclusion resolver to allow clients to request relation traversal in
17
+ queries and include related models, e.g.
18
+ ` Product.find({include: ['category']}) `
19
+ - REST API endpoints for querying a modifying models on the other side of the
20
+ relation, e.g. ` GET /api/categories/1/products ` .
21
+
22
+ In LoopBack 4, these building blocks are typically provided by the application
23
+ developer.
24
+
25
+ - Relation metadata is defined via model decorators like ` @hasMany ` .
26
+ - Relation repositories implement APIs for accessing and modifying data of
27
+ related models.
28
+ - Inclusion resolvers implement relation traversal in queries.
29
+ - Relation controllers implement REST APIs for model relations.
30
+
31
+ At the moment, all of these artifacts are defined via source code files which
32
+ are typically created by running ` lb4 relation ` . With source code files ready to
33
+ be edited, developers get a lot of power and flexibility in customizing the
34
+ default behavior offered by the framework.
35
+
36
+ In the future, we would like to provide a declarative approach for building
37
+ model relations: the developer defines relation metadata, and the framework
38
+ builds all required artifacts at runtime, similar to how LoopBack 3 works. You
39
+ can join the discussion in the GitHub issue
40
+ [ loopback-next #2483 ] ( https://github.com/strongloop/loopback-next/issues/2483 ) .
41
+
42
+ ## Migration path
43
+
44
+ Follow these steps to migrate a model relation from LoopBack 3 to LoopBack 4:
45
+
46
+ 1 . Make sure to complete all steps described in
47
+ [ Migrating model definitions and built-in APIs] ( ./core.md ) , especially the
48
+ creation of Repository classes for models on both sides of the relation.
49
+
50
+ 2 . Run ` lb4 relation ` to define the model relation in your model class, generate
51
+ code for the relation repository and optionally register inclusion resolver.
52
+ This command will also create a new Controller class implementing public REST
53
+ API for your relation.
54
+
55
+ You can learn more about ` lb4 relation ` command in
56
+ [ Relation generator] ( ../../Relation-generator.md ) .
57
+
58
+ ## Relation types
59
+
60
+ The following relations are supported by LoopBack 4 and can be migrated from
61
+ LoopBack 3:
62
+
63
+ - [ HasMany] ( ../../HasMany-relation.md )
64
+ - [ HasOne] ( ../../hasOne-relation.md )
65
+ - [ BelongsTo] ( ../../BelongsTo-relation.md )
66
+
67
+ Other relations types are not supported yet, you can subscribe to our progress
68
+ in the high-level tracking issue
69
+ [ loopback-next #1450 ] ( https://github.com/strongloop/loopback-next/issues/1450 ) .
70
+ See also issues for individual relation types as mentioned in the tracking
71
+ issue, for example:
72
+
73
+ - HasManyThrough -
74
+ [ loopback-next #2264 ] ( https://github.com/strongloop/loopback-next/issues/2264 )
75
+ - HasAndBelongsToMany -
76
+ [ loopback-next #2308 ] ( https://github.com/strongloop/loopback-next/issues/2308 )
77
+ - Polymorphic relations -
78
+ [ loopback-next #2487 ] ( https://github.com/strongloop/loopback-next/issues/2487 )
79
+ - ReferencesMany -
80
+ [ loopback-next #2488 ] ( https://github.com/strongloop/loopback-next/issues/1450 )
0 commit comments