From ce4bfb9df6c76890cc252bf196a691e2a70da16c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 23 Aug 2020 11:03:49 +0200 Subject: [PATCH] [skip travis] Explain that date and uuid strategies do not apply to requests (#821) --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/README.md b/README.md index 0f9c8648b..e659609da 100644 --- a/README.md +++ b/README.md @@ -2861,6 +2861,56 @@ See [DatabaseDateDecodingStrategy](https://groue.github.io/GRDB.swift/docs/5.0.0 > :point_up: **Note**: there is no customization of uuid decoding, because UUID can already decode all its encoded variants (16-bytes blobs, and uuid strings). +> :point_up: **Note**: Customized date and uuid handling only apply during the encoding and decoding of database rows to and from records. *They do not apply* when you define requests based on date or uuid values. + +So make sure that dates and uuids are properly encoded in your requests. For example: + +```swift +struct Player: Codable, FetchableRecord, PersistableRecord { + // UUIDs are stored as strings + static let databaseUUIDEncodingStrategy = DatabaseUUIDEncodingStrategy.string + var uuid: UUID + ... +} + +try dbQueue.write { db in + let uuid = UUID() + let player = Player(uuid: uuid, ...) + + // Inserts a player in the database, with a string uuid + try player.insert(db) + + // BAD: performs a blob-based query, fails to find the inserted player + _ = try Player.filter(Column("uuid") == uuid).fetchOne(db) + _ = try Player.filter(key: uuid).fetchOne(db) + + // GOOD: performs a string-based query, finds the inserted player + _ = try Player.filter(Column("uuid") == uuid.uuidString).fetchOne(db) + _ = try Player.filter(key: uuid.uuidString).fetchOne(db) +} +``` + +The [Good Practices for Designing Record Types](Documentation/GoodPracticesForDesigningRecordTypes.md) suggest to define a specific method in such situation: + +```swift +extension DerivableRequest where RowDecoder == Player { + func filter(uuid: UUID) -> Self { + filter(Column("uuid") == uuid.uuidString) + } +} + +try dbQueue.write { db in + let uuid = UUID() + let player = Player(uuid: uuid, ...) + + // Inserts a player in the database, with a string uuid + try player.insert(db) + + // GOOD: performs a string-based query, finds the inserted player + _ = try Player.all().filter(uuid: uuid).fetchOne(db) +} +``` + ### The userInfo Dictionary