vapor / fluent

Vapor ORM (queries, models, and relations) for NoSQL and SQL databases
https://docs.vapor.codes/4.0/fluent/overview/
MIT License
1.3k stars 171 forks source link

Concurrency Swift 5.7 - Any plans to include AsyncMap #754

Closed CristianCardosoA closed 1 year ago

CristianCardosoA commented 1 year ago

Hello, I'm trying to create a query to obtain "courses" table and "sections", but I need to call map $sections.load(database) to obtain those records.

Is there any option to achieve this instead of calling map with load() ?

Note: asyncMap is a custom extension, otherwise I need to change return type to: [EventLoopFuture<[Course]>]

struct CourseRepository {

    let database: Database

    func getCourseByLang(lang: Language) async throws -> [Course] {
        let courses = try await Course.query(on: database)
            .filter(\.$lang == lang.rawValue)
            .all()

          //NEED TO DO THIS TO GET CHILD
        _ = try await courses.asyncMap { course in
            try await course.$sections.load(on: database)
        }

        return courses
    }
}

Versions:

.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
.package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"),
.package(url: "https://github.com/vapor/fluent-mysql-driver.git", from: "4.0.0"),
0xTim commented 1 year ago

Why not just eager load the children?

CristianCardosoA commented 1 year ago

I would need to do this instead:

for course in courses {
     try await course.$sections.load(on: database)
}
CristianCardosoA commented 1 year ago

let courses = try await Course.query(on: database) .filter(\.$lang == lang.rawValue) .sort(\.$title) .with(\.$sections) .all()