Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Sources/SQLiteData/Documentation.docc/Articles/Fetching.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,39 @@ and its value names each section:
var reminders
```

Sections are not limited to `@FetchAll`. Any query can be fetched into a
``ResultsSectionCollection`` directly, which is useful when defining a custom
``FetchKeyRequest``:

```swift
struct RemindersByCategory: FetchKeyRequest {
func fetch(_ db: Database) throws -> ResultsSectionCollection<Reminder, String?> {
try Reminder
.order(by: \.title)
.fetchAll(db, sectionBy: { $0.category })
}
}
```

The request can be observed with ``Fetch``, and its sections drive a list just like
``FetchAll/sections`` does:

```swift
@Fetch(RemindersByCategory()) var sections = ResultsSectionCollection<Reminder, String?>()
```

And while `@FetchAll` always names its sections with strings, this form can section by any
`Hashable` value the database can decode, and supports joins and custom selections:

```swift
try Reminder
.order(by: \.title)
.join(RemindersList.all) { $0.listID.eq($1.id) }
.select { ReminderRow.Columns(title: $0.title, list: $1.name) }
.fetchAll(db, sectionBy: { $1.id })
// ResultsSectionCollection<ReminderRow, Int?>
```

[sq-safe-sql-strings]: https://swiftpackageindex.com/pointfreeco/swift-structured-queries/~/documentation/structuredqueriescore/safesqlstrings
[structured-queries-gh]: https://github.com/pointfreeco/swift-structured-queries
[structured-queries-docs]: https://swiftpackageindex.com/pointfreeco/swift-structured-queries/main/documentation/structuredqueriescore/
Expand Down
Loading