Skip to content

Commit 1128309

Browse files
committed
add psql string model, fixes #44
1 parent b36bd0b commit 1128309

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ let package = Package(
1212
.package(url: "https://github.com/vapor/core.git", .branch("fluent-gm")),
1313

1414
// Swift ORM framework (queries, models, and relations) for building NoSQL and SQL database integrations.
15-
.package(url: "https://github.com/vapor/fluent.git", .branch("gm")),
15+
.package(url: "https://github.com/vapor/fluent.git", .branch("master")),
1616

1717
// 🐘 Non-blocking, event-driven Swift client for PostgreSQL.
18-
.package(url: "https://github.com/vapor/postgresql.git", .branch("gm")),
18+
.package(url: "https://github.com/vapor/postgresql.git", .branch("master")),
1919
],
2020
targets: [
2121
.target(name: "FluentPostgreSQL", dependencies: ["Async", "Fluent", "PostgreSQL"]),

Sources/FluentPostgreSQL/PostgreSQLModel.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@ public protocol PostgreSQLModel: Model where Self.Database == PostgreSQLDatabase
44
}
55

66
extension PostgreSQLModel {
7-
/// See `Model.idKey`
7+
/// See `Model`.
88
public static var idKey: IDKey { return \.id }
99
}
1010

11+
public protocol PostgreSQLStringModel: Model where Self.Database == PostgreSQLDatabase, Self.ID == String {
12+
/// This model's unique identifier.
13+
var id: String? { get set }
14+
}
15+
16+
extension PostgreSQLStringModel {
17+
/// See `Model`.
18+
public static var idKey: IDKey { return \.id }
19+
}
20+
21+
1122
public protocol PostgreSQLPivot: Pivot, PostgreSQLModel { }
1223

1324
public protocol PostgreSQLMigration: Migration where Self.Database == PostgreSQLDatabase { }

Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,6 @@ class FluentPostgreSQLTests: XCTestCase {
413413
}
414414

415415
func testSort() throws {
416-
struct Planet: PostgreSQLModel, PostgreSQLMigration, Equatable {
417-
var id: Int?
418-
var name: String
419-
init(id: Int? = nil, name: String) {
420-
self.id = id
421-
self.name = name
422-
}
423-
}
424416
let conn = try benchmarker.pool.requestConnection().wait()
425417
defer { benchmarker.pool.releaseConnection(conn) }
426418
try Planet.prepare(on: conn).wait()
@@ -434,6 +426,20 @@ class FluentPostgreSQLTests: XCTestCase {
434426
let ordered = try Planet.query(on: conn).sort(\.name).all().wait()
435427
XCTAssertNotEqual(unordered, ordered)
436428
}
429+
430+
func testCustomFilter() throws {
431+
let conn = try benchmarker.pool.requestConnection().wait()
432+
defer { benchmarker.pool.releaseConnection(conn) }
433+
try Planet.prepare(on: conn).wait()
434+
defer { _ = try? Planet.revert(on: conn).wait() }
435+
436+
_ = try Planet(name: "Jupiter").save(on: conn).wait()
437+
_ = try Planet(name: "Earth").save(on: conn).wait()
438+
_ = try Planet(name: "Mars").save(on: conn).wait()
439+
440+
let earth = try Planet.query(on: conn).filter(\.name, .ilike, "earth").first().wait()
441+
XCTAssertEqual(earth?.name, "Earth")
442+
}
437443

438444
static let allTests = [
439445
("testSchema", testSchema),
@@ -461,9 +467,19 @@ class FluentPostgreSQLTests: XCTestCase {
461467
("testContains", testContains),
462468
("testEmptySubset", testEmptySubset),
463469
("testSort", testSort),
470+
("testCustomFilter", testCustomFilter),
464471
]
465472
}
466473

474+
struct Planet: PostgreSQLModel, PostgreSQLMigration, Equatable {
475+
var id: Int?
476+
var name: String
477+
init(id: Int? = nil, name: String) {
478+
self.id = id
479+
self.name = name
480+
}
481+
}
482+
467483
extension PostgreSQLColumnType {
468484
static var planetType: PostgreSQLColumnType {
469485
return .custom("PLANET_TYPE")

0 commit comments

Comments
 (0)