From 1543ec6e8f72e9833ee3b89a279cabebde07bfcd Mon Sep 17 00:00:00 2001 From: Cue <1493221+cuebit@users.noreply.github.com> Date: Fri, 12 Nov 2021 10:43:16 +0000 Subject: [PATCH 1/2] docs: loading all relationships --- docs/guide/relationships/getting-started.md | 30 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/docs/guide/relationships/getting-started.md b/docs/guide/relationships/getting-started.md index ede4fe1..6e2af00 100644 --- a/docs/guide/relationships/getting-started.md +++ b/docs/guide/relationships/getting-started.md @@ -122,7 +122,7 @@ const users = store.$repo(User).with('posts', (query) => { ### Lazy Relationship Loading -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. +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. ```js const userRepo = store.$repo(User) @@ -142,7 +142,7 @@ userRepo.with('posts', (query) => { }).load(users) ``` -Note that the `load` method will mutate the given models. It would be safer to use the method within a single `computed` method. +Note that the `load` method will mutate the given models, therefore, it is advised to use the method within a single `computed` property. ```js import { mapRepos } from '@vuex-orm/core' @@ -173,6 +173,32 @@ export default { } ``` +### Loading All Relationships + +To load all relationships, you may use the `withAll` and `withAllRecursive` methods. + +The `withAll` method will load all model level relationships. Note that any constraints will be applied to all top-level relationships. + +```js +// Fetch models with all top-level relationships. +store.$repo(User).withAll().get() + +// As above, with a constraint. +store.$repo(User).withAll((query) => { + query.has('posts') +}).get() +``` + +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. + +```js +// Fetch models with relationships recursively. +store.$repo(User).withAllRecursive().get() + +// As above, limiting to 2 levels deep. +store.$repo(User).withAllRecursive(2).get() +``` + ## Inserting Relationships 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: From c3198e8af03661c3f5d385d8f713c0ea3f8f6319 Mon Sep 17 00:00:00 2001 From: Cue <1493221+cuebit@users.noreply.github.com> Date: Mon, 15 Nov 2021 09:17:44 +0000 Subject: [PATCH 2/2] Update docs/guide/relationships/getting-started.md Co-authored-by: Kia King Ishii --- docs/guide/relationships/getting-started.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/guide/relationships/getting-started.md b/docs/guide/relationships/getting-started.md index 6e2af00..6fdd1a3 100644 --- a/docs/guide/relationships/getting-started.md +++ b/docs/guide/relationships/getting-started.md @@ -185,7 +185,9 @@ store.$repo(User).withAll().get() // As above, with a constraint. store.$repo(User).withAll((query) => { - query.has('posts') + // This constraint will apply to all of the relationship User has. For this + // example, all relationship will be sorted by `createdAt` field. + query.orderBy('createdAt') }).get() ```