Skip to content

Commit a1062d1

Browse files
cuebitkiaking
andauthored
docs: loading all relationships (#100)
Co-authored-by: Kia King Ishii <[email protected]>
1 parent 54426a4 commit a1062d1

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

docs/guide/relationships/getting-started.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ const users = store.$repo(User).with('posts', (query) => {
122122

123123
### Lazy Relationship Loading
124124

125-
Sometimes you may need to load a relationship after the model has already been retrieved. For example, this may be useful if you need to dynamically decide whether to load related models. You may use `load` method on a repository to load relationships on the fly in such a case.
125+
Sometimes you may need to load a relationship after the model has already been retrieved. For example, this may be useful if you need to decide whether to load related models dynamically. You may use the `load` method on a repository to load relationships on the fly in such a case.
126126

127127
```js
128128
const userRepo = store.$repo(User)
@@ -142,7 +142,7 @@ userRepo.with('posts', (query) => {
142142
}).load(users)
143143
```
144144

145-
Note that the `load` method will mutate the given models. It would be safer to use the method within a single `computed` method.
145+
Note that the `load` method will mutate the given models, therefore, it is advised to use the method within a single `computed` property.
146146

147147
```js
148148
import { mapRepos } from '@vuex-orm/core'
@@ -173,6 +173,34 @@ export default {
173173
}
174174
```
175175

176+
### Loading All Relationships
177+
178+
To load all relationships, you may use the `withAll` and `withAllRecursive` methods.
179+
180+
The `withAll` method will load all model level relationships. Note that any constraints will be applied to all top-level relationships.
181+
182+
```js
183+
// Fetch models with all top-level relationships.
184+
store.$repo(User).withAll().get()
185+
186+
// As above, with a constraint.
187+
store.$repo(User).withAll((query) => {
188+
// This constraint will apply to all of the relationship User has. For this
189+
// example, all relationship will be sorted by `createdAt` field.
190+
query.orderBy('createdAt')
191+
}).get()
192+
```
193+
194+
The `withAllRecursive` method will load all model level relationships and sub relationships recursively. By default, the maximum recursion depth is 3 when an argument is omitted.
195+
196+
```js
197+
// Fetch models with relationships recursively.
198+
store.$repo(User).withAllRecursive().get()
199+
200+
// As above, limiting to 2 levels deep.
201+
store.$repo(User).withAllRecursive(2).get()
202+
```
203+
176204
## Inserting Relationships
177205

178206
You may use `save` method to save a record with its nested relationships to the store. When saving new records into the store via `save` method, Vuex ORM automatically normalizes and stores data that contains any nested relationships in it's data structure. For example, let's say you have the `User` model that has a relationship to the `Post` model:

0 commit comments

Comments
 (0)