|
| 1 | +# Adopting the new PostgresRow cell API |
| 2 | + |
| 3 | +This article describes how to adopt the new ``PostgresRow`` cell APIs in existing Postgres code |
| 4 | +which use the ``PostgresRow/column(_:)`` API today. |
| 5 | + |
| 6 | +## TLDR |
| 7 | + |
| 8 | +1. Map your sequence of ``PostgresRow``s to ``PostgresRandomAccessRow``s. |
| 9 | +2. Use the ``PostgresRandomAccessRow/subscript(name:)`` API to receive a ``PostgresCell`` |
| 10 | +3. Decode the ``PostgresCell`` into a Swift type using the ``PostgresCell/decode(_:file:line:)`` method. |
| 11 | + |
| 12 | +```swift |
| 13 | +let rows: [PostgresRow] // your existing return value |
| 14 | +for row in rows.map({ PostgresRandomAccessRow($0) }) { |
| 15 | + let id = try row["id"].decode(UUID.self) |
| 16 | + let name = try row["name"].decode(String.self) |
| 17 | + let email = try row["email"].decode(String.self) |
| 18 | + let age = try row["age"].decode(Int.self) |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +## Overview |
| 23 | + |
| 24 | +When Postgres [`1.9.0`] was released we changed the default behaviour of ``PostgresRow``s. |
| 25 | +Previously for each row we created an internal lookup table, that allowed you to access the rows' |
| 26 | +cells by name: |
| 27 | + |
| 28 | +```swift |
| 29 | +connection.query("SELECT id, name, email, age FROM users").whenComplete { |
| 30 | + switch $0 { |
| 31 | + case .success(let result): |
| 32 | + for row in result.rows { |
| 33 | + let id = row.column("id").uuid |
| 34 | + let name = row.column("name").string |
| 35 | + let email = row.column("email").string |
| 36 | + let age = row.column("age").int |
| 37 | + // do further processing |
| 38 | + } |
| 39 | + case .failure(let error): |
| 40 | + // handle the error |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +During the last year we introduced a new API that let's you consume ``PostgresRow`` by iterating |
| 46 | +its cells. This approach has the performance benefit of not needing to create an internal cell |
| 47 | +lookup table for each row: |
| 48 | + |
| 49 | +```swift |
| 50 | +connection.query("SELECT id, name, email, age FROM users").whenComplete { |
| 51 | + switch $0 { |
| 52 | + case .success(let result): |
| 53 | + for row in result.rows { |
| 54 | + let (id, name, email, age) = try row.decode((UUID, String, String, Int).self) |
| 55 | + // do further processing |
| 56 | + } |
| 57 | + case .failure(let error): |
| 58 | + // handle the error |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +However, since we still supported the ``PostgresRow/column(_:)`` API, which requires a precomputed |
| 64 | +lookup table within the row, users were not seeing any performance benefits. To allow users to |
| 65 | +benefit of the new fastpath, we changed ``PostgresRow``'s behavior: |
| 66 | + |
| 67 | +By default the ``PostgresRow`` does not create an internal lookup table for its cells on creation |
| 68 | +anymore. Because of this, when using the ``PostgresRow/column(_:)`` API, a throwaway lookup table |
| 69 | +needs to be produced on every call. Since this is wasteful we have deprecated this API. Instead we |
| 70 | +allow users now to explicitly opt-in into the cell lookup API by using the new |
| 71 | +``PostgresRandomAccessRow``. |
| 72 | + |
| 73 | +```swift |
| 74 | +connection.query("SELECT id, name, email, age FROM users").whenComplete { |
| 75 | + switch $0 { |
| 76 | + case .success(let result): |
| 77 | + for row in result.rows.map { PostgresRandomAccessRow($0) } { |
| 78 | + let id = try row["id"].decode(UUID.self) |
| 79 | + let name = try row["name"].decode(String.self) |
| 80 | + let email = try row["email"].decode(String.self) |
| 81 | + let age = try row["age"].decode(Int.self) |
| 82 | + // do further processing |
| 83 | + } |
| 84 | + case .failure(let error): |
| 85 | + // handle the error |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +## Topics |
| 91 | + |
| 92 | +### Relevant types |
| 93 | + |
| 94 | +- ``PostgresConnection`` |
| 95 | +- ``PostgresQuery`` |
| 96 | +- ``PostgresBindings`` |
| 97 | +- ``PostgresRow`` |
| 98 | +- ``PostgresRandomAccessRow`` |
| 99 | +- ``PostgresEncodable`` |
| 100 | +- ``PostgresDecodable`` |
| 101 | + |
| 102 | +[`1.9.0`]: https://github.com/vapor/postgres-nio/releases/tag/1.9.0 |
0 commit comments