Swiftpack.co - Package - vapor-community/postgresql

Swift Linux Build Status macOS Build Status codecov GitHub license

PostgreSQL for Swift

Prerequisites

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

Using PostgreSQL

This section outlines how to import the PostgreSQL package both with or without a Vapor project.

With Vapor

The easiest way to use PostgreSQL with Vapor is to include the PostgreSQL provider.

import PackageDescription

let package = Package(
    name: "Project",
    dependencies: [
        .Package(url: "https://github.com/vapor/vapor.git", majorVersion: 2),
        .Package(url: "https://github.com/vapor-community/postgresql-provider.git", majorVersion: 2)
    ],
    exclude: [ ... ]
)

The PostgreSQL provider package adds PostgreSQL to your project and adds some additional, Vapor-specific conveniences like drop.postgresql().

Using import PostgreSQLProvider will import both Fluent and Fluent's Vapor-specific APIs.

With Fluent

Fluent is a powerful, pure-Swift ORM that can be used with any Server-Side Swift framework. The PostgreSQL driver allows you to use a PostgreSQL database to power your models and queries.

import PackageDescription

let package = Package(
    name: "Project",
    dependencies: [
        ...
        .Package(url: "https://github.com/vapor/fluent.git", majorVersion: 2),
        .Package(url: "https://github.com/vapor-community/postgresql-driver.git", majorVersion: 2)
    ],
    exclude: [ ... ]
)

Use import PostgreSQLDriver to access the PostgreSQLDriver class which you can use to initialize a Fluent Database.

Just PostgreSQL

At the core of the PostgreSQL provider and PostgreSQL driver is a Swift wrapper around the C PostgreSQL client. This package can be used by itself to send raw, parameterized queries to your PostgreSQL database.

import PackageDescription

let package = Package(
    name: "Project",
    dependencies: [
        ...
        .Package(url: "https://github.com/vapor/postgresql.git", majorVersion: 2)
    ],
    exclude: [ ... ]
)

Use import PostgreSQL to access the PostgreSQL.Database class.

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 PostgreSQL.Values.

let results = try postgreSQL.execute("SELECT * FROM users WHERE age >= $1", [.int(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 Steven Roebert, Nate Bird, Prince Ugwuh, and other members of the Vapor community.

Github

link
Stars: 126

Used By

Total: 0

Releases

2.2.0 - 2020-05-18 15:19:18

Adds support for Swift 5

PostgreSQL 2.1.2 - 2018-05-22 07:24:13

This release fixes a critical issue that affected parsing numeric values from the database (#76). Updating is highly recommended.

PostgreSQL 1.1.1 - 2018-01-16 17:10:37

Changes:

  • The package now explicitly depends on vapor-community/cpostgresql to support successful builds in the future, no matter how Vapor repos are renamed.

PostgreSQL 2.1.1 – Highly Recommended - 2017-12-01 22:21:13

This release fixes a memory leak in the C bindings, which caused memory to slowly trickle with every SQL operation executed by the driver. Updating is highly recommended.

Thanks to @kzaher for the fix.

PostgreSQL 2.1 - 2017-09-05 07:13:46

New:

  • Preliminary Swift 4 support.

PostgreSQL 2.0.2 - 2017-06-15 12:34:43

Fixes:

  • Some numeric values (e.g. the result of AVG(x)) were parsed incorrectly. #48

PostgreSQL 2.0.1 - 2017-05-22 08:13:03

Changes:

  • Removed the dependency on tzdata on Linux (this fixes #43).

PostgreSQL 2.0.0 - 2017-05-17 22:22:37

Changes:

  • Compatible with Node 2.0 and Fluent 2.0!
  • Added support for LISTEN and NOTIFY (thanks @johnbona!)
  • Improved error messaging
  • Added support for Date types
  • Added support for master and read replicas. (init parameter changed from host to hostname)

PostgreSQL 2.0.0 Beta 4 - 2017-05-13 22:40:19

Changes:

  • Moved all methods from Database to Connection, leaving Database to be a simple class containing all the information to connect to a database.
  • Updated PostgreSQLError and replaced all uses of DatabaseError with it.
  • Created method for performing multiple queries in one transaction, using a specific isolation level.
  • Added option to the listen function to stop listening.
  • Prevented the listen function from crashing with a fatal error. Now an error is returned instead.
  • Refactored all binding code into a similar structure as Vapor MySQL.
  • Implemented handling of Date, instead of parsing the date into a string.
  • Added unit tests for the new date type parsing.

PostgreSQL 2.0.0 Beta 3 - 2017-04-25 23:56:47

Fixed:

  • infinite loop when calling the execute(...) -> Node extension on a Connection.

PostgreSQL 2.0 Beta 2 - 2017-04-07 05:03:41

New:

  • Support for LISTEN and NOTIFY (thanks @johnbona!)
  • Add Error class for better error messaging.
  • Add Context class with isPostgreSQL var.

Changes:

  • Rename connection parameter from host to hostname. This allows for supporting master and read replica in PostgreSQL Driver.
  • Rename connected boolean to isConnected.
  • Rename connection pointer to cConnection.
  • Rename error var to lastError.
  • Move all connection tests into the ConnectionTests file.

PostgreSQL 2.0 Beta 1 - 2017-04-05 17:55:45

Changes:

  • Compatible with CPostgreSQL 2.0 and Node 2.0 betas

- 2016-12-02 20:52:12

PostgreSQL 1.1 - 2016-10-31 22:44:39

  • Cleanup of pointer handling in binding parameters
  • Proper handling of binary data, parsing from and to .bytes in Node.

PostgreSQL 1.0.2 - 2016-10-26 16:50:46

  • Unknown field types are now returned as a Node.string instead of Node.null, allowing the values to be used as a string.

PostgreSQL 1.0.1 - 2016-10-18 17:04:16

Fixed:

  • Boolean values are now read correctly.

PostgreSQL 1.0 - 2016-09-28 01:40:01

Swiftenv GM Support, Node 0.6.0 support - 2016-09-12 20:35:45

GM Support - 2016-09-12 18:25:47

Supports latest Node build

- 2016-09-05 19:18:23

initial release of PostgreSQL wrapper for Swift - 2016-07-15 19:20:23