Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager Loading of HasMany associations #525

Merged
merged 131 commits into from
May 21, 2019
Merged

Eager Loading of HasMany associations #525

merged 131 commits into from
May 21, 2019

Conversation

groue
Copy link
Owner

@groue groue commented May 7, 2019

This PR "completes" the associations feature set by allowing eager loading of HasMany and HasManyThrough associations:

// Setup
struct Author: TableRecord, FetchableRecord, Decodable {
    static let books = hasMany(Book.self)
    var id: Int64
    var name: String
}

struct Book: TableRecord, FetchableRecord, Decodable {
    var id: Int64
    var authorId: Int64
    var title: String
}

// Load all authors along with their books
struct AuthorInfo: FetchableRecord, Decodable {
    var author: Author
    var books: [Book]
}

let request = Author.including(all: Author.books)
let infos: [AuthorInfo] = try AuthorInfo.fetchAll(db, request)

for info in infos {
    print("\(info.author.name) wrote:")
    for book in info.books {
        print("- \(book.title)")
    }
}

@groue groue force-pushed the feature/including-all branch from 31e0b39 to 304fdab Compare May 20, 2019 05:31
@groue groue merged commit e163b91 into GRDB-4.0 May 21, 2019
@groue groue deleted the feature/including-all branch May 21, 2019 05:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants