Skip to content

Commit

Permalink
Merge 729ef04 into 5a670ef
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Jan 8, 2019
2 parents 5a670ef + 729ef04 commit 116c1ad
Showing 1 changed file with 125 additions and 13 deletions.
138 changes: 125 additions & 13 deletions docs/site/Decorators_repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ If the model or datasource is already bound to the app, you can create the
repository by providing their names instead of the classes. For example:

```ts
// with `datasource` and `Todo` already defined.
// with `db` and `Todo` already defined.
app.bind('datasources.db').to(db);
app.bind('models.Todo').to(Todo);

Expand All @@ -119,9 +119,6 @@ export class TodoController {

### Relation Decorators

_This feature has not yet been released in alpha form. Documentation will be
added here as this feature progresses._

The relation decorator defines the nature of a relationship between two models.

#### Relation Decorator
Expand All @@ -133,19 +130,134 @@ Register a general relation.
_This feature has not yet been released in alpha form. Documentation will be
added here as this feature progresses._

#### Specific Relation Decorator
#### BelongsTo Decorator

Syntax:

- `@belongsTo`
- `@hasOne`
- `@hasMany`
- `@belongsTo(targetResolver: EntityResolver<T>, definition?: Partial<BelongsToDefinition>)`
- Many-to-one connection between models.
- A `Todo` belongs to a `TodoList`.
- See [BelongsTo relation](BelongsTo-relation.md) for more details.

{% include code-caption.html content="todo.model.ts" %}

```ts
import {belongsTo} from '@loopback/repository';
import {TodoList} from './todo-list.model';

export class Todo extends Entity {
// properties

@belongsTo(() => TodoList)
todoListId: number;

// etc
}
```

#### HasOne Decorator

- `@hasOne(targetResolver: EntityResolver<T>, definition?: Partial<HasOneDefinition>)`
- One-to-one connection between models.
- A `TodoList` has one `image`.
- See [HasOne relation](hasOne-relation.md) for more details.

{% include code-caption.html content="todo-list.model.ts" %}

```ts
import {hasOne} from '@loopback/repository';
import {TodoListImage} from './todo-list-image.model';

export class TodoList extends Entity {
@property({
type: 'number',
id: true,
})
id?: number;

// other properties

@hasOne(() => TodoListImage)
image: TodoListImage;

// etc
}
```

{% include code-caption.html content="todo-list-image.model.ts" %}

```ts
import {belongsTo} from '@loopback/repository';
import {TodoList} from './todo-list.model';

export class TodoListImage extends Entity {
@property({
type: 'number',
id: true,
})
id: number;

// other properties

@belongsTo(() => TodoList)
todoListId?: number;

// etc
}
```

#### HasMany Decorator

- `@hasMany(targetResolver: EntityResolver<T>, definition?: Partial<HasManyDefinition>)`
- One-to-many connection between models.
- A `TodoList` has many `Todo`s.
- See [HasMany relation](HasMany-relation.md) for more details.

{% include code-caption.html content="todo-list.model.ts" %}

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

export class TodoList extends Entity {
@property({
type: 'number',
id: true,
})
id?: number;

// other properties

@hasMany(() => Todo)
todos: Todo[];

// etc
}
```

{% include code-caption.html content="todo.model.ts" %}

```ts
import {belongsTo} from '@loopback/repository';
import {TodoList} from './todo-list.model';

export class Todo extends Entity {
// other properties

@belongsTo(() => TodoList)
todoListId?: number;

// etc
}
```

#### Other Decorators

The following decorators are not implemented yet. To see their progress, please
go to the
[Relations epic](https://github.com/strongloop/loopback-next/issues/1450).

- `@embedsOne`
- `@embedsMany`
- `@referencesOne`
- `@referencesMany`

Register a specific relation.

_This feature has not yet been released in alpha form. Documentation will be
added here as this feature progresses._

0 comments on commit 116c1ad

Please sign in to comment.