Update more documentation with the closure syntax

This commit is contained in:
Gwendal Roué committed 2025-05-11 10:57:20 +02:00
1 parent 004968517e
commit 96d04716c2
7 files changed
+35 -21

No files matched your search

@@ -262,11 +262,11 @@ try dbQueue.write { db in
// Use instead:
let player = try Player
.filter(Column("teamId") == 42 && Column("position") == 1)
.filter { $0.teamId == 42 && $0.position == 1 }
.fetchOne(db)
try Player
.filter(Column("name") == "Arthur")
.filter { $0.name == "Arthur" }
.deleteAll(db)
}
```
@@ -59,7 +59,7 @@ You can feed `DatabaseRegionObservation` with any type that conforms to the ``Da
```swift
// Observe the score column of the 'player' table
let observation = DatabaseRegionObservation(
tracking: Player.select(Column("score")))
tracking: Player.select(\.score))
// Observe the 'score' column of the 'player' table
let observation = DatabaseRegionObservation(
@@ -214,7 +214,7 @@ Consider a scenario where you'd like to get a specific Player's row, but only wh
let observation = ValueObservation.tracking(
// Define the tracked database region
// (the score column of the player with id 1)
region: Player.select(Column("score")).filter(id: 1),
region: Player.select(\.score).filter(id: 1),
// Define what to fetch upon such change to the tracked region
// (the player with id 1)
fetch: { db in try Player.fetchOne(db, id: 1) }
@@ -214,7 +214,7 @@ extension Book.Kind: DatabaseValueConvertible { }
// Fetch all novels
let novels = try dbQueue.read { db in
try Book.filter(Column("kind") == Book.Kind.novel).fetchAll(db)
try Book.filter { $0.kind == Book.Kind.novel }.fetchAll(db)
}
```
@@ -330,12 +330,12 @@ try dbQueue.read { db in
// Fetch all authors, ordered by name,
// in a localized case-insensitive fashion
let sortedAuthors: [Author] = try Author.all()
.order(Author.Columns.name.collating(.localizedCaseInsensitiveCompare))
.order { $0.name.collating(.localizedCaseInsensitiveCompare) }
.fetchAll(db)
// Count French authors
let frenchAuthorCount: Int = try Author.all()
.filter(Author.Columns.countryCode == "FR")
.filter { $0.countryCode == "FR" }
.fetchCount(db)
}
```
@@ -351,13 +351,12 @@ Define those methods in extensions of the ``DerivableRequest`` protocol, as belo
extension DerivableRequest<Author> {
/// Order authors by name, in a localized case-insensitive fashion
func orderByName() -> Self {
let name = Author.Columns.name
return order(name.collating(.localizedCaseInsensitiveCompare))
order { $0.name.collating(.localizedCaseInsensitiveCompare) }
}
/// Filters authors from a country
func filter(countryCode: String) -> Self {
filter(Author.Columns.countryCode == countryCode)
filter { $0.countryCode == countryCode }
}
}
@@ -365,13 +364,12 @@ extension DerivableRequest<Author> {
extension DerivableRequest<Book> {
/// Order books by title, in a localized case-insensitive fashion
func orderByTitle() -> Self {
let title = Book.Columns.title
return order(title.collating(.localizedCaseInsensitiveCompare))
order { $0.title.collating(.localizedCaseInsensitiveCompare) }
}
/// Filters books by kind
func filter(kind: Book.Kind) -> Self {
filter(Book.Columns.kind == kind)
filter { $0.kind == kind }
}
}
```
@@ -397,7 +395,7 @@ Extensions to the `DerivableRequest` protocol can not change the type of request
extension QueryInterfaceRequest<Author> {
// Selects authors' name
func selectName() -> QueryInterfaceRequest<String> {
select(Author.Columns.name)
select { $0.name }
}
}
+8 -2
View File
@@ -91,8 +91,14 @@ extension Player: FetchableRecord, PersistableRecord {
// because non-'Sendable' type '[any SQLSelectable]'
// may have shared mutable state
static let databaseSelection: [any SQLSelectable] = [
Column("id"), Column("name"), Column("score")
Columns.id, Columns.name, Columns.score
]
enum Columns {
static let id = Column("id")
static let name = Column("name")
static let score = Column("score")
}
}
```
@@ -101,7 +107,7 @@ extension Player: FetchableRecord, PersistableRecord {
```swift
extension Player: FetchableRecord, PersistableRecord {
static var databaseSelection: [any SQLSelectable] {
[Column("id"), Column("name"), Column("score")]
[Columns.id, Columns.name, Columns.score]
}
}
```
+12 -2
View File
@@ -217,7 +217,12 @@ public protocol TableRecord {
/// struct PartialPlayer: TableRecord {
/// static let databaseTableName = "player"
/// static var databaseSelection: [any SQLSelectable] {
/// [Column("id"), Column("name")]
/// [Columns.id, Columns.name]
/// }
///
/// enum Columns {
/// static let id = Column("id")
/// static let name = Column("name")
/// }
/// }
///
@@ -372,7 +377,12 @@ extension TableRecord {
/// struct PartialPlayer: TableRecord {
/// static let databaseTableName = "player"
/// static var databaseSelection: [any SQLSelectable] {
/// [Column("id"), Column("name")]
/// [Columns.id, Columns.name]
/// }
///
/// enum Columns {
/// static let id = Column("id")
/// static let name = Column("name")
/// }
/// }
///
+3 -3
View File
@@ -820,7 +820,7 @@ extension ValueObservation {
///
/// // Tracks the 'score' column in the 'player' table
/// let observation = ValueObservation.tracking(
/// region: Player.select(Column("score"),
/// region: Player.select(\.score),
/// fetch: { db in ... })
///
/// // Tracks the 'score' column in the 'player' table
@@ -892,7 +892,7 @@ extension ValueObservation {
///
/// // Tracks the 'score' column in the 'player' table
/// let observation = ValueObservation.tracking(
/// regions: [Player.select(Column("score")],
/// regions: [Player.select(\.score)],
/// fetch: { db in ... })
///
/// // Tracks the 'score' column in the 'player' table
@@ -963,7 +963,7 @@ extension ValueObservation {
/// let totalPlayerCount = try Player.fetchCount(db)
///
/// let bestPlayers = try Player
/// .order(Column("score").desc)
/// .order(\.score.desc)
/// .limit(10)
/// .fetchAll(db)
///