Skip to content

Commit

Permalink
Skip DB execution when all ids for @OptionalParent are nil (#583)
Browse files Browse the repository at this point in the history
Co-authored-by: Gwynne Raskind <[email protected]>
  • Loading branch information
mtj0928 and gwynne authored Mar 23, 2024
1 parent bb47433 commit 5f0938a
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/FluentKit/Properties/OptionalParent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ private struct OptionalParentEagerLoader<From, To>: EagerLoader
var sets = Dictionary(grouping: models, by: { $0[keyPath: self.relationKey].id })
let nilParentModels = sets.removeValue(forKey: nil) ?? []

if sets.isEmpty {
// Fetching "To" objects is unnecessary when no models have an id for "To".
nilParentModels.forEach { $0[keyPath: self.relationKey].value = .some(.none) }
return database.eventLoop.makeSucceededVoidFuture()
}

let builder = To.query(on: database).filter(\._$id ~~ Set(sets.keys.compactMap { $0 }))
if (self.withDeleted) {
builder.withDeleted()
Expand Down

0 comments on commit 5f0938a

Please sign in to comment.