Skip to content

Eager-loading of associations without using TableInfo structs #1084

@tcwalther

Description

@tcwalther

I understand that GRDB doesn't have lazy-loading associations, which I think is a great design choice. However, I find the concept of writing AuthorInfo structs (name is an example) not that elegant. When I use such *Info structs, it feels that my code is very closely tied to my database structure, and that I am moving away from my business domain.

I wonder whether it would be possible to eager-load associations via optionals. In particular, I propose the following structure:

struct Author {
    var id: Int64
    var name: String
    var books: [Book]?
    static let books = hasMany(Book.self)
}

struct Book { ... }

The books variable is an optional that is nil by default. If, however, I fetch a record via Author.including(all: Author.books), the books variable becomes a list.

That way, in my code, I can simply check the optional to know whether I eager-loaded the associations. And then I can still decide to fetch the association if I feel like it, but it will be explicit. And it would remove a lot of authorInfo.author in my code.

As always, thanks in advance for your help :-).

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions