Skip to content

Commit 3235a8e

Browse files
committed
improve model helpers?
1 parent 43a4fe7 commit 3235a8e

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
public protocol PostgreSQLModel: Model where Self.Database == PostgreSQLDatabase { }
1+
public protocol PostgreSQLModel: Model where Self.Database == PostgreSQLDatabase, Self.ID == Int {
2+
/// This model's unique identifier.
3+
var id: Int? { get set }
4+
}
25

36
extension PostgreSQLModel {
47
/// See `Model.Database`
58
public typealias Database = PostgreSQLDatabase
9+
10+
/// See `Model.ID`
11+
public typealias ID = Int
12+
13+
/// See `Model.idKey`
14+
public static var idKey: IDKey { return \.id }
615
}
16+
17+
public protocol PostgreSQLPivot: Pivot, PostgreSQLModel { }

Sources/FluentPostgreSQL/PostgreSQLPivot.swift

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Foundation
2+
3+
public protocol PostgreSQLUUIDModel: Model where Self.Database == PostgreSQLDatabase, Self.ID == UUID {
4+
/// This model's unique identifier.
5+
var id: UUID? { get set }
6+
}
7+
8+
extension PostgreSQLUUIDModel {
9+
/// See `Model.Database`
10+
public typealias Database = PostgreSQLDatabase
11+
12+
/// See `Model.ID`
13+
public typealias ID = UUID
14+
15+
/// See `Model.idKey`
16+
public static var idKey: IDKey { return \.id }
17+
}
18+
19+
public protocol PostgreSQLUUIDPivot: Pivot, PostgreSQLUUIDModel { }

Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ class FluentPostgreSQLTests: XCTestCase {
8484
try benchmarker.benchmarkIndexSupporting_withSchema()
8585
}
8686

87+
func testMinimumViableModelDeclaration() throws {
88+
/// NOTE: these must never fail to build
89+
struct Foo: PostgreSQLModel {
90+
var id: Int?
91+
var name: String
92+
}
93+
final class Bar: PostgreSQLModel {
94+
var id: Int?
95+
var name: String
96+
}
97+
struct Baz: PostgreSQLUUIDModel {
98+
var id: UUID?
99+
var name: String
100+
}
101+
final class Qux: PostgreSQLUUIDModel {
102+
var id: UUID?
103+
var name: String
104+
}
105+
}
106+
87107
static let allTests = [
88108
("testSchema", testSchema),
89109
("testModels", testModels),
@@ -97,6 +117,7 @@ class FluentPostgreSQLTests: XCTestCase {
97117
("testSoftDeletable", testSoftDeletable),
98118
("testReferentialActions", testReferentialActions),
99119
("testIndexSupporting", testIndexSupporting),
120+
("testMinimumViableModelDeclaration", testMinimumViableModelDeclaration),
100121
]
101122
}
102123

0 commit comments

Comments
 (0)