Skip to content

Commit 4a4ff99

Browse files
committed
FetchableRecord.find() methods
1 parent afc578d commit 4a4ff99

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

GRDB/Record/FetchableRecord+TableRecord.swift

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,32 @@ extension FetchableRecord where Self: TableRecord {
189189
}
190190
return try filter(key: key).fetchOne(db)
191191
}
192+
193+
/// Returns the record identified by a primary key, or throws an error if
194+
/// the record does not exist.
195+
///
196+
/// For example:
197+
///
198+
/// ```swift
199+
/// try dbQueue.read { db in
200+
/// let player = try Player.find(db, key: 123)
201+
/// let country = try Country.find(db, key: "FR")
202+
/// }
203+
/// ```
204+
///
205+
/// - parameters:
206+
/// - db: A database connection.
207+
/// - key: A primary key value.
208+
/// - returns: A record.
209+
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or a
210+
/// ``RecordError/recordNotFound(databaseTableName:key:)`` if the record
211+
/// does not exist in the database.
212+
public static func find(_ db: Database, key: some DatabaseValueConvertible) throws -> Self {
213+
guard let record = try fetchOne(db, key: key) else {
214+
try recordNotFound(db, key: key)
215+
}
216+
return record
217+
}
192218
}
193219

194220
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6, *)
@@ -278,6 +304,29 @@ extension FetchableRecord where Self: TableRecord & Identifiable, ID: DatabaseVa
278304
public static func fetchOne(_ db: Database, id: ID) throws -> Self? {
279305
try filter(id: id).fetchOne(db)
280306
}
307+
308+
/// Returns the record identified by a primary key, or throws an error if
309+
/// the record does not exist.
310+
///
311+
/// For example:
312+
///
313+
/// ```swift
314+
/// try dbQueue.read { db in
315+
/// let player = try Player.find(db, id: 123)
316+
/// let country = try Country.find(db, id: "FR")
317+
/// }
318+
/// ```
319+
///
320+
/// - parameters:
321+
/// - db: A database connection.
322+
/// - id: A primary key value.
323+
/// - returns: A record.
324+
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or a
325+
/// ``RecordError/recordNotFound(databaseTableName:key:)`` if the record
326+
/// does not exist in the database.
327+
public static func find(_ db: Database, id: ID) throws -> Self {
328+
try find(db, key: id)
329+
}
281330
}
282331

283332
extension FetchableRecord where Self: TableRecord & Hashable {
@@ -431,6 +480,32 @@ extension FetchableRecord where Self: TableRecord {
431480
}
432481
return try filter(key: key).fetchOne(db)
433482
}
483+
484+
/// Returns the record identified by a unique key (the primary key or
485+
/// any key with a unique index on it), or throws an error if the record
486+
/// does not exist.
487+
///
488+
/// For example:
489+
///
490+
/// ```swift
491+
/// try dbQueue.read { db in
492+
/// let player = try Player.find(db, key: ["name": "Arthur"])
493+
/// }
494+
/// ```
495+
///
496+
/// - parameters:
497+
/// - db: A database connection.
498+
/// - key: A key dictionary.
499+
/// - returns: A record.
500+
/// - throws: A ``DatabaseError`` whenever an SQLite error occurs, or a
501+
/// ``RecordError/recordNotFound(databaseTableName:key:)`` if the record
502+
/// does not exist in the database.
503+
public static func find(_ db: Database, key: [String: (any DatabaseValueConvertible)?]) throws -> Self {
504+
guard let record = try filter(key: key).fetchOne(db) else {
505+
try recordNotFound(key: key)
506+
}
507+
return record
508+
}
434509
}
435510

436511
extension FetchableRecord where Self: TableRecord & Hashable {

GRDB/Record/FetchableRecord.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ import Foundation
7575
/// - ``fetchAll(_:keys:)-4c8no``
7676
/// - ``fetchSet(_:keys:)-e6uy``
7777
/// - ``fetchOne(_:key:)-3f3hc``
78+
/// - ``find(_:id:)``
79+
/// - ``find(_:key:)-4kry5``
80+
/// - ``find(_:key:)-1dfbe``
7881
///
7982
/// ### Fetching Record by Key
8083
///

0 commit comments

Comments
 (0)