Rope provides a convenient, easy-to-use, type-safe access to PostgreSQL for server-side Swift 3.
It uses the thread-safe, highly performant libpq library.
Rope is so simple, you just need to learn 3 methods:
connect()to create a connectionquery()to run a queryrows()to turn a query result into a two-dimensional array
// credential struct as helper
let creds = RopeCredentials(host: "localhost", port: 5432, dbName: "mydb",
user: "foo", password: "bar")
// establish connection using the struct, returns nil on error
guard let db = try? Rope.connect(credentials: creds) else {
print("Could not connect to Postgres")
return
}
// run INSERT query, it returns nil on a syntax or connection error
// the insert is SQL-injection safe due to the use of dollar params!
let text = "Hello World"
guard let _ = try? db.query("INSERT INTO my_table (my_text) VALUES($1)')", params: [text]) else {
print("Could not insert \(text) into database");
return
}
// run SELECT query, it returns nil on a syntax or connection error
guard let res = try? db.query("SELECT id, my_text FROM my_table") else {
print("Could not fetch id & my_text from database")
return
}
// execute statements with params (SQL-injection safe)
guard let res = try? db.query("SELECT * FROM my_table WHERE my_text=$1", params: ["Hello World"]) else {
print("Could not fetch id & my_text from database")
return
}
// handle errors with a do/catch
do {
let res = try db.query("SELECT id, my_text FROM my_table")
} catch {
// Error handling
}
// turn result into 2-dimensional array
if let rows = res?.rows() {
for row in rows {
let id = row["id"] as? Int
let myText = row["my_text"] as? String
}
}serial,bigserial,smallint,integer, andbigintare returned asIntrealanddoubleprecision are returned asFloatchar,varchar, andtextare returned asStringjsonis converted to aDictionaryof[String: Any?]- the
booleantype is returned asBool date,timestampare returned asDate
Rope’s unit tests require a running Postgres 9.x database and you can either provide the database credentials via environment variables, or via CLI arguments or use the built-in default values.
All tests run without any additional configuration if your database has the following setup:
host: "localhost"port: 5432database name: "rope"user: "postgres"password: ""
You can easily provide the database credentials via environment variables.
Please see the RopeTestCredentials.swift file. Please also see the unit tests about how to use RopeCredentials to establish a connection.
For environment variables in Xcode, please enter the following info via Edit Scheme > Arguements using Environment Variables or Arguments Passend On Launch:
DATABASE_HOSTDATABASE_PORTDATABASE_NAMEDATABASE_USERDATABASE_PASSWORD
swift build DATABASE_HOST=mydatabase_host DATABASE_PORT=mydatabase_port DATABASE_NAME=mydatabase_dbname DATABASE_USER=mydatabase_user DATABASE_PASSWORD=mydatabase_very_secure_password
To run tests simple type swift test in your CLI.
The source code is formatted using SwiftLint and all commits & PRs need to be without any SwiftLint warnings or errors.
Rope is maintained by Thomas Catterall (@swizzlr), Johannes Erhardt (@johanneserhardt), Sebastian Kreutzberger (@skreutzberger).
Contributions are more than welcomed. You can either work on existing Github issues or discuss with us your ideas in a new Github issue. Thanks 🙌
Rope is released under the Apache 2.0 License.