diff --git a/GRDB/Core/Protocols/DatabaseTableMapping.swift b/GRDB/Core/Protocols/DatabaseTableMapping.swift index a1a004904..4ec2e8b4d 100644 --- a/GRDB/Core/Protocols/DatabaseTableMapping.swift +++ b/GRDB/Core/Protocols/DatabaseTableMapping.swift @@ -9,7 +9,7 @@ /// /// DatabaseTableMapping is adopted by Record. public protocol DatabaseTableMapping { - /// The name of the database table. + /// The name of the database table static func databaseTableName() -> String } diff --git a/README.md b/README.md index 522da871a..8a2b34442 100644 --- a/README.md +++ b/README.md @@ -1496,11 +1496,36 @@ See also the [Record](#records) class, which builds on top of RowConvertible and ```swift public protocol DatabaseTableMapping { - /// The name of the database table. + /// The name of the database table static func databaseTableName() -> String } ``` +For example: + +```swift +// CREATE TABLE persons ( +// id INTEGER PRIMARY KEY, +// name TEXT, +// email TEXT UNIQUE COLLATE NOCASE +// ) +struct Person : RowConvertible, DatabaseTableMapping { + var id: Int64? + var name: String? + var email: String? + + static func databaseTableName() -> String { + return "persons" + } + + init(row: Row) { + id = row.value(named: "id") + name = row.value(named: "name") + email = row.value(named: "email") + } +} +``` + You are then granted with fetching methods based on database keys: ```swift