DatabaseConnection

public protocol DatabaseConnection

This protocol describes a handle to a database.

This is implemented by both our real database connections and our in-memory database connection for testing.

  • The event loop that the database uses to handle async work.

    Declaration

    Swift

    var eventLoop: EventLoop { get }
  • This method starts a new transaction.

    Declaration

    Swift

    func startTransaction() -> Transaction

    Return Value

    The new transaction.

  • This method commits a transaction to the database.

    The database will check the read conflict ranges on the transaction for conflicts with recent changes, and if it detects any, it will fail the transaction. Otherwise, the transaction’s changes will be committed into the database and will be available for subsequent reads.

    Declaration

    Swift

    func commit(transaction: Transaction) -> EventLoopFuture<()>

    Parameters

    transaction

    The transaction we are committing.

    Return Value

    A future that will fire when the transaction is finished committing. If the transaction cannot be committed, the future will throw an error.

  • transaction(_:) Extension method

    This method starts a transaction, runs a block of code, and commits the transaction.

    The block will be run asynchronously.

    Declaration

    Swift

    public func transaction<T>(_ block: @escaping (Transaction) throws -> T) -> EventLoopFuture<T>

    Parameters

    block

    The block to run with the transaction.

    Return Value

    A future providing the result of the block.

  • transaction(_:) Extension method

    This method starts a transaction, runs a block of code, and commits the transaction.

    The block will be run asynchronously.

    In this version, the block provides a future, and the value provided by that future will also be provided by the future that this method returns.

    Declaration

    Swift

    public func transaction<T>(_ block: @escaping (Transaction) throws -> EventLoopFuture<T>) -> EventLoopFuture<T>

    Parameters

    block

    The block to run with the transaction.

    Return Value

    A future providing the result of the block.