Skip to content

Commit 697cbeb

Browse files
committed
refactor: user relation cli
1 parent e3103b1 commit 697cbeb

File tree

5 files changed

+77
-25
lines changed

5 files changed

+77
-25
lines changed

docs/site/migration/auth/example.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ Enter an empty property name when done
548548
Model UserCredentials was created in src/models/
549549
```
550550

551-
Create corresponding repositories
551+
Create corresponding repositories:
552552

553553
```sh
554554
$ lb4 repository
@@ -557,6 +557,56 @@ $ lb4 repository
557557
? Please select the repository base class DefaultCrudRepository (Legacy juggler bridge)
558558
```
559559

560+
Create relations:
561+
562+
- `Project` belongsTo `User`
563+
564+
```sh
565+
lb4 relation
566+
? Please select the relation type belongsTo
567+
? Please select source model Project
568+
? Please select target model User
569+
? Foreign key name to define on the source model ownerId
570+
? Relation name owner
571+
? Allow Project queries to include data from related User instances? Yes
572+
```
573+
574+
{% include tip.html content="
575+
Please delete the generated controller file `src/controllers/project-user.controller.ts` to keep the exposed endpoints clean.
576+
" %}
577+
578+
- `User` hasOne `UserCredentials`
579+
580+
```sh
581+
lb4 relation
582+
? Please select the relation type hasOne
583+
? Please select source model User
584+
? Please select target model UserCredentials
585+
? Foreign key name to define on the target model userId
586+
? Source property name for the relation getter (will be the relation name) userCredentials
587+
? Allow User queries to include data from related UserCredentials instances? Yes
588+
```
589+
590+
{% include tip.html content="
591+
Please delete the generated controller file `src/controllers/user-user-credentials.controller.ts` to keep the exposed endpoints clean.
592+
" %}
593+
594+
- `User` hasMany `Team`
595+
596+
```sh
597+
lb4 relation
598+
? Please select the relation type hasMany
599+
? Please select source model User
600+
? Please select target model Team
601+
? Foreign key name to define on the target model ownerId
602+
? Source property name for the relation getter (will be the relation name) teams
603+
? Allow User queries to include data from related Team instances? Yes
604+
```
605+
606+
{% include tip.html content="
607+
Please delete the generated controller file `src/controllers/user-team.controller.ts` to keep the exposed endpoints clean.
608+
" %}
609+
560610
### User Controller Creation
561611

562612
```sh

examples/access-control-migration/src/models/project.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class Project extends Entity {
2727
balance: number;
2828

2929
@belongsTo(() => User)
30-
ownerId?: number;
30+
ownerId: number;
3131

3232
// Define well-known properties here
3333

examples/access-control-migration/src/models/user.model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// License text available at https://opensource.org/licenses/MIT
55

66
import {Entity, hasMany, hasOne, model, property} from '@loopback/repository';
7-
import {Team} from './team.model';
7+
import {Team} from '.';
88
import {UserCredentials} from './user-credentials.model';
99

1010
@model({
@@ -50,12 +50,12 @@ export class User extends Entity {
5050
})
5151
verificationToken?: string;
5252

53-
@hasMany(() => Team, {keyTo: 'ownerId'})
54-
teams: Team[];
55-
5653
@hasOne(() => UserCredentials)
5754
userCredentials: UserCredentials;
5855

56+
@hasMany(() => Team, {keyTo: 'ownerId'})
57+
teams: Team[];
58+
5959
// Define well-known properties here
6060

6161
// Indexer property to allow additional data

examples/access-control-migration/src/repositories/project.repository.ts

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

6+
import {Getter, inject} from '@loopback/core';
67
import {
8+
BelongsToAccessor,
79
DefaultCrudRepository,
810
repository,
9-
BelongsToAccessor,
1011
} from '@loopback/repository';
11-
import {Project, ProjectRelations, User} from '../models';
1212
import {DbDataSource} from '../datasources';
13-
import {inject, Getter} from '@loopback/core';
13+
import {Project, ProjectRelations, User} from '../models';
1414
import {UserRepository} from './user.repository';
1515

1616
export class ProjectRepository extends DefaultCrudRepository<
@@ -20,13 +20,15 @@ export class ProjectRepository extends DefaultCrudRepository<
2020
> {
2121
public readonly user: BelongsToAccessor<User, typeof Project.prototype.id>;
2222

23+
public readonly owner: BelongsToAccessor<User, typeof Project.prototype.id>;
24+
2325
constructor(
2426
@inject('datasources.db') dataSource: DbDataSource,
2527
@repository.getter('UserRepository')
2628
protected userRepositoryGetter: Getter<UserRepository>,
2729
) {
2830
super(Project, dataSource);
29-
this.user = this.createBelongsToAccessorFor('owner', userRepositoryGetter);
30-
this.registerInclusionResolver('user', this.user.inclusionResolver);
31+
this.owner = this.createBelongsToAccessorFor('owner', userRepositoryGetter);
32+
this.registerInclusionResolver('owner', this.owner.inclusionResolver);
3133
}
3234
}

examples/access-control-migration/src/repositories/user.repository.ts

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

6+
import {Getter, inject} from '@loopback/core';
67
import {
78
DefaultCrudRepository,
8-
repository,
99
HasManyRepositoryFactory,
1010
HasOneRepositoryFactory,
11+
repository,
1112
} from '@loopback/repository';
12-
import {User, UserRelations, Team, UserCredentials} from '../models';
1313
import {DbDataSource} from '../datasources';
14-
import {inject, Getter} from '@loopback/core';
14+
import {Team, User, UserCredentials, UserRelations} from '../models';
1515
import {TeamRepository} from './team.repository';
1616
import {UserCredentialsRepository} from './user-credentials.repository';
1717

@@ -20,26 +20,31 @@ export class UserRepository extends DefaultCrudRepository<
2020
typeof User.prototype.id,
2121
UserRelations
2222
> {
23-
public readonly teams: HasManyRepositoryFactory<
24-
Team,
23+
public readonly userCredentials: HasOneRepositoryFactory<
24+
UserCredentials,
2525
typeof User.prototype.id
2626
>;
2727

28-
public readonly userCredentials: HasOneRepositoryFactory<
29-
UserCredentials,
28+
public readonly teams: HasManyRepositoryFactory<
29+
Team,
3030
typeof User.prototype.id
3131
>;
3232

3333
constructor(
3434
@inject('datasources.db') dataSource: DbDataSource,
35-
@repository.getter('TeamRepository')
36-
protected teamRepositoryGetter: Getter<TeamRepository>,
3735
@repository.getter('UserCredentialsRepository')
3836
protected userCredentialsRepositoryGetter: Getter<
3937
UserCredentialsRepository
4038
>,
39+
@repository.getter('TeamRepository')
40+
protected teamRepositoryGetter: Getter<TeamRepository>,
4141
) {
4242
super(User, dataSource);
43+
this.teams = this.createHasManyRepositoryFactoryFor(
44+
'teams',
45+
teamRepositoryGetter,
46+
);
47+
this.registerInclusionResolver('teams', this.teams.inclusionResolver);
4348
this.userCredentials = this.createHasOneRepositoryFactoryFor(
4449
'userCredentials',
4550
userCredentialsRepositoryGetter,
@@ -48,11 +53,6 @@ export class UserRepository extends DefaultCrudRepository<
4853
'userCredentials',
4954
this.userCredentials.inclusionResolver,
5055
);
51-
this.teams = this.createHasManyRepositoryFactoryFor(
52-
'teams',
53-
teamRepositoryGetter,
54-
);
55-
this.registerInclusionResolver('teams', this.teams.inclusionResolver);
5656
}
5757

5858
async findCredentials(

0 commit comments

Comments
 (0)