Skip to content

PostgresKit 2.0.0 GM #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.0.0-rc.1"),
.package(url: "https://github.com/vapor/postgres-kit.git", from: "2.0.0-rc.1"),
.package(url: "https://github.com/vapor/postgres-kit.git", from: "2.0.0"),
],
targets: [
.target(name: "FluentPostgresDriver", dependencies: [
Expand Down
43 changes: 37 additions & 6 deletions Sources/FluentPostgresDriver/FluentPostgresConfiguration.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
extension DatabaseConfigurationFactory {
public static func postgres(
url urlString: String,
maxConnectionsPerEventLoop: Int = 1,
encoder: PostgresDataEncoder = .init(),
decoder: PostgresDataDecoder = .init()
) throws -> DatabaseConfigurationFactory {
guard let url = URL(string: urlString) else {
throw FluentPostgresError.invalidURL(urlString)
}
return try .postgres(
url: url,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
encoder: encoder,
decoder: decoder
)
}

public static func postgres(
url: URL,
maxConnectionsPerEventLoop: Int = 1
maxConnectionsPerEventLoop: Int = 1,
encoder: PostgresDataEncoder = .init(),
decoder: PostgresDataDecoder = .init()
) throws -> DatabaseConfigurationFactory {
guard let configuration = PostgresConfiguration(url: url) else {
throw FluentPostgresError.invalidURL(url)
throw FluentPostgresError.invalidURL(url.absoluteString)
}
return .postgres(
configuration: configuration,
Expand All @@ -19,7 +38,9 @@ extension DatabaseConfigurationFactory {
password: String,
database: String? = nil,
tlsConfiguration: TLSConfiguration? = nil,
maxConnectionsPerEventLoop: Int = 1
maxConnectionsPerEventLoop: Int = 1,
encoder: PostgresDataEncoder = .init(),
decoder: PostgresDataDecoder = .init()
) -> DatabaseConfigurationFactory {
return .postgres(
configuration: .init(
Expand All @@ -36,13 +57,17 @@ extension DatabaseConfigurationFactory {

public static func postgres(
configuration: PostgresConfiguration,
maxConnectionsPerEventLoop: Int = 1
maxConnectionsPerEventLoop: Int = 1,
encoder: PostgresDataEncoder = .init(),
decoder: PostgresDataDecoder = .init()
) -> DatabaseConfigurationFactory {
return DatabaseConfigurationFactory {
FluentPostgresConfiguration(
middleware: [],
configuration: configuration,
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
encoder: encoder,
decoder: decoder
)
}
}
Expand All @@ -52,6 +77,8 @@ struct FluentPostgresConfiguration: DatabaseConfiguration {
var middleware: [AnyModelMiddleware]
let configuration: PostgresConfiguration
let maxConnectionsPerEventLoop: Int
let encoder: PostgresDataEncoder
let decoder: PostgresDataDecoder

func makeDriver(for databases: Databases) -> DatabaseDriver {
let db = PostgresConnectionSource(
Expand All @@ -62,6 +89,10 @@ struct FluentPostgresConfiguration: DatabaseConfiguration {
maxConnectionsPerEventLoop: maxConnectionsPerEventLoop,
on: databases.eventLoopGroup
)
return _FluentPostgresDriver(pool: pool)
return _FluentPostgresDriver(
pool: pool,
encoder: encoder,
decoder: decoder
)
}
}
8 changes: 5 additions & 3 deletions Sources/FluentPostgresDriver/FluentPostgresDriver.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
enum FluentPostgresError: Error {
case invalidURL(URL)
case invalidURL(String)
}

struct _FluentPostgresDriver: DatabaseDriver {
let pool: EventLoopGroupConnectionPool<PostgresConnectionSource>
let encoder: PostgresDataEncoder
let decoder: PostgresDataDecoder

var eventLoopGroup: EventLoopGroup {
self.pool.eventLoopGroup
Expand All @@ -13,8 +15,8 @@ struct _FluentPostgresDriver: DatabaseDriver {
_FluentPostgresDatabase(
database: self.pool.pool(for: context.eventLoop).database(logger: context.logger),
context: context,
encoder: self.pool.source.configuration.encoder,
decoder: self.pool.source.configuration.decoder
encoder: self.encoder,
decoder: self.decoder
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ final class FluentPostgresDriverTests: XCTestCase {
hostname: hostname,
username: "vapor_username",
password: "vapor_password",
database: "vapor_database",
database: "vapor_database"
)
self.dbs.use(.postgres(
configuration: configuration,
encoder: PostgresDataEncoder(json: jsonEncoder),
decoder: PostgresDataDecoder(json: jsonDecoder)
)
self.dbs.use(.postgres(configuration: configuration), as: .iso8601)
), as: .iso8601)
let db = self.dbs.database(
.iso8601,
logger: .init(label: "test"),
Expand Down