Stop recommending String enums for Columns.

We now recommend:

    enum Columns {
        static let id = Column("id")
        static let title = Column("title")
        static let latitude = Column("latitude")
        static let longitude = Column("longitude")
    }

Instead of the previous recommendation:

    enum Columns: String, ColumnExpression {
        case id, title, latitude, longitude
    }

This is because SE-0438 does not support Metatype Keypaths for enums. A request like `Place.order(\.title)` does not compile with a String enum.
This commit is contained in:
Gwendal Roué committed 2025-05-10 09:36:14 +02:00
1 parent ed98ac30e0
commit 352f77f225
6 files changed
+51 -29

No files matched your search

+3 -2
View File
@@ -751,8 +751,9 @@ Foreign keys can also be defined from query interface columns:
```swift
struct Book: TableRecord {
enum Columns: String, ColumnExpression {
case id, title, authorId, translatorId
enum Columns {
static let authorId = Column("authorId")
static let translatorId = Column("translatorId")
}
static let authorForeignKey = ForeignKey([Columns.authorId])
@@ -297,7 +297,7 @@ Once we have record types that are able to read and write in the database, we'd
### Columns
Requests that filter or sort records are defined with **columns**, defined in a dedicated enumeration. When the record type conforms to [`Codable`], columns can be derived from the `CodingKeys` enum:
Requests that filter or sort records are defined with **columns**, defined in a dedicated enumeration, with the name `Columns`, nested inside the record type. When the record type conforms to [`Codable`], columns can be derived from the `CodingKeys` enum:
```swift
// HOW TO define columns for a Codable record
@@ -310,13 +310,15 @@ extension Author {
}
```
For other record types, declare a plain `String` enum that conforms to the ``ColumnExpression`` protocol:
For non-Codable record types, declare columns with their names:
```swift
// HOW TO define columns for a non-Codable record
extension Author {
enum Columns: String, ColumnExpression {
case id, name, countryCode
enum Columns {
static let id = Column("id")
static let name = Column("name")
static let countryCode = Column("countryCode")
}
}
```
+4 -2
View File
@@ -59,8 +59,10 @@
/// > extension Player: FetchableRecord, PersistableRecord {
/// > // That's another valid way to define columns.
/// > // But we don't have any JSONColumn this time.
/// > enum Columns: String, ColumnExpression {
/// > case id, name, address
/// > enum Columns {
/// > static let id = Column("id")
/// > static let name = Column("name")
/// > static let address = Column("address")
/// > }
/// > }
/// >
+3 -2
View File
@@ -45,8 +45,9 @@
///
/// ```swift
/// struct Book: TableRecord {
/// enum Columns: String, ColumnExpression {
/// case id, title, authorId, translatorId
/// enum Columns {
/// static let authorId = Column("authorId")
/// static let translatorId = Column("translatorId")
/// }
///
/// static let authorForeignKey = ForeignKey([Columns.authorId]))
+6 -4
View File
@@ -123,11 +123,13 @@ extension Column: ColumnExpression { }
/// Support for column enums:
///
/// struct Player {
/// enum Columns: String, ColumnExpression {
/// case id, name, score
/// }
/// ```swift
/// struct Player {
/// enum Columns: String, ColumnExpression {
/// case id, name, score
/// }
/// }
/// ```
extension ColumnExpression where Self: RawRepresentable, Self.RawValue == String {
public var name: String { rawValue }
}
+29 -15
View File
@@ -1904,8 +1904,11 @@ Rows also accept column enums:
```swift
extension Place : FetchableRecord {
enum Columns: String, ColumnExpression {
case id, title, latitude, longitude
enum Columns {
static let id = Column("id")
static let title = Column("title")
static let latitude = Column("latitude")
static let longitude = Column("longitude")
}
init(row: Row) {
@@ -2070,8 +2073,11 @@ Persistence containers also accept column enums:
```swift
extension Place : MutablePersistableRecord {
enum Columns: String, ColumnExpression {
case id, title, latitude, longitude
enum Columns {
static let id = Column("id")
static let title = Column("title")
static let latitude = Column("latitude")
static let longitude = Column("longitude")
}
func encode(to container: inout PersistenceContainer) {
@@ -3070,8 +3076,12 @@ struct Place {
// SQL generation
extension Place: TableRecord {
/// The table columns
enum Columns: String, ColumnExpression {
case id, title, isFavorite, latitude, longitude
enum Columns {
static let id = Column("id")
static let title = Column("title")
static let isFavorite = Column("isFavorite")
static let latitude = Column("latitude")
static let longitude = Column("longitude")
}
}
@@ -3305,21 +3315,25 @@ let players = try request.fetchAll(db) // [Player]
> **Note**: all examples in the documentation below use a record type, but you can always substitute a `Table` instead.
Next, declare the table **columns** that you want to use for filtering, or sorting:
Next, declare the table **columns** that you want to use for filtering, or sorting, in a nested type named `Columns`:
```swift
let idColumn = Column("id")
let nameColumn = Column("name")
extension Player {
enum Columns {
static let id = Column("id")
static let name = Column("name")
}
}
```
You can also declare column enums, if you prefer:
If `Player` is `Codable`, you'll prefer defining columns from coding keys:
```swift
// Columns.id and Columns.name can be used just as
// idColumn and nameColumn declared above.
enum Columns: String, ColumnExpression {
case id
case name
extension Player {
enum Columns {
static let id = Column(CodingKeys.id)
static let name = Column(CodingKeys.name)
}
}
```