All Projects → vapor → postgres-kit

vapor / postgres-kit

Licence: MIT License
🐘 Non-blocking, event-driven Swift client for PostgreSQL.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to postgres-kit

auth
👤 Authentication and Authorization framework for Fluent.
Stars: ✭ 51 (-59.2%)
Mutual labels:  vapor, server-side-swift, vapor-service
fluent-postgres-driver
🐘 PostgreSQL driver for Fluent.
Stars: ✭ 120 (-4%)
Mutual labels:  vapor, server-side-swift, vapor-service
fluent-mysql-driver
🖋🐬 Swift ORM (queries, models, relations, etc) built on MySQL.
Stars: ✭ 69 (-44.8%)
Mutual labels:  vapor, server-side-swift, vapor-service
async
⏱ Promises and reactive-streams in Swift built for high-performance and scalability.
Stars: ✭ 35 (-72%)
Mutual labels:  vapor, server-side-swift
template
A Vapor template for convenient and fast scaffolding 🏎
Stars: ✭ 33 (-73.6%)
Mutual labels:  vapor, server-side-swift
sendgrid
SendGrid-powered mail backend for Vapor
Stars: ✭ 66 (-47.2%)
Mutual labels:  vapor, vapor-service
GraphQLRouteCollection
A GraphQL based RouteCollection for Vapor
Stars: ✭ 18 (-85.6%)
Mutual labels:  vapor, server-side-swift
readme
Welcome to Vapor development at Nodes 📖
Stars: ✭ 47 (-62.4%)
Mutual labels:  vapor, server-side-swift
HomeKitty
A Vapor 3 website to easily browse HomeKit accessories.
Stars: ✭ 75 (-40%)
Mutual labels:  vapor, server-side-swift
flash
Flash messages between views ⚡️
Stars: ✭ 34 (-72.8%)
Mutual labels:  vapor, server-side-swift
gatekeeper
Rate limiting middleware for Vapor 👮
Stars: ✭ 54 (-56.8%)
Mutual labels:  vapor, server-side-swift
awesome-vapor
A curated list of Vapor-related awesome projects.
Stars: ✭ 907 (+625.6%)
Mutual labels:  vapor, server-side-swift
SwiftString
A comprehensive, lightweight string extension for Swift 3.x & 4.0
Stars: ✭ 117 (-6.4%)
Mutual labels:  vapor, server-side-swift
Stevenson
Stevenson is a Vapor framework designed to build integrations between Slack apps, GitHub, JIRA and CI services (CircleCI).
Stars: ✭ 57 (-54.4%)
Mutual labels:  vapor, server-side-swift
paginator
Offset pagination for Vapor 🗂
Stars: ✭ 67 (-46.4%)
Mutual labels:  vapor, server-side-swift
sqlite-kit
Non-blocking SQLite client library with SQL builder built on SwiftNIO
Stars: ✭ 51 (-59.2%)
Mutual labels:  vapor, server-side-swift
apns
Vapor APNS for iOS
Stars: ✭ 59 (-52.8%)
Mutual labels:  vapor, vapor-service
VaporTwilioService
Twilio API provider for all your Vapor needs
Stars: ✭ 19 (-84.8%)
Mutual labels:  vapor, server-side-swift
routing-kit
🚍 High-performance trie-node router.
Stars: ✭ 95 (-24%)
Mutual labels:  vapor, server-side-swift
bugsnag
Report errors with Bugsnag 🐛
Stars: ✭ 37 (-70.4%)
Mutual labels:  vapor, server-side-swift

PostgresKit


Documentation Team Chat MIT License Continuous Integration Swift 5.2

🐘 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 16.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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].