Skip to content

Commit 3279fe6

Browse files
committed
Document throwing accessors for records and record collections
1 parent e540a21 commit 3279fe6

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Documentation/AssociationsBasics.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,26 +2186,25 @@ struct BookInfo: FetchableRecord {
21862186

21872187
init(row: Row) throws {
21882188
book = try Book(row: row)
2189-
author = row["author"]
2190-
country = row["country"]
2191-
coverImage = row["coverImage"]
2189+
author = try row.decode(forKey: "author")
2190+
country = try row.decodeIfPresent(forKey: "country")
2191+
coverImage = try row.decodeIfPresent(forKey: "coverImage")
21922192
}
21932193
}
21942194

21952195
let bookInfos: [BookInfo] = try BookInfo.fetchAll(db, request)
21962196
```
21972197

2198-
You are already familiar with row subscripts to decode [database values](../README.md#column-values):
2198+
When you extract a record from a row, GRDB looks up the tree of **association keys**:
21992199

22002200
```swift
2201-
let name: String = row["name"]
2201+
let author = try row.decode(Author.self, forKey: "author")
22022202
```
22032203

2204-
When you extract a record instead of a value from a row, GRDB looks up the tree of **association keys**. If the key is not found, or only associated with columns that all contain NULL values, an optional record is decoded as nil:
2204+
If the key is not found, or only associated with columns that are all NULL, an optional record is decoded as nil:
22052205

22062206
```swift
2207-
let author: Author = row["author"]
2208-
let country: Country? = row["country"]
2207+
let country = try row.decodeIfPresent(Country.self, forKey: "country")
22092208
```
22102209

22112210
You can also perform custom navigation in the tree by using *row scopes*. See [Row Adapters] for more information.
@@ -2221,7 +2220,7 @@ struct AuthorInfo: FetchableRecord {
22212220

22222221
init(row: Row) throws {
22232222
author = try Author(row: row)
2224-
books = row["books"]
2223+
books = try row.decode(forKey: "books")
22252224
}
22262225
}
22272226

0 commit comments

Comments
 (0)