-
-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add section for Relationship Loading Strategy (#588)
related: #440
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }] | ||
} }); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters