@@ -81,17 +81,14 @@ await withTaskGroup(of: Void.self) { taskGroup in
8181Use string interpolation to safely execute queries with parameters:
8282
8383``` swift
84- // Simple SELECT query
84+ // Simple SELECT query - explicitly request only the columns you need
8585let minAge = 21
8686let rows = try await client.query (
87- " SELECT * FROM users WHERE age > \( minAge ) " ,
87+ " SELECT id, name FROM users WHERE age > \( minAge ) " ,
8888 logger : logger
8989)
9090
91- for try await row in rows {
92- let randomAccessRow = row.makeRandomAccess ()
93- let id: Int = try randomAccessRow.decode (column : " id" , as : Int .self , context : .default )
94- let name: String = try randomAccessRow.decode (column : " name" , as : String .self , context : .default )
91+ for try await (id, name) in rows.decode ((Int , String ).self , context : .default ) {
9592 print (" User: \( name ) (ID: \( id ) )" )
9693}
9794
@@ -159,7 +156,7 @@ try await client.withTransaction { connection in
159156
160157### 5. Using withConnection for Multiple Queries
161158
162- Execute multiple queries on the same connection for better performance :
159+ Execute multiple queries on a single connection when you need to ensure they run on the same connection :
163160
164161``` swift
165162try await client.withConnection { connection in
@@ -184,8 +181,8 @@ For more details, see <doc:running-queries>.
184181Many Swift types already work out of the box. For custom types, implement `` PostgresEncodable `` and `` PostgresDecodable `` :
185182
186183``` swift
187- // Store complex data as JSONB
188- struct UserProfile : Codable {
184+ // Store complex data as JSONB - conform to PostgresCodable for encoding/decoding
185+ struct UserProfile : Codable , PostgresCodable {
189186 let displayName: String
190187 let bio: String
191188 let interests: [String ]
@@ -209,9 +206,7 @@ let rows = try await client.query(
209206 logger : logger
210207)
211208
212- for try await row in rows {
213- let randomAccessRow = row.makeRandomAccess ()
214- let profile = try randomAccessRow.decode (column : " profile" , as : UserProfile.self , context : .default )
209+ for try await (profile,) in rows.decode (UserProfile.self , context : .default ) {
215210 print (" Display name: \( profile.displayName ) " )
216211}
217212```
0 commit comments