Skip to content

Commit 5c3f531

Browse files
committed
then query
1 parent 67ae4d1 commit 5c3f531

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

Sources/Feather/Queries.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ public enum Queries {
9191
}
9292

9393
public struct Then<First, Second>: DatabaseQuery
94-
where First: DatabaseQuery, Second: DatabaseQuery,
95-
First.Output == Second.Input
94+
where First: DatabaseQuery, Second: DatabaseQuery
9695
{
9796
let first: First
9897
let second: Second
98+
let secondInput: @Sendable (First.Input, First.Output) -> Second.Input
9999

100100
public typealias DB = any Database
101101

@@ -108,7 +108,8 @@ public enum Queries {
108108
tx: borrowing Transaction
109109
) throws -> (First.Output, Second.Output) {
110110
let firstOutput = try first.execute(with: input, tx: tx)
111-
let secondOutput = try second.execute(with: firstOutput, tx: tx)
111+
let secondInput = secondInput(input, firstOutput)
112+
let secondOutput = try second.execute(with: secondInput, tx: tx)
112113
return (firstOutput, secondOutput)
113114
}
114115
}
@@ -142,4 +143,19 @@ public extension DatabaseQuery {
142143
return entity ?? value()
143144
}
144145
}
146+
147+
func then<Next>(_ next: Next) -> Queries.Then<Self, Next>
148+
where Next: DatabaseQuery, Self.Input == Next.Input
149+
{
150+
return Queries.Then(first: self, second: next) { input, _ in input }
151+
}
152+
153+
func then<Next>(
154+
_ next: Next,
155+
nextInput: @Sendable @escaping (Output) -> Next.Input
156+
) -> Queries.Then<Self, Next>
157+
where Next: DatabaseQuery
158+
{
159+
return Queries.Then(first: self, second: next) { _, output in nextInput(output) }
160+
}
145161
}

0 commit comments

Comments
 (0)