Skip to content

Commit

Permalink
Merge 8d7bc26 into 1fd35f4
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Jan 10, 2019
2 parents 1fd35f4 + 8d7bc26 commit 3f42961
Showing 1 changed file with 128 additions and 13 deletions.
141 changes: 128 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,137 @@ 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(targetResolver: EntityResolver<T>, definition?: Partial<BelongsToDefinition>)`

Many-to-one or one-to-one connection between models e.g. a `Todo` model belongs
to a `TodoList` model. 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

Syntax:
`@hasOne(targetResolver: EntityResolver<T>, definition?: Partial<HasOneDefinition>)`

One-to-one connection between models e.g. a `TodoList` model has one
`TodoListImage` model. 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

Syntax:
`@hasMany(targetResolver: EntityResolver<T>, definition?: Partial<HasManyDefinition>)`

One-to-many connection between models e.g. a `TodoList` model has many of the
`Todo` model. 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).

- `@belongsTo`
- `@hasOne`
- `@hasMany`
- `@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 3f42961

Please sign in to comment.