Connect to your PostgreSQL database. Run queries. All natively in Swift.
Installation
Install via swift-package-manager by adding a depdendency to your Package.swift.
.Package(url: "https://github.com/stepanhruda/PostgreSQL-Swift.git", majorVersion: 0)
Usage
Connection
let parameters = ConnectionParameters(
host: "123.123.123.123",
port: "9000",
databaseName: "banana_pantry",
login: "mehungry",
password: "reallyhungrygotnopatience"
)
let connection = try Database.connect(parameters: parameters)
Environment variables
Your database configuration should not be in your application's source. Connecting to the database becomes as easy as:
let connection = try Database.connect()
Configuration is automatically loaded from default PostgreSQL environment variables.
export PGHOST 123.123.123.123
export PGPORT 9000
export PGDATABASE banana_pantry
export PGUSER mehungry
export PGPASSWORD reallyhungrygotnopatience
Queries and results
let result = try connection.execute("SELECT color, is_tasty, length FROM bananas")
for row in result.rows {
let color = row["color"] as! String
let isTasty = row["is_tasty"] as! Bool
let length = row["length"] as! Int
let banana = Banana(color: color, isTasty: isTasty, length: length)
}
Development on OS X
- Install dependencies
- Xcode 7+ (Swift 2.x)
brew cask install dockertoolbox
make development.setup
- Starts a PostgreSQL container that tests can be run against. Before running make sure your docker-machine environment variables are available (usually you run
eval $(docker-machine env default)
) development.setup
also adds handy opinionated environment variables to your Xcode scheme that connect to the container. If you are using a custom setup rather than what docker-machine gives you out of the box, you might need to tweak them. Also, please don't commit any changes to the.xcscheme
file.
make test
to run tests or run them through Xcode