Skip to content

Commit ce0119d

Browse files
authored
Merge pull request #15 from vapor/beta-2
Beta 2
2 parents 4ffaf9b + b130345 commit ce0119d

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ let package = Package(
1515
.package(url: "https://github.com/vapor/core.git", .exact("3.0.0-beta.1")),
1616

1717
// Swift ORM framework (queries, models, and relations) for building NoSQL and SQL database integrations.
18-
.package(url: "https://github.com/vapor/fluent.git", .exact("3.0.0-beta.1")),
18+
.package(url: "https://github.com/vapor/fluent.git", .exact("3.0.0-beta.2")),
1919

2020
// Pure Swift, async/non-blocking client for PostgreSQL.
21-
.package(url: "https://github.com/vapor/postgresql.git", .exact("1.0.0-beta.1")),
21+
.package(url: "https://github.com/vapor/postgresql.git", .exact("1.0.0-beta.2")),
2222
],
2323
targets: [
2424
.target(name: "FluentPostgreSQL", dependencies: ["Async", "CodableKit", "Fluent", "FluentSQL", "PostgreSQL"]),

Sources/FluentPostgreSQL/PostgreSQLDatabase+QuerySupporting.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,27 @@ extension PostgreSQLDatabase: QuerySupporting {
7777
}
7878

7979
/// See `QuerySupporting.modelEvent`
80-
public static func modelEvent<M>(event: ModelEvent, model: M, on connection: PostgreSQLConnection) -> Future<Void>
80+
public static func modelEvent<M>(event: ModelEvent, model: M, on connection: PostgreSQLConnection) -> Future<M>
8181
where PostgreSQLDatabase == M.Database, M: Model
8282
{
8383
switch event {
8484
case .willCreate:
8585
if M.ID.self == UUID.self {
86+
var model = model
8687
model.fluentID = UUID() as? M.ID
88+
return Future(model)
8789
}
8890
case .didCreate:
8991
if M.ID.self == Int.self {
90-
return connection.simpleQuery("SELECT LASTVAL();").map(to: Void.self) { row in
91-
try! model.fluentID = row[0]["lastval"]?.decode(Int.self) as? M.ID
92+
return connection.simpleQuery("SELECT LASTVAL();").map(to: M.self) { row in
93+
var model = model
94+
try model.fluentID = row[0]["lastval"]?.decode(Int.self) as? M.ID
95+
return model
9296
}
9397
}
9498
default: break
9599
}
96100

97-
return .done
101+
return Future(model)
98102
}
99103
}

Sources/FluentPostgreSQL/PostgreSQLRowEncoder.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
13
internal final class PostgreSQLRowEncoder: Encoder {
24
var codingPath: [CodingKey]
35
var userInfo: [CodingUserInfoKey : Any]
@@ -43,7 +45,7 @@ fileprivate struct PostgreSQLRowKeyedEncodingContainer<K>: KeyedEncodingContaine
4345
self.codingPath = []
4446
}
4547

46-
mutating func encodeNil(forKey key: K) throws { encoder.data[key.stringValue] = PostgreSQLData(type: .void) }
48+
mutating func encodeNil(forKey key: K) throws { encoder.data[key.stringValue] = PostgreSQLData(type: .void, data: nil) }
4749
mutating func encode(_ value: Bool, forKey key: K) throws { encoder.data[key.stringValue] = try value.convertToPostgreSQLData() }
4850
mutating func encode(_ value: Int, forKey key: K) throws { encoder.data[key.stringValue] = try value.convertToPostgreSQLData() }
4951
mutating func encode(_ value: Int16, forKey key: K) throws { encoder.data[key.stringValue] = try value.convertToPostgreSQLData() }
@@ -59,7 +61,18 @@ fileprivate struct PostgreSQLRowKeyedEncodingContainer<K>: KeyedEncodingContaine
5961
mutating func encode(_ value: String, forKey key: K) throws { encoder.data[key.stringValue] = try value.convertToPostgreSQLData() }
6062
mutating func superEncoder() -> Encoder { return encoder }
6163
mutating func superEncoder(forKey key: K) -> Encoder { return encoder }
62-
mutating func encode<T>(_ value: T, forKey key: K) throws where T : Encodable {
64+
mutating func encodeIfPresent<T>(_ value: T?, forKey key: K) throws where T : Encodable {
65+
if let value = value {
66+
try encode(value, forKey: key)
67+
} else {
68+
if let convertibleType = T.self as? PostgreSQLDataCustomConvertible.Type {
69+
encoder.data[key.stringValue] = PostgreSQLData(type: convertibleType.postgreSQLDataType, data: nil)
70+
} else {
71+
try encodeNil(forKey: key)
72+
}
73+
}
74+
}
75+
mutating func encode<T>(_ value: T, forKey key: K) throws where T: Encodable {
6376
guard let convertible = value as? PostgreSQLDataCustomConvertible else {
6477
let type = Swift.type(of: value)
6578
throw PostgreSQLError(

Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ class FluentPostgreSQLTests: XCTestCase {
4242
try benchmarker.benchmarkAutoincrement_withSchema()
4343
}
4444

45+
func testCache() throws {
46+
try benchmarker.benchmarkCache_withSchema()
47+
}
48+
49+
func testJoins() throws {
50+
try benchmarker.benchmarkJoins_withSchema()
51+
}
52+
53+
func testSoftDeletable() throws {
54+
try benchmarker.benchmarkSoftDeletable_withSchema()
55+
}
56+
57+
func testReferentialActions() throws {
58+
try benchmarker.benchmarkReferentialActions_withSchema()
59+
}
60+
4561
func testNestedStruct() throws {
4662
/// Swift runtime does not yet support dynamically querying conditional conformance ('Swift.Array<Swift.String>': 'CodableKit.AnyKeyStringDecodable')
4763
return;
@@ -72,6 +88,10 @@ class FluentPostgreSQLTests: XCTestCase {
7288
("testTransactions", testTransactions),
7389
("testChunking", testChunking),
7490
("testAutoincrement", testAutoincrement),
91+
("testCache", testCache),
92+
("testJoins", testJoins),
93+
("testSoftDeletable", testSoftDeletable),
94+
("testReferentialActions", testReferentialActions),
7595
]
7696
}
7797

@@ -80,7 +100,7 @@ struct Pet: PostgreSQLJSONType {
80100
}
81101

82102
final class User: PostgreSQLModel, Migration {
83-
static let idKey = \User.id
103+
static let idKey: WritableKeyPath<User, Int?> = \User.id
84104
var id: Int?
85105
var name: String
86106
var age: Int?

0 commit comments

Comments
 (0)