Skip to content

Commit f85b077

Browse files
committed
separate protocol
1 parent 60977b8 commit f85b077

5 files changed

Lines changed: 40 additions & 17 deletions

File tree

Sources/Feather/DatabaseQuery.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
/// A database query that fetches any array of rows.
9-
public struct FetchManyQuery<Input, Output>: Queryable
9+
public struct FetchManyQuery<Input, Output>: DatabaseQuery
1010
where Input: Sendable,
1111
Output: RangeReplaceableCollection & ExpressibleByArrayLiteral & Sendable,
1212
Output.Element: RowDecodable & Sendable
@@ -42,7 +42,7 @@ public struct FetchManyQuery<Input, Output>: Queryable
4242
}
4343

4444
/// A database that fetches a single element. Can return `nil`
45-
public struct FetchSingleQuery<Input, Output>: Queryable
45+
public struct FetchSingleQuery<Input, Output>: DatabaseQuery
4646
where Input: Sendable, Output: RowDecodable & Sendable
4747
{
4848
public let transactionKind: TransactionKind
@@ -72,7 +72,7 @@ public struct FetchSingleQuery<Input, Output>: Queryable
7272
}
7373

7474
/// A query that has no return value.
75-
public struct VoidQuery<Input>: Queryable where Input: Sendable {
75+
public struct VoidQuery<Input>: DatabaseQuery where Input: Sendable {
7676
public typealias Output = ()
7777

7878
public let transactionKind: TransactionKind

Sources/Feather/Queries.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public enum Queries {
1111
/// Allows for the erasing of the database so a query can be
1212
/// passed around and be able to be executed without
1313
/// having the caller worry about by what.
14-
public struct WithDatabase<Base: Queryable>: Queryable {
14+
public struct WithDatabase<Base: DatabaseQuery>: DatabaseQuery, Query {
1515
/// The original query that requires a database
1616
let base: Base
1717
/// The database to execute the query in
@@ -43,10 +43,16 @@ public enum Queries {
4343
) -> QueryObservation<Input, Output> {
4444
return base.observe(with: input, in: database, handle: handle, cancelled: cancelled)
4545
}
46+
47+
public func execute(
48+
with input: Base.Input
49+
) async throws -> Base.Output {
50+
return try await base.execute(with: input, in: database)
51+
}
4652
}
4753

4854
/// Applies a transform to the queries result
49-
public struct Map<Base: Queryable, Output: Sendable>: Queryable {
55+
public struct Map<Base: DatabaseQuery, Output: Sendable>: DatabaseQuery {
5056
/// The upstream query to transform
5157
let base: Base
5258
/// The transform to apply to the output
@@ -72,7 +78,7 @@ public enum Queries {
7278
}
7379

7480
/// Applies a transform to the queries result
75-
public struct Just<Input, Output>: Queryable
81+
public struct Just<Input, Output>: DatabaseQuery
7682
where Input: Sendable, Output: Sendable
7783
{
7884
let output: Output
@@ -100,8 +106,8 @@ public enum Queries {
100106
}
101107
}
102108

103-
public struct Then<First, Second>: Queryable
104-
where First: Queryable, Second: Queryable,
109+
public struct Then<First, Second>: DatabaseQuery
110+
where First: DatabaseQuery, Second: DatabaseQuery,
105111
First.Output == Second.Input
106112
{
107113
let first: First
@@ -124,7 +130,7 @@ public enum Queries {
124130
}
125131
}
126132

127-
public extension Queryable {
133+
public extension DatabaseQuery {
128134
func with(database: any Database) -> Queries.WithDatabase<Self> {
129135
return Queries.WithDatabase(base: self, database: database)
130136
}

Sources/Feather/Query.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Query.swift
3+
// Feather
4+
//
5+
// Created by Wes Wickwire on 3/29/25.
6+
//
7+
8+
public protocol Query<Input, Output> {
9+
associatedtype Input
10+
associatedtype Output
11+
12+
func execute(with input: Input) async throws -> Output
13+
}
14+
15+
extension Query where Input == () {
16+
func execute() async throws -> Output {
17+
return try await execute(with: ())
18+
}
19+
}

Sources/Feather/QueryObservation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
public final class QueryObservation<Input, Output>: DatabaseSubscriber, Sendable
99
where Input: Sendable, Output: Sendable
1010
{
11-
private let query: any Query<Input, Output>
11+
private let query: any DatabaseQuery<Input, Output>
1212
private let input: Input
1313
private let database: any Database
1414
private let handle: @Sendable (Output) -> Void
1515
private let cancelled: @Sendable () -> Void
1616

1717
init(
18-
query: any Query<Input, Output>,
18+
query: any DatabaseQuery<Input, Output>,
1919
input: Input,
2020
database: any Database,
2121
handle: @Sendable @escaping (Output) -> Void,

Sources/Feather/Queryable.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
//
2-
// Queryable.swift
2+
// DatabaseQuery.swift
33
// Feather
44
//
55
// Created by Wes Wickwire on 11/9/24.
66
//
77

8-
public typealias Query<Input, Output> = Queryable<Input, Output>
9-
10-
public protocol Queryable<Input, Output>: Sendable {
8+
public protocol DatabaseQuery<Input, Output>: Sendable {
119
associatedtype Input: Sendable
1210
associatedtype Output: Sendable
1311

@@ -25,7 +23,7 @@ public protocol Queryable<Input, Output>: Sendable {
2523
) throws -> Output
2624
}
2725

28-
public extension Queryable {
26+
public extension DatabaseQuery {
2927
func execute(
3028
with input: Input,
3129
in database: any Database
@@ -70,7 +68,7 @@ public extension Queryable {
7068
}
7169
}
7270

73-
extension Queryable where Input == () {
71+
extension DatabaseQuery where Input == () {
7472
func execute(in database: any Database) async throws -> Output {
7573
return try await execute(with: (), in: database)
7674
}

0 commit comments

Comments
 (0)