Skip to content

Commit baccd01

Browse files
committed
Removed restriction on findOrCreate() so it can be used to create a type if none exist at all.
1 parent 154b73f commit baccd01

File tree

4 files changed

+0
-42
lines changed

4 files changed

+0
-42
lines changed

Sources/SwiftQuery/Fetch+Concurrency.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,6 @@ public extension Query {
182182
isolation: isolated (any ModelActor) = #isolation,
183183
body: () -> T
184184
) throws -> T {
185-
guard predicate != nil else {
186-
throw Error.missingPredicate
187-
}
188185
if let found = try first() {
189186
return found
190187
} else {

Sources/SwiftQuery/Fetch+MainActor.swift

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ public extension Query {
123123
in container: ModelContainer,
124124
body: () -> T
125125
) throws -> T {
126-
guard predicate != nil else {
127-
throw Error.missingPredicate
128-
}
129126
guard let found = try first(in: container) else {
130127
let created = body()
131128
container.mainContext.insert(created)
@@ -150,18 +147,3 @@ public extension Query {
150147
}
151148
}
152149
}
153-
154-
public extension Query {
155-
/// Errors that can occur during query execution.
156-
enum Error: Swift.Error, LocalizedError {
157-
/// Thrown when `findOrCreate` is called on a query without a predicate.
158-
case missingPredicate
159-
160-
public var errorDescription: String? {
161-
switch self {
162-
case .missingPredicate:
163-
return "Cannot use findOrCreate without a predicate. Add include() or exclude() to specify which object to find."
164-
}
165-
}
166-
}
167-
}

Tests/SwiftQueryTests/ConcurrentFetchTests.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,6 @@ struct ConcurrentFetchTests {
196196
}
197197
}
198198

199-
@Test func findOrCreate_failsWithoutPredicate() async throws {
200-
_ = await modelContainer.createQueryActor().perform { actor in
201-
#expect(throws: Query<Person>.Error.missingPredicate, performing: {
202-
try Person
203-
.findOrCreate(
204-
isolation: actor,
205-
body: { Person(name: "Ramona", age: 99) }
206-
)
207-
})
208-
}
209-
}
210-
211199
@Test func findOrCreate_finds() async throws {
212200
let foundAge = try await modelContainer.createQueryActor().perform { _ in
213201
let person = try Person

Tests/SwiftQueryTests/FetchTests.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,6 @@ struct FetchTests {
165165
#expect(results.last?.name == "Jack")
166166
}
167167

168-
@Test func findOrCreate_failsWithoutPredicate() throws {
169-
#expect(throws: Query<Person>.Error.missingPredicate, performing: {
170-
try Person
171-
.findOrCreate(in: modelContainer) {
172-
Person(name: "Ramona", age: 99)
173-
}
174-
})
175-
}
176-
177168
@Test func findOrCreate_finds() throws {
178169
let ramona = try Person
179170
.include(#Predicate { $0.name == "Ramona" })

0 commit comments

Comments
 (0)