GithubHelp home page GithubHelp logo

isabella232 / postgres-kit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vapor/postgres-kit

0.0 0.0 0.0 748 KB

๐Ÿ˜ Non-blocking, event-driven Swift client for PostgreSQL.

License: MIT License

Swift 100.00%

postgres-kit's Introduction

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.

postgres-kit's People

Contributors

0xtim avatar abbasmousavi avatar axtonpitt avatar baarde avatar bennydebock avatar bensyverson avatar calebkleveter avatar cellane avatar code28 avatar dmonagle avatar ffried avatar grahamburgsma avatar gwynne avatar jaapwijnen avatar jordanebelanger avatar labradon avatar loganwright avatar mattpolzin avatar michal-tomlein avatar mihaelisaev avatar mrmage avatar mura-admin avatar obrhoff avatar pedantix avatar rafiki270 avatar siemensikkema avatar stjernegard avatar tanner0101 avatar twof avatar vkill avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.