Skip to content

Commit

Permalink
fixup! apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed May 30, 2019
1 parent 61a0ddc commit 5d5450e
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 26 deletions.
14 changes: 13 additions & 1 deletion docs/site/BelongsTo-relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export class Order extends Entity {
super(data);
}
}

export interface OrderRelations {
// describe navigational properties here
}

export type OrderWithRelations = Order & OrderRelations;
```

The definition of the `belongsTo` relation is inferred by using the `@belongsTo`
Expand All @@ -83,6 +89,10 @@ class Order extends Entity {
@belongsTo(() => Customer, {keyTo: 'pk'})
customerId: number;
}

export interface OrderRelations {
customer?: CustomerWithRelations;
}
```

## Configuring a belongsTo relation
Expand Down Expand Up @@ -214,7 +224,9 @@ export class Category extends Entity {
}
}

export interface CategoryRelations {}
export interface CategoryRelations {
categories?: CategoryWithRelations[];
}

export type CategoryWithRelations = Category & CategoryRelations;
```
Expand Down
14 changes: 13 additions & 1 deletion docs/site/HasMany-relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ export class Order extends Entity {
super(data);
}
}

export interface OrderRelations {
// describe navigational properties here
}

export type OrderWithRelations = Order & OrderRelations;
```

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

```ts
import {Entity, model, property, belongsTo} from '@loopback/repository';
import {Customer} from './customer.model';
import {Customer, CustomerWithRelations} from './customer.model';

@model()
export class Order extends Entity {
Expand All @@ -164,6 +170,12 @@ export class Order extends Entity {
super(data);
}
}

export interface OrderRelations {
customer?: CustomerWithRelations;
}

export type OrderWithRelations = Order & OrderRelations;
```

## Configuring a hasMany relation
Expand Down
16 changes: 14 additions & 2 deletions docs/site/hasOne-relation.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ defined on a source model `Supplier` in the example below:
{% include code-caption.html content="/src/models/supplier.model.ts" %}

```ts
import {Account} from './account.model';
import {Account, AccountWithRelations} from './account.model';
import {Entity, property, hasOne} from '@loopback/repository';

export class Supplier extends Entity {
Expand All @@ -80,13 +80,19 @@ export class Supplier extends Entity {
super(data);
}
}

export interface SupplierRelations {
account?: AccountWithRelations;
}

export type SupplierWithRelations = Supplier & SupplierRelations;
```

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

```ts
import {Supplier} from './supplier.model';
import {Supplier, SupplierWithRelations} from './supplier.model';
import {Entity, property, belongsTo} from '@loopback/repository';

export class Account extends Entity {
Expand All @@ -108,6 +114,12 @@ export class Account extends Entity {
super(data);
}
}

export interface AccountRelations {
supplier?: SupplierWithRelations;
}

export type AccountWithRelations = Account & AccountRelations;
```

The definition of the `hasOne` relation is inferred by using the `@hasOne`
Expand Down
10 changes: 7 additions & 3 deletions docs/site/tutorials/todo-list/todo-list-tutorial-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ model. Add the following import statements and property to the `TodoList` model:

```ts
import {hasMany} from '@loopback/repository';
import {Todo} from './todo.model';
import {Todo, TodoWithRelations} from './todo.model';

@model()
export class TodoList extends Entity {
Expand All @@ -88,7 +88,9 @@ export class TodoList extends Entity {
// ...constructor def...
}

export interface TodoListRelations {}
export interface TodoListRelations {
todos?: TodoWithRelations[];
}

export type TodoListWithRelations = TodoList & TodoListRelations;
```
Expand All @@ -113,7 +115,9 @@ export class Todo extends Entity {
// ...constructor def...
}

export interface TodoRelations {}
export interface TodoRelations {
todoList?: TodoListWithRelations;
}

export type TodoWithRelations = Todo & TodoRelations;
```
Expand Down
4 changes: 3 additions & 1 deletion examples/express-composition/src/models/note.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export class Note extends Entity {
}
}

export interface NoteRelations {}
export interface NoteRelations {
// describe navigational properties here
}

export type NoteWithRelations = Note & NoteRelations;
6 changes: 4 additions & 2 deletions examples/todo-list/src/models/todo-list-image.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

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

@model()
export class TodoListImage extends Entity {
Expand All @@ -30,6 +30,8 @@ export class TodoListImage extends Entity {
}
}

export interface TodoListImageRelations {}
export interface TodoListImageRelations {
todoList?: TodoListWithRelations;
}

export type TodoListImageWithRelations = TodoListImage & TodoListImageRelations;
12 changes: 9 additions & 3 deletions examples/todo-list/src/models/todo-list.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
// License text available at https://opensource.org/licenses/MIT

import {Entity, hasMany, hasOne, model, property} from '@loopback/repository';
import {TodoListImage} from './todo-list-image.model';
import {Todo} from './todo.model';
import {
TodoListImage,
TodoListImageWithRelations,
} from './todo-list-image.model';
import {Todo, TodoWithRelations} from './todo.model';

@model()
export class TodoList extends Entity {
Expand Down Expand Up @@ -37,6 +40,9 @@ export class TodoList extends Entity {
}
}

export interface TodoListRelations {}
export interface TodoListRelations {
todos?: TodoWithRelations[];
image?: TodoListImageWithRelations;
}

export type TodoListWithRelations = TodoList & TodoListRelations;
6 changes: 4 additions & 2 deletions examples/todo-list/src/models/todo.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

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

@model()
export class Todo extends Entity {
Expand Down Expand Up @@ -42,6 +42,8 @@ export class Todo extends Entity {
}
}

export interface TodoRelations {}
export interface TodoRelations {
todoList?: TodoListWithRelations[];
}

export type TodoWithRelations = Todo & TodoRelations;
4 changes: 3 additions & 1 deletion examples/todo/src/models/todo.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export class Todo extends Entity {
}
}

export interface TodoRelations {}
export interface TodoRelations {
// describe navigational properties here
}

export type TodoWithRelations = Todo & TodoRelations;
6 changes: 6 additions & 0 deletions packages/cli/generators/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
if (this.artifactInfo.allowAdditionalProperties) {
Object.assign(this.artifactInfo.modelSettings, {strict: false});
}

this.log(
`Let's add a property to ${chalk.yellow(
this.artifactInfo.className,
)}`,
);
})
.catch(err => {
debug(`Error during model strict mode prompt: ${err}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/generators/model/templates/model.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class <%= className %> extends <%= modelBaseClass %> {
}
export interface <%= className %>Relations {
// describe navigational properties here
}
export type <%= className %>WithRelations = <%= className %> & <%= className %>Relations;
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ describe('lb4 model integration', () => {
);
assert.fileContent(
expectedModelFile,
/export interface TestRelations {\n\n}/,
/export interface TestRelations {\n \/\/ describe navigational properties here\n}/,
);
});

Expand Down
4 changes: 3 additions & 1 deletion packages/repository/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export class Note extends Entity {
content: string;
}

export interface NoteRelations {}
export interface NoteRelations {
// describe navigational properties here
}

export type NoteWithRelations = Note & NoteRelations;
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {belongsTo, Entity, model, property} from '../../..';
import {Customer} from './customer.model';
import {Customer, CustomerWithRelations} from './customer.model';

@model()
export class Address extends Entity {
Expand All @@ -30,6 +30,8 @@ export class Address extends Entity {
customerId: number;
}

export interface AddressRelations {}
export interface AddressRelations {
customer?: CustomerWithRelations;
}

export type AddressWithRelations = Address & AddressRelations;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {belongsTo, Entity, hasMany, hasOne, model, property} from '../../..';
import {Address} from './address.model';
import {Address, AddressWithRelations} from './address.model';
import {Order} from './order.model';

@model()
Expand Down Expand Up @@ -33,6 +33,10 @@ export class Customer extends Entity {
parentId?: number;
}

export interface CustomerRelations {}
export interface CustomerRelations {
address?: AddressWithRelations;
customers?: CustomerWithRelations[];
parentCustomer?: CustomerWithRelations;
}

export type CustomerWithRelations = Customer & CustomerRelations;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {belongsTo, Entity, model, property} from '../../..';
import {Customer} from './customer.model';
import {Customer, CustomerWithRelations} from './customer.model';

@model()
export class Order extends Entity {
Expand All @@ -30,6 +30,8 @@ export class Order extends Entity {
customerId: number;
}

export interface OrderRelations {}
export interface OrderRelations {
customer?: CustomerWithRelations;
}

export type OrderWithRelations = Order & OrderRelations;
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class Product extends Entity {
}
}

export interface ProductRelations {}
export interface ProductRelations {
// describe navigational properties here
}

export type ProductWithRelations = Product & ProductRelations;

0 comments on commit 5d5450e

Please sign in to comment.