Swiftpack.co - vapor/postgres-kit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by vapor.
vapor/postgres-kit 2.12.3
🐘 Non-blocking, event-driven Swift client for PostgreSQL.
⭐️ 177
πŸ•“ 4 weeks ago
iOS macOS watchOS tvOS linux macOS iOS
.package(url: "https://github.com/vapor/postgres-kit.git", from: "2.12.3")

PostgresKit

Documentation Team Chat MIT License Continuous Integration Swift 5.7+


🐘 Non-blocking, event-driven Swift client for PostgreSQL.

Major Releases

The table below shows a list of PostgresKit major releases alongside their compatible NIO and Swift versions.

Version NIO Swift SPM
2.0 2.0 5.2+ from: "2.0.0"
1.0 1.0 4.0+ from: "1.0.0"

Use the SPM string to easily include the dependendency in your Package.swift file.

.package(url: "https://github.com/vapor/postgres-kit.git", from: ...)

Supported Platforms

PostgresKit supports the following platforms:

  • Ubuntu 20.04+
  • macOS 10.15+

Overview

PostgresKit is a PostgreSQL client library built on SQLKit. It supports building and serializing Postgres-dialect SQL queries. PostgresKit uses PostgresNIO to connect and communicate with the database server asynchronously. AsyncKit is used to provide connection pooling.

Configuration

Database connection options and credentials are specified using a PostgresConfiguration struct.

import PostgresKit

let configuration = PostgresConfiguration(
    hostname: "localhost",
    username: "vapor_username",
    password: "vapor_password",
    database: "vapor_database"
)

URL string based configuration is also supported.

guard let configuration = PostgresConfiguration(url: "postgres://...") else {
    ...
}

To connect via unix-domain sockets, use unixDomainSocketPath instead of hostname and port.

let configuration = PostgresConfiguration(
    unixDomainSocketPath: "/path/to/socket",
    username: "vapor_username",
    password: "vapor_password",
    database: "vapor_database"
)

Connection Pool

Once you have a PostgresConfiguration, you can use it to create a connection source and pool.

let eventLoopGroup: EventLoopGroup = ...
defer { try! eventLoopGroup.syncShutdown() }

let pools = EventLoopGroupConnectionPool(
    source: PostgresConnectionSource(configuration: configuration), 
    on: eventLoopGroup
)
defer { pools.shutdown() }

First create a PostgresConnectionSource using the configuration struct. This type is responsible for creating new connections to your database server as needed.

Next, use the connection source to create an EventLoopGroupConnectionPool. You will also need to pass an EventLoopGroup. For more information on creating an EventLoopGroup, visit SwiftNIO's documentation. Make sure to shutdown the connection pool before it deinitializes.

EventLoopGroupConnectionPool is a collection of pools for each event loop. When using EventLoopGroupConnectionPool directly, random event loops will be chosen as needed.

pools.withConnection { conn 
    print(conn) // PostgresConnection on randomly chosen event loop
}

To get a pool for a specific event loop, use pool(for:). This returns an EventLoopConnectionPool.

let eventLoop: EventLoop = ...
let pool = pools.pool(for: eventLoop)

pool.withConnection { conn
    print(conn) // PostgresConnection on eventLoop
}

PostgresDatabase

Both EventLoopGroupConnectionPool and EventLoopConnectionPool can be used to create instances of PostgresDatabase.

let postgres = pool.database(logger: ...) // PostgresDatabase
let rows = try postgres.simpleQuery("SELECT version();").wait()

Visit PostgresNIO's docs for more information on using PostgresDatabase.

SQLDatabase

A PostgresDatabase can be used to create an instance of SQLDatabase.

let sql = postgres.sql() // SQLDatabase
let planets = try sql.select().column("*").from("planets").all().wait()

Visit SQLKit's docs for more information on using SQLDatabase.

GitHub

link
Stars: 177
Last commit: 2 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

2.12.3 - Fix mishandling of URL
4 weeks ago

What's Changed

Fix mishandling of URL by @gwynne in #257

We have for some time now been incorrectly encoding URLs in an incorrect format that fails to trivially round-trip back through decoding. This is now fixed. Any previously broken data stored in a database affected by this issue will now be correctly read back and will have the correct format when next written.

[!NOTE] This is not related in any way to the recent issues with Vapor’s URI type; it is a separate concern affecting the URL type that comes from Foundation.

Also incidentally fixes #255

Reviewers

Thanks to the reviewers for their help:

  • @MahdiBM
This patch was released by @gwynne

Full Changelog: https://github.com/vapor/postgres-kit/compare/2.12.2...2.12.3

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics