|
1 | 1 | extension ModelQuery {
|
2 |
| - /// A closure for defining any nested eager loading when loading a |
3 |
| - /// relationship on this `Model`. |
4 |
| - /// |
5 |
| - /// "Eager loading" refers to loading a model at the other end of |
6 |
| - /// a relationship of this queried model. Nested eager loads |
7 |
| - /// refers to loading a model from a relationship on that |
8 |
| - /// _other_ model. |
9 |
| - public typealias NestedQuery<R: Model> = (ModelQuery<R>) -> ModelQuery<R> |
10 |
| - |
11 | 2 | /// A tuple of models and the SQLRow that they were loaded from.
|
12 | 3 | typealias ModelRow = (model: M, row: SQLRow)
|
13 | 4 |
|
@@ -100,6 +91,15 @@ extension ModelQuery {
|
100 | 91 | }
|
101 | 92 | }
|
102 | 93 |
|
| 94 | +/// A closure for defining any nested eager loading when loading a |
| 95 | +/// relationship on this `Model`. |
| 96 | +/// |
| 97 | +/// "Eager loading" refers to loading a model at the other end of |
| 98 | +/// a relationship of this queried model. Nested eager loads |
| 99 | +/// refers to loading a model from a relationship on that |
| 100 | +/// _other_ model. |
| 101 | +public typealias NestedQuery<R: Model> = (ModelQuery<R>) -> ModelQuery<R> |
| 102 | + |
103 | 103 | extension RelationshipMapping {
|
104 | 104 | fileprivate func load<M: Model>(_ values: [SQLRow], database: Database) throws -> ModelQuery<M> {
|
105 | 105 | var query = M.query(database: database)
|
@@ -132,26 +132,41 @@ extension Array where Element: Hashable {
|
132 | 132 | }
|
133 | 133 | }
|
134 | 134 |
|
135 |
| - |
136 | 135 | // MARK: - Additional Eager Load convience functions
|
137 | 136 |
|
138 | 137 | extension Model {
|
139 |
| - public func with<R: Relationship>(db: Database = DB, _ relationship: KeyPath<Self, R>) async throws -> Self where R.From == Self { |
140 |
| - try await sync(db: db) { $0.with(relationship) } |
| 138 | + public func with<R: Relationship>( |
| 139 | + db: Database = DB, |
| 140 | + _ relationship: KeyPath<Self, R>, |
| 141 | + nested: @escaping NestedQuery<R.To.Value> = { $0 } |
| 142 | + ) async throws -> Self where R.From == Self { |
| 143 | + try await sync(db: db) { $0.with(relationship, nested: nested) } |
141 | 144 | }
|
142 | 145 |
|
143 |
| - public func fetch<R: Relationship>(db: Database = DB, _ relationship: KeyPath<Self, R>) async throws -> R.Wrapped where R.From == Self { |
144 |
| - try await sync(db: db) { $0.with(relationship) }[keyPath: relationship].wrappedValue |
| 146 | + public func fetch<R: Relationship>( |
| 147 | + db: Database = DB, |
| 148 | + _ relationship: KeyPath<Self, R>, |
| 149 | + nested: @escaping NestedQuery<R.To.Value> = { $0 } |
| 150 | + ) async throws -> R.Wrapped where R.From == Self { |
| 151 | + try await sync(db: db) { $0.with(relationship, nested: nested) }[keyPath: relationship].wrappedValue |
145 | 152 | }
|
146 | 153 | }
|
147 | 154 |
|
148 | 155 | extension Array where Element: Model {
|
149 |
| - public func with<R: Relationship>(db: Database = DB, _ relationship: KeyPath<Element, R>) async throws -> Self where R.From == Element { |
150 |
| - try await syncAll(db: db) { $0.with(relationship) } |
| 156 | + public func with<R: Relationship>( |
| 157 | + db: Database = DB, |
| 158 | + _ relationship: KeyPath<Element, R>, |
| 159 | + nested: @escaping NestedQuery<R.To.Value> = { $0 } |
| 160 | + ) async throws -> Self where R.From == Element { |
| 161 | + try await syncAll(db: db) { $0.with(relationship, nested: nested) } |
151 | 162 | }
|
152 | 163 |
|
153 |
| - public func fetch<R: Relationship>(db: Database = DB, _ relationship: KeyPath<Element, R>) async throws -> [R.Wrapped] where R.From == Element { |
154 |
| - try await syncAll(db: db) { $0.with(relationship) } |
| 164 | + public func fetch<R: Relationship>( |
| 165 | + db: Database = DB, |
| 166 | + _ relationship: KeyPath<Element, R>, |
| 167 | + nested: @escaping NestedQuery<R.To.Value> = { $0 } |
| 168 | + ) async throws -> [R.Wrapped] where R.From == Element { |
| 169 | + try await syncAll(db: db) { $0.with(relationship, nested: nested) } |
155 | 170 | .map { $0[keyPath: relationship].wrappedValue }
|
156 | 171 | }
|
157 | 172 | }
|
0 commit comments