Skip to content

Commit 6c16a9a

Browse files
committed
Document the find method
1 parent 726022d commit 6c16a9a

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ See [Records](#records)
250250

251251
```swift
252252
try dbQueue.read { db in
253-
// Place?
254-
let paris = try Place.fetchOne(db, id: 1)
253+
// Place
254+
let paris = try Place.find(db, id: 1)
255255

256256
// Place?
257257
let berlin = try Place.filter(Column("title") == "Berlin").fetchOne(db)
@@ -2517,6 +2517,7 @@ let bestPlayers = try Player // [Player]
25172517
.fetchAll(db)
25182518

25192519
let spain = try Country.fetchOne(db, id: "ES") // Country?
2520+
let italy = try Country.find(db, id: "IT") // Country
25202521
```
25212522

25222523
:point_right: Fetching from raw SQL is available for subclasses of the [Record](#record-class) class, and types that adopt the [FetchableRecord] protocol.
@@ -3278,9 +3279,10 @@ struct Player: Identifiable, FetchableRecord, PersistableRecord {
32783279
When `id` has a [database-compatible type](#values) (Int64, Int, String, UUID, ...), the `Identifiable` conformance unlocks type-safe record and request methods:
32793280

32803281
```swift
3281-
let player = try Player.fetchOne(db, id: 1)
3282-
let players = try Player.fetchAll(db, ids: [1, 2, 3])
3283-
let players = try Player.fetchSet(db, ids: [1, 2, 3])
3282+
let player = try Player.find(db, id: 1) // Player
3283+
let player = try Player.fetchOne(db, id: 1) // Player?
3284+
let players = try Player.fetchAll(db, ids: [1, 2, 3]) // [Player]
3285+
let players = try Player.fetchSet(db, ids: [1, 2, 3]) // Set<Player>
32843286

32853287
let request = Player.filter(id: 1)
32863288
let request = Player.filter(ids: [1, 2, 3])
@@ -4234,6 +4236,8 @@ This is the list of record methods, along with their required protocols. The [Re
42344236
| `Type.fetchOne(db, sql: sql)` | [FetchableRecord] | <a href="#list-of-record-methods-3">³</a> |
42354237
| `Type.fetchOne(statement)` | [FetchableRecord] | <a href="#list-of-record-methods-4">⁴</a> |
42364238
| `Type.filter(...).fetchOne(db)` | [FetchableRecord] & [TableRecord] | <a href="#list-of-record-methods-2">²</a> |
4239+
| `Type.find(db, key:...)` | [FetchableRecord] & [TableRecord] | <a href="#list-of-record-methods-1">¹</a> |
4240+
| `Type.find(db, id:...)` | [FetchableRecord] & [TableRecord] & [Identifiable] | <a href="#list-of-record-methods-1">¹</a> |
42374241
| **[Codable Records]** | | |
42384242
| `Type.databaseDecodingUserInfo` | [FetchableRecord] | [*](#the-userinfo-dictionary) |
42394243
| `Type.databaseJSONDecoder(for:)` | [FetchableRecord] | [*](#json-columns) |
@@ -5293,16 +5297,18 @@ You can also change the request so that it knows the type it has to fetch:
52935297

52945298
**Fetching records according to their primary key** is a common task.
52955299

5296-
[Identifiable Records] can use the type-safe methods `fetchOne(_:id:)`, `fetchAll(_:ids:)` and `fetchSet(_:ids:)`:
5300+
[Identifiable Records] can use the type-safe methods `find(_:id:)`, `fetchOne(_:id:)`, `fetchAll(_:ids:)` and `fetchSet(_:ids:)`:
52975301

52985302
```swift
5303+
try Player.find(db, id: 1) // Player
52995304
try Player.fetchOne(db, id: 1) // Player?
53005305
try Country.fetchAll(db, ids: ["FR", "US"]) // [Countries]
53015306
```
53025307

5303-
All record types can use `fetchOne(_:key:)`, `fetchAll(_:keys:)` and `fetchSet(_:keys:)` that apply conditions on primary and unique keys:
5308+
All record types can use `find(_:key:)`, `fetchOne(_:key:)`, `fetchAll(_:keys:)` and `fetchSet(_:keys:)` that apply conditions on primary and unique keys:
53045309

53055310
```swift
5311+
try Player.find(db, key: 1) // Player
53065312
try Player.fetchOne(db, key: 1) // Player?
53075313
try Country.fetchAll(db, keys: ["FR", "US"]) // [Country]
53085314
try Player.fetchOne(db, key: ["email": "[email protected]"]) // Player?
@@ -6393,7 +6399,7 @@ Each DatabaseError has two codes: an `extendedResultCode` (see [extended result
63936399

63946400
### RecordError
63956401

6396-
**RecordError** is thrown by the [PersistableRecord] protocol, in a single case: when the `update` method could not find any row to update:
6402+
**RecordError** is thrown by the [PersistableRecord] protocol when the `update` method could not find any row to update:
63976403

63986404
```swift
63996405
do {
@@ -6403,6 +6409,16 @@ do {
64036409
}
64046410
```
64056411

6412+
**RecordError** is also thrown by the [FetchableRecord] protocol when the `find` method does not find any record:
6413+
6414+
```swift
6415+
do {
6416+
let player = try Player.find(db, id: 42)
6417+
} catch let RecordError.recordNotFound(databaseTableName: table, key: key) {
6418+
print("Key \(key) was not found in table \(table).")
6419+
}
6420+
```
6421+
64066422

64076423
### Fatal Errors
64086424

0 commit comments

Comments
 (0)