Database

public class Database

Class defining the connection to the database.

To setup the database, in this case using PostgreSQL:

import SwiftKueryORM
import SwiftKueryPostgreSQL
let pool = PostgreSQLConnection.createPool(host: "localhost", port: 5432,
                                           options: [.databaseName("FoodDatabase")],
                                           poolOptions: ConnectionPoolOptions(
                                                                      initialCapacity: 10,
                                                                      maxCapacity: 50,
                                                                      timeout: 10000))
Database.default = Database(pool)
  • Definition of a DatabaseTask completion handler which accepts an optional Connection and optional Error

    Declaration

    Swift

    public typealias DatabaseTask = (Connection?, QueryError?) -> ()
  • Global default Database for the application

    Declaration

    Swift

    public static var `default`: Database?
  • Instance of TableInfo containing cached tables

    Declaration

    Swift

    public static var tableInfo: TableInfo
  • Create a Database instance which uses a single connection to perform each operation. The connection will remain open for the lifetime of the Database. It is safe for you to issue concurrent operations on the database: the ORM will ensure those operations are executed sequentially. This connection strategy provides lower latency requests, as a new connection does not need to be established for each operation, and has a small resource overhead. However, scalability is limited as all database operations are serialized. Below is example code which creates a connection and uses it to create a Database instance:

    var opts = [ConnectionOptions]()
    opts.append(ConnectionOptions.userName("myUser"))
    opts.append(ConnectionOptions.password("myPassword"))
    let connection = PostgreSQLConnection(host: host, port: port, options: opts)
    let result = connection.connectSync()
    guard let result.success else {
        // Handle error
        return
    }
    let db = Database(single: connection)
    

    Declaration

    Swift

    public convenience init(single connection: Connection)
  • Create a Database instance with multiple connections, managed by a connection pool, allowing operations to be performed concurrently. These connections will remain open for the lifetime of the Database. This connection strategy provides lower latency requests, as a new connection does not need to be established for each operation. You can choose between better performance or lower resource consumption by adjusting the number of connections in the pool. Below is an example code which creates a connection pool and uses it to create a Database instance:

    let connectionPool = PostgreSQLConnection.createPool(host: host, port: port, options: [.userName("myUser"), .password("myPassword")], poolOptions: ConnectionPoolOptions(initialCapacity: 5, maxCapacity: 10))
    
    let db = Database(connectionPool)
    

    Declaration

    Swift

    public init(_ pool: ConnectionPool)
  • Create a Database instance which uses short-lived connections that are generated on demand. A new Connection is created for every operation, and will be closed once the operation completes. This connection strategy allows for minimal resource consumption when idle, and allows multiple operations to be performed concurrently. However, the process of establishing a connection will increase the time taken to process each operation. Below is an example of a function that can be used as a connection generator and the call to create the Database instance:

    func getConnectionAndRunTask(task: @escaping (Connection?, QueryError?) -> ()) {
        var opts = [ConnectionOptions]()
        opts.append(ConnectionOptions.userName("myUser"))
        opts.append(ConnectionOptions.password("myPassword"))
        let connection = PostgreSQLConnection(host: host, port: port, options: opts)
        connection.connect() { result in
            guard result.success else {
                // Handle error
                return task(nil, QueryError.connection(result.asError?.localizedDescription ?? "Unknown connection error"))
            }
        return task(connection, nil)
        }
    }
    
    let db = Database(generator: getConnectionAndRunTask)```
    

    Declaration

    Swift

    public init(generator: @escaping (@escaping DatabaseTask) -> ())