Skip to content

Commit

Permalink
docs: add section for Relationship Loading Strategy (#588)
Browse files Browse the repository at this point in the history
related: #440
  • Loading branch information
willsoto authored and B4nan committed Aug 2, 2020
1 parent 03d0ae4 commit bc10e02
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ hide_title: true
- [Updating Entity Values with `Entity.assign()`](entity-helper.md)
- [Property Validation](property-validation.md)
- [Lifecycle Hooks](lifecycle-hooks.md)
- [Loading Strategies](loading-strategies.md)
- [Naming Strategy](naming-strategy.md)
- [Metadata Providers](metadata-providers.md)
- [Metadata Cache](metadata-cache.md)
Expand Down
72 changes: 72 additions & 0 deletions docs/docs/loading-strategies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
title: Relationship Loading Strategies
sidebar_label: Loading Strategies
---

> SQL only feature
Controls how relationships get loaded when querying. By default, populated relationships
are loaded via the `select-in` strategy. This strategy issues one additional `SELECT`
statement per relation being loaded.

The loading strategy can be specified both at mapping time and when loading entities.

For example, given the following entities:

```typescript
import { Entity, LoadStrategy, OneToMany, ManyToOne } from 'mikro-orm';

@Entity()
export class Author {
@OneToMany(() => Book, b => b.author)
books = new Collection<Book>(this);
}

@Entity()
export class Book {
@ManyToOne()
author: Author;
}
```

The following will issue two SQL statements.
One to load the author and another to load all the books belonging to that author:

```typescript
const author = await orm.em.findOne(Author, 1, ['books']);
```

If we update the `Author.books` mapping to the following:

```typescript
import { Entity, LoadStrategy, OneToMany } from 'mikro-orm';

@Entity()
export class Author {
@OneToMany({
entity: () => Book,
mappedBy: b => b.author,
strategy: LoadStrategy.JOINED,
})
books = new Collection<Book>(this);
}
```

The following will issue **one** SQL statement:

```typescript
const author = await orm.em.findOne(Author, 1, ['books']);
```

You can also specify the load strategy as needed. This will override whatever strategy is declared in the mapping.
This also works for nested populates:

```typescript
// one level
const author = await orm.em.findOne(Author, 1, { populate: { books: LoadStrategy.JOINED } });

// two levels
const author = await orm.em.findOne(Author, 1, { populate: {
books: [LoadStrategy.JOINED, { publisher: LoadStrategy.JOINED }]
} });
```
1 change: 1 addition & 0 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
'entity-helper',
'property-validation',
'lifecycle-hooks',
'loading-strategies',
'naming-strategy',
'custom-types',
'entity-schema',
Expand Down

0 comments on commit bc10e02

Please sign in to comment.