Skip to content

Commit b7dfd3f

Browse files
committed
support nil data
1 parent e8cc0d1 commit b7dfd3f

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

Sources/FluentPostgreSQL/PostgreSQLDatabase+QuerySupporting.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ extension PostgreSQLDatabase: QuerySupporting {
7979
// Run the query
8080
return try connection.query(sqlString, parameters) { row in
8181
let codableDict = row.mapValues { psqlData -> DecodableData in
82-
return .single({ PostgreSQLDataDecoder(data: psqlData) })
82+
if psqlData.data == nil {
83+
return .null
84+
} else {
85+
return .single({ PostgreSQLDataDecoder(data: psqlData) })
86+
}
8387
}
8488
do {
8589
let decoded = try CodableDataDecoder().decode(D.self, from: .dictionary(codableDict))

Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ class FluentPostgreSQLTests: XCTestCase {
4343
}
4444

4545
func testNestedStruct() throws {
46-
let conn = try database.makeConnection(using: .init(), on: eventLoop).await(on: eventLoop)
47-
try User.prepare(on: conn).await(on: eventLoop)
46+
let conn = try! database.makeConnection(using: .init(), on: eventLoop).await(on: eventLoop)
47+
try! User.prepare(on: conn).await(on: eventLoop)
4848

49-
let user = try User(id: nil, name: "Tanner", pet: Pet(name: "Zizek"))
49+
let user = try! User(id: nil, name: "Tanner", pet: Pet(name: "Zizek"))
5050
.save(on: conn).await(on: eventLoop)
5151

52-
let fetched = try User.query(on: conn).first().await(on: eventLoop)
52+
let fetched = try! User.query(on: conn).first().await(on: eventLoop)
5353

5454
XCTAssertEqual(user.id, fetched?.id)
55-
try User.revert(on: conn).await(on: eventLoop)
55+
try! User.revert(on: conn).await(on: eventLoop)
5656
conn.close()
5757
}
5858

@@ -75,6 +75,7 @@ final class User: PostgreSQLModel, Migration {
7575
static let idKey = \User.id
7676
var id: Int?
7777
var name: String
78+
var age: Int?
7879
var pet: Pet
7980

8081
init(id: Int? = nil, name: String, pet: Pet) {

0 commit comments

Comments
 (0)