@@ -189,6 +189,32 @@ extension FetchableRecord where Self: TableRecord {
189
189
}
190
190
return try filter ( key: key) . fetchOne ( db)
191
191
}
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
+ }
192
218
}
193
219
194
220
@available ( OSX 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , watchOS 6 , * )
@@ -278,6 +304,29 @@ extension FetchableRecord where Self: TableRecord & Identifiable, ID: DatabaseVa
278
304
public static func fetchOne( _ db: Database , id: ID ) throws -> Self ? {
279
305
try filter ( id: id) . fetchOne ( db)
280
306
}
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
+ }
281
330
}
282
331
283
332
extension FetchableRecord where Self: TableRecord & Hashable {
@@ -431,6 +480,32 @@ extension FetchableRecord where Self: TableRecord {
431
480
}
432
481
return try filter ( key: key) . fetchOne ( db)
433
482
}
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
+ }
434
509
}
435
510
436
511
extension FetchableRecord where Self: TableRecord & Hashable {
0 commit comments