Skip to content

Commit afc578d

Browse files
committed
TableRecord.recordNotFound() methods
1 parent df3bd46 commit afc578d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

GRDB/Record/TableRecord.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ import Foundation
3030
/// - ``exists(_:key:)-60hf2``
3131
/// - ``exists(_:key:)-6ha6``
3232
///
33+
/// ### Throwing Record Not Found Errors
34+
///
35+
/// - ``recordNotFound(_:id:)``
36+
/// - ``recordNotFound(_:key:)``
37+
/// - ``recordNotFound(key:)``
38+
///
3339
/// ### Deleting Records
3440
///
3541
/// - ``deleteAll(_:)``
@@ -673,6 +679,31 @@ extension RecordError: CustomStringConvertible {
673679
}
674680
}
675681

682+
extension TableRecord {
683+
/// Throws a `RecordError.recordNotFound` error.
684+
public static func recordNotFound(_ db: Database, key: some DatabaseValueConvertible) throws -> Never {
685+
let primaryKey = try db.primaryKey(databaseTableName)
686+
throw RecordError.recordNotFound(
687+
databaseTableName: databaseTableName,
688+
key: [primaryKey.columns[0]: key.databaseValue])
689+
}
690+
691+
/// Throws a `RecordError.recordNotFound` error.
692+
public static func recordNotFound(key: [String: (any DatabaseValueConvertible)?]) throws -> Never {
693+
throw RecordError.recordNotFound(
694+
databaseTableName: databaseTableName,
695+
key: key.mapValues { $0?.databaseValue ?? .null })
696+
}
697+
}
698+
699+
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6, *)
700+
extension TableRecord where Self: Identifiable, ID: DatabaseValueConvertible {
701+
/// Throws a `RecordError.recordNotFound` error.
702+
public static func recordNotFound(_ db: Database, id: Self.ID) throws -> Never {
703+
try recordNotFound(db, key: id)
704+
}
705+
}
706+
676707
@available(*, deprecated, renamed: "RecordError")
677708
public typealias PersistenceError = RecordError
678709

0 commit comments

Comments
 (0)