Skip to content

Commit 1f0aa0b

Browse files
nabdelgadirbajtos
andcommitted
feat: add navigational properties to find* methods
Updated find* methods to include navigational properties, introduced *Relations to models, and updated DefaultCrudRepository to include Relations Co-authored-by: Miroslav Bajtoš <mbajtoss@gmail.com>
1 parent 87cf4ca commit 1f0aa0b

34 files changed

+270
-81
lines changed

docs/site/BelongsTo-relation.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ export class Order extends Entity {
6767
super(data);
6868
}
6969
}
70+
71+
export interface OrderRelations {
72+
// describe navigational properties here
73+
}
74+
75+
export type OrderWithRelations = Order & OrderRelations;
7076
```
7177

7278
The definition of the `belongsTo` relation is inferred by using the `@belongsTo`
@@ -83,6 +89,10 @@ class Order extends Entity {
8389
@belongsTo(() => Customer, {keyTo: 'pk'})
8490
customerId: number;
8591
}
92+
93+
export interface OrderRelations {
94+
customer?: CustomerWithRelations;
95+
}
8696
```
8797

8898
## Configuring a belongsTo relation
@@ -120,12 +130,13 @@ import {
120130
juggler,
121131
repository,
122132
} from '@loopback/repository';
123-
import {Customer, Order} from '../models';
133+
import {Customer, Order, OrderRelations} from '../models';
124134
import {CustomerRepository} from '../repositories';
125135

126136
export class OrderRepository extends DefaultCrudRepository<
127137
Order,
128-
typeof Order.prototype.id
138+
typeof Order.prototype.id,
139+
OrderRelations
129140
> {
130141
public readonly customer: BelongsToAccessor<
131142
Customer,
@@ -212,14 +223,22 @@ export class Category extends Entity {
212223
super(data);
213224
}
214225
}
226+
227+
export interface CategoryRelations {
228+
categories?: CategoryWithRelations[];
229+
parent?: CategoryWithRelations;
230+
}
231+
232+
export type CategoryWithRelations = Category & CategoryRelations;
215233
```
216234

217235
The `CategoryRepository` must be declared like below
218236

219237
```ts
220238
export class CategoryRepository extends DefaultCrudRepository<
221239
Category,
222-
typeof Category.prototype.id
240+
typeof Category.prototype.id,
241+
CategoryRelations
223242
> {
224243
public readonly parent: BelongsToAccessor<
225244
Category,

docs/site/HasMany-relation.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ export class Order extends Entity {
131131
super(data);
132132
}
133133
}
134+
135+
export interface OrderRelations {
136+
// describe navigational properties here
137+
}
138+
139+
export type OrderWithRelations = Order & OrderRelations;
134140
```
135141

136142
The foreign key property (`customerId`) in the target model can be added via a
@@ -140,7 +146,7 @@ corresponding [belongsTo](BelongsTo-relation.md) relation, too.
140146

141147
```ts
142148
import {Entity, model, property, belongsTo} from '@loopback/repository';
143-
import {Customer} from './customer.model';
149+
import {Customer, CustomerWithRelations} from './customer.model';
144150

145151
@model()
146152
export class Order extends Entity {
@@ -164,6 +170,12 @@ export class Order extends Entity {
164170
super(data);
165171
}
166172
}
173+
174+
export interface OrderRelations {
175+
customer?: CustomerWithRelations;
176+
}
177+
178+
export type OrderWithRelations = Order & OrderRelations;
167179
```
168180

169181
## Configuring a hasMany relation
@@ -195,7 +207,7 @@ The following code snippet shows how it would look like:
195207
content="/src/repositories/customer.repository.ts" %}
196208

197209
```ts
198-
import {Order, Customer} from '../models';
210+
import {Order, Customer, CustomerRelations} from '../models';
199211
import {OrderRepository} from './order.repository';
200212
import {
201213
DefaultCrudRepository,
@@ -207,7 +219,8 @@ import {inject, Getter} from '@loopback/core';
207219

208220
export class CustomerRepository extends DefaultCrudRepository<
209221
Customer,
210-
typeof Customer.prototype.id
222+
typeof Customer.prototype.id,
223+
CustomerRelations
211224
> {
212225
public readonly orders: HasManyRepositoryFactory<
213226
Order,

docs/site/Repositories.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,14 @@ TypeScript version:
162162

163163
```ts
164164
import {DefaultCrudRepository, juggler} from '@loopback/repository';
165-
import {Account} from '../models';
165+
import {Account, AccountRelations} from '../models';
166166
import {DbDataSource} from '../datasources';
167167
import {inject} from '@loopback/context';
168168

169169
export class AccountRepository extends DefaultCrudRepository<
170170
Account,
171-
typeof Account.prototype.id
171+
typeof Account.prototype.id,
172+
AccountRelations
172173
> {
173174
constructor(@inject('datasources.db') dataSource: DbDataSource) {
174175
super(Account, dataSource);

docs/site/hasOne-relation.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ defined on a source model `Supplier` in the example below:
5757
{% include code-caption.html content="/src/models/supplier.model.ts" %}
5858

5959
```ts
60-
import {Account} from './account.model';
60+
import {Account, AccountWithRelations} from './account.model';
6161
import {Entity, property, hasOne} from '@loopback/repository';
6262

6363
export class Supplier extends Entity {
@@ -80,13 +80,19 @@ export class Supplier extends Entity {
8080
super(data);
8181
}
8282
}
83+
84+
export interface SupplierRelations {
85+
account?: AccountWithRelations;
86+
}
87+
88+
export type SupplierWithRelations = Supplier & SupplierRelations;
8389
```
8490

8591
On the other side of the relation, we'd need to declare a `belongsTo` relation
8692
since every `Account` has to belong to exactly one `Supplier`:
8793

8894
```ts
89-
import {Supplier} from './supplier.model';
95+
import {Supplier, SupplierWithRelations} from './supplier.model';
9096
import {Entity, property, belongsTo} from '@loopback/repository';
9197

9298
export class Account extends Entity {
@@ -108,6 +114,12 @@ export class Account extends Entity {
108114
super(data);
109115
}
110116
}
117+
118+
export interface AccountRelations {
119+
supplier?: SupplierWithRelations;
120+
}
121+
122+
export type AccountWithRelations = Account & AccountRelations;
111123
```
112124

113125
The definition of the `hasOne` relation is inferred by using the `@hasOne`
@@ -190,7 +202,7 @@ The following code snippet shows how it would look like:
190202
content="/src/repositories/supplier.repository.ts" %}
191203

192204
```ts
193-
import {Account, Supplier} from '../models';
205+
import {Account, Supplier, SupplierRelations} from '../models';
194206
import {AccountRepository} from './account.repository';
195207
import {
196208
DefaultCrudRepository,
@@ -202,7 +214,8 @@ import {inject, Getter} from '@loopback/core';
202214

203215
export class SupplierRepository extends DefaultCrudRepository<
204216
Supplier,
205-
typeof Supplier.prototype.id
217+
typeof Supplier.prototype.id,
218+
SupplierRelations
206219
> {
207220
public readonly account: HasOneRepositoryFactory<
208221
Account,

docs/site/tutorials/todo-list/todo-list-tutorial-model.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ model. Add the following import statements and property to the `TodoList` model:
7676

7777
```ts
7878
import {hasMany} from '@loopback/repository';
79-
import {Todo} from './todo.model';
79+
import {Todo, TodoWithRelations} from './todo.model';
8080

8181
@model()
8282
export class TodoList extends Entity {
@@ -87,6 +87,12 @@ export class TodoList extends Entity {
8787

8888
// ...constructor def...
8989
}
90+
91+
export interface TodoListRelations {
92+
todos?: TodoWithRelations[];
93+
}
94+
95+
export type TodoListWithRelations = TodoList & TodoListRelations;
9096
```
9197

9298
The `@hasMany()` decorator defines this property. As the decorator's name
@@ -108,6 +114,12 @@ export class Todo extends Entity {
108114

109115
// ...constructor def...
110116
}
117+
118+
export interface TodoRelations {
119+
todoList?: TodoListWithRelations;
120+
}
121+
122+
export type TodoWithRelations = Todo & TodoRelations;
111123
```
112124

113125
Once the models have been completely configured, it's time to move on to adding

docs/site/tutorials/todo-list/todo-list-tutorial-repository.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ import {
5858
juggler,
5959
repository,
6060
} from '@loopback/repository';
61-
import {Todo, TodoList} from '../models';
61+
import {Todo, TodoList, TodoListRelations} from '../models';
6262
import {TodoRepository} from './todo.repository';
6363
6464
export class TodoListRepository extends DefaultCrudRepository<
6565
TodoList,
66-
typeof TodoList.prototype.id
66+
typeof TodoList.prototype.id,
67+
TodoListRelations
6768
> {
6869
public readonly todos: HasManyRepositoryFactory<
6970
Todo,

examples/express-composition/src/models/note.model.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ export class Note extends Entity {
2828
super(data);
2929
}
3030
}
31+
32+
export interface NoteRelations {
33+
// describe navigational properties here
34+
}
35+
36+
export type NoteWithRelations = Note & NoteRelations;

examples/express-composition/src/repositories/note.repository.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6+
import {inject} from '@loopback/core';
67
import {DefaultCrudRepository} from '@loopback/repository';
7-
import {Note} from '../models';
88
import {DsDataSource} from '../datasources';
9-
import {inject} from '@loopback/core';
9+
import {Note, NoteRelations} from '../models';
1010

1111
export class NoteRepository extends DefaultCrudRepository<
1212
Note,
13-
typeof Note.prototype.id
13+
typeof Note.prototype.id,
14+
NoteRelations
1415
> {
1516
constructor(@inject('datasources.ds') dataSource: DsDataSource) {
1617
super(Note, dataSource);

examples/todo-list/src/models/todo-list-image.model.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6-
import {Entity, model, property, belongsTo} from '@loopback/repository';
7-
import {TodoList} from './todo-list.model';
6+
import {belongsTo, Entity, model, property} from '@loopback/repository';
7+
import {TodoList, TodoListWithRelations} from './todo-list.model';
88

99
@model()
1010
export class TodoListImage extends Entity {
@@ -29,3 +29,9 @@ export class TodoListImage extends Entity {
2929
super(data);
3030
}
3131
}
32+
33+
export interface TodoListImageRelations {
34+
todoList?: TodoListWithRelations;
35+
}
36+
37+
export type TodoListImageWithRelations = TodoListImage & TodoListImageRelations;

examples/todo-list/src/models/todo-list.model.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6-
import {Entity, model, property, hasMany, hasOne} from '@loopback/repository';
7-
import {Todo} from './todo.model';
8-
import {TodoListImage} from './todo-list-image.model';
6+
import {Entity, hasMany, hasOne, model, property} from '@loopback/repository';
7+
import {
8+
TodoListImage,
9+
TodoListImageWithRelations,
10+
} from './todo-list-image.model';
11+
import {Todo, TodoWithRelations} from './todo.model';
912

1013
@model()
1114
export class TodoList extends Entity {
@@ -36,3 +39,10 @@ export class TodoList extends Entity {
3639
super(data);
3740
}
3841
}
42+
43+
export interface TodoListRelations {
44+
todos?: TodoWithRelations[];
45+
image?: TodoListImageWithRelations;
46+
}
47+
48+
export type TodoListWithRelations = TodoList & TodoListRelations;

0 commit comments

Comments
 (0)