Skip to content

Commit 2535037

Browse files
authored
add eagerLoaded property to relations (#109)
1 parent e77647e commit 2535037

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed

Sources/FluentBenchmark/FluentBenchmarker.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ public final class FluentBenchmarker {
145145
for galaxy in galaxies {
146146
switch galaxy.name {
147147
case "Milky Way":
148-
guard try galaxy.$planets.eagerLoaded().contains(where: { $0.name == "Earth" }) else {
148+
guard galaxy.planets.contains(where: { $0.name == "Earth" }) else {
149149
throw Failure("unexpected missing planet")
150150
}
151-
guard try !galaxy.$planets.eagerLoaded().contains(where: { $0.name == "PA-99-N2"}) else {
151+
guard !galaxy.planets.contains(where: { $0.name == "PA-99-N2"}) else {
152152
throw Failure("unexpected planet")
153153
}
154154
default: break

Sources/FluentKit/Properties/Children.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public final class Children<From, To>: AnyProperty
8181

8282

8383
extension Children: EagerLoadable {
84+
public var eagerLoaded: [To]? {
85+
self.eagerLoadedValue
86+
}
87+
8488
public func eagerLoad<Model>(to builder: QueryBuilder<Model>)
8589
where Model: FluentKit.Model
8690
{
@@ -103,13 +107,6 @@ extension Children: AnyEagerLoadable {
103107
return self.eagerLoadedValue
104108
}
105109

106-
public func eagerLoaded() throws -> [To] {
107-
guard let rows = self.eagerLoadedValue else {
108-
throw FluentError.missingEagerLoad(name: To.schema.self)
109-
}
110-
return rows
111-
}
112-
113110
func eagerLoad(from eagerLoads: EagerLoads) throws {
114111
guard let request = eagerLoads.requests[self.eagerLoadKey] else {
115112
return

Sources/FluentKit/Properties/Parent+EagerLoad.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ extension Parent: AnyEagerLoadable {
2121
}
2222

2323
extension Parent: EagerLoadable {
24+
public var eagerLoaded: To? {
25+
self.eagerLoadedValue
26+
}
27+
2428
public func eagerLoad<Model>(to builder: QueryBuilder<Model>)
2529
where Model: FluentKit.Model
2630
{
@@ -59,6 +63,10 @@ extension OptionalParent: AnyEagerLoadable {
5963
}
6064

6165
extension OptionalParent: EagerLoadable {
66+
public var eagerLoaded: To? {
67+
self.eagerLoadedValue
68+
}
69+
6270
public func eagerLoad<Model>(to builder: QueryBuilder<Model>)
6371
where Model: FluentKit.Model
6472
{

Sources/FluentKit/Properties/Property.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ extension AnyField where Self: FieldRepresentable {
3535
}
3636

3737
public protocol EagerLoadable {
38+
associatedtype EagerLoadValue
39+
var eagerLoaded: EagerLoadValue? { get }
3840
func eagerLoad<Model>(to builder: QueryBuilder<Model>)
3941
where Model: FluentKit.Model
4042
}

Sources/FluentKit/Properties/Siblings.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public final class Siblings<From, To, Through>: AnyProperty
101101

102102

103103
extension Siblings: EagerLoadable {
104+
public var eagerLoaded: [To]? {
105+
self.eagerLoadedValue
106+
}
107+
104108
public func eagerLoad<Model>(to builder: QueryBuilder<Model>)
105109
where Model: FluentKit.Model
106110
{
@@ -121,13 +125,6 @@ extension Siblings: AnyEagerLoadable {
121125
return self.eagerLoadedValue
122126
}
123127

124-
public func eagerLoaded() throws -> [To] {
125-
guard let rows = self.eagerLoadedValue else {
126-
throw FluentError.missingEagerLoad(name: To.schema.self)
127-
}
128-
return rows
129-
}
130-
131128
func eagerLoad(from eagerLoads: EagerLoads) throws {
132129
guard let request = eagerLoads.requests[self.eagerLoadKey] else {
133130
return

0 commit comments

Comments
 (0)