Skip to content

Lazy loading after draft 15 #108

Closed
Closed
@SimonDatapas

Description

@SimonDatapas

For some use-cases in our apps we need to use a lot of relations. For some of those cases we'd like the guarantee of having the relations loaded and made use of a way of lazy loading those relations.

import {Model} from "@vuex-orm/core";
import Post from "./Post";

export default class User extends Model {
    static entity = 'User';

    static fields() {
        return {
            id: this.uid(),
            name: this.string(''),
            posts: this.hasMany(Posts, 'user_id')
        };
    }

    get lazyPosts() {
        if (this.posts === undefined) {
            this._database.store.$repo(User).with('posts').load([this]);
        }

        return this.posts;
    }

    get lazyComments() {
        return this.lazyPosts.reduce((carry, post) => carry.concat(post.lazyComments), []);
    }
}

We are aware that lazy loading brings a bit of overhead and should be avoided by lots of data, but we do have some usecases were we'd still like the guarantee of having the relation loaded or else loading.

How should we deal with these types of situations?
Will this be suported (in the future)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions