Swiftpack.co -  khanlou/SwiftgreSQL as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
khanlou/SwiftgreSQL
A synchronous blocking wrapper around libpq with no dependencies. Forked and modified from `vapor-community/postgresql`.
.package(url: "https://github.com/khanlou/SwiftgreSQL.git", from: "0.1.6")

SwiftgreSQL

This is a PostgreSQL library for Swift, generally designed to be used on the server with no dependencies. Queries are blocking.

It is forked from vapor-community/postgresql.

Prerequisites

The PostgreSQL C driver must be installed in order to use this package. Follow the README of the cpostgresql repo to get started.

Installing PostgreSQL

import PackageDescription

let package = Package(
    name: "Project",
    dependencies: [
        .package(url: "https://github.com/khanlou/SwiftgresQL", from: "0.1.0"),
    ],
    exclude: [ ... ]
)

Examples

Connecting to the Database

import PostgreSQL

let postgreSQL =  PostgreSQL.Database(
    hostname: "localhost",
    database: "test",
    user: "root",
    password: ""
)

Select

let version = try postgreSQL.execute("SELECT version()")

Prepared Statement

The second parameter to execute() is an array of PostgresDataType values.

let results = try postgreSQL.execute("SELECT * FROM users WHERE age >= $1", values: [.int(21)])

You can also pass in PostgresDataTypeConvertible values, which will create PostgresDataType values from common stdlib and Foundation types.

let results = try postgreSQL.execute("SELECT * FROM users WHERE age >= $1", [21])

Listen and Notify

try postgreSQL.listen(to: "test_channel") { notification in
    print(notification.channel)
    print(notification.payload)
}

// Allow set up time for LISTEN
sleep(1)

try postgreSQL.notify(channel: "test_channel", payload: "test_payload")

Connection

Each call to execute() creates a new connection to the PostgreSQL database. This ensures thread safety since a single connection cannot be used on more than one thread.

If you would like to re-use a connection between calls to execute, create a reusable connection and pass it as the third parameter to execute().

let connection = try postgreSQL.makeConnection()
let result = try postgreSQL.execute("SELECT * FROM users WHERE age >= $1", [.int(21)]), connection)

Contributors

Maintained by Soroush Khanlou, with thanks to the Vapor community for the original code..

GitHub

link
Stars: 3
Last commit: 14 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

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