Association
public protocol Association : DerivableRequest
The base protocol for all associations that define a connection between two record types.
-
Undocumented
Declaration
Swift
associatedtype OriginRowDecoder
-
Undocumented
Declaration
Swift
associatedtype RowDecoder
-
The association key defines how rows fetched from this association should be consumed.
For example:
struct Player: TableRecord { // The default key of this association is the name of the // database table for teams, let's say "team": static let team = belongsTo(Team.self) } print(Player.team.key) // Prints "team" // Consume rows: let request = Player.including(required: Player.team) for row in Row.fetchAll(db, request) { let team: Team = row["team"] // the association key }
The key can be redefined with the
forKey
method:let request = Player.including(required: Player.team.forKey("custom")) for row in Row.fetchAll(db, request) { let team: Team = row["custom"] }
Declaration
Swift
var key: String { get }
-
forKey(_:)
Default implementationCreates an association with the given key.
This new key impacts how rows fetched from the resulting association should be consumed:
struct Player: TableRecord { static let team = belongsTo(Team.self) } // Consume rows: let request = Player.including(required: Player.team.forKey("custom")) for row in Row.fetchAll(db, request) { let team: Team = row["custom"] }
Default Implementation
Creates an association with the given key.
This new key helps Decodable records decode rows fetched from the resulting association:
struct Player: TableRecord { static let team = belongsTo(Team.self) } struct PlayerInfo: FetchableRecord, Decodable { let player: Player let team: Team static func all() -> QueryInterfaceRequest<PlayerInfo> { return Player .including(required: Player.team.forKey(CodingKeys.team)) .asRequest(of: PlayerInfo.self) } } let playerInfos = PlayerInfo.all().fetchAll(db) print(playerInfos.first?.team)
Declaration
Swift
func forKey(_ key: String) -> Self
-
select(_:)
Extension methodCreates an association which selects selection.
struct Player: TableRecord { static let team = belongsTo(Team.self) } // SELECT player.*, team.color // FROM player // JOIN team ON team.id = player.teamId let association = Player.team.select([Column("color")]) var request = Player.including(required: association)
Any previous selection is replaced:
// SELECT player.*, team.color // FROM player // JOIN team ON team.id = player.teamId let association = Player.team .select([Column("id")]) .select([Column("color")]) var request = Player.including(required: association)
Declaration
Swift
public func select(_ selection: [SQLSelectable]) -> Self
-
filter(_:)
Extension methodCreates an association with the provided predicate promise added to the eventual set of already applied predicates.
struct Player: TableRecord { static let team = belongsTo(Team.self) } // SELECT player.*, team.* // FROM player // JOIN team ON team.id = player.teamId AND 1 let association = Player.team.filter { db in true } var request = Player.including(required: association)
Declaration
Swift
public func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> Self
-
order(_:)
Extension methodCreates an association with the provided orderings promise.
struct Player: TableRecord { static let team = belongsTo(Team.self) } // SELECT player.*, team.* // FROM player // JOIN team ON team.id = player.teamId // ORDER BY team.name let association = Player.team.order { _ in [Column("name")] } var request = Player.including(required: association)
Any previous ordering is replaced:
// SELECT player.*, team.* // FROM player // JOIN team ON team.id = player.teamId // ORDER BY team.name let association = Player.team .order{ _ in [Column("color")] } .reversed() .order{ _ in [Column("name")] } var request = Player.including(required: association)
Declaration
Swift
public func order(_ orderings: @escaping (Database) throws -> [SQLOrderingTerm]) -> Self
-
reversed()
Extension methodCreates an association that reverses applied orderings.
struct Player: TableRecord { static let team = belongsTo(Team.self) } // SELECT player.*, team.* // FROM player // JOIN team ON team.id = player.teamId // ORDER BY team.name DESC let association = Player.team.order(Column("name")).reversed() var request = Player.including(required: association)
If no ordering was applied, the returned association is identical.
// SELECT player.*, team.* // FROM player // JOIN team ON team.id = player.teamId let association = Player.team.reversed() var request = Player.including(required: association)
Declaration
Swift
public func reversed() -> Self
-
aliased(_:)
Extension methodCreates an association that allows you to define expressions that target a specific database table.
In the example below, the
team.color = ‘red’
condition in the where clause could be not achieved without table aliases.struct Player: TableRecord { static let team = belongsTo(Team.self) } // SELECT player.*, team.* // JOIN team ON ... // WHERE team.color = 'red' let teamAlias = TableAlias() let request = Player .including(required: Player.team.aliased(teamAlias)) .filter(teamAlias[Column("color")] == "red")
When you give a name to a table alias, you can reliably inject sql snippets in your requests:
// SELECT player.*, custom.* // JOIN team custom ON ... // WHERE custom.color = 'red' let teamAlias = TableAlias(name: "custom") let request = Player .including(required: Player.team.aliased(teamAlias)) .filter(sql: "custom.color = ?", arguments: ["red"])
Declaration
Swift
public func aliased(_ alias: TableAlias) -> Self
-
including(optional:)
Extension methodCreates an association that includes another one. The columns of the associated record are selected. The returned association does not require that the associated database table contains a matching row.
Declaration
Swift
public func including<A>(optional association: A) -> Self where A : Association, Self.RowDecoder == A.OriginRowDecoder
-
including(required:)
Extension methodCreates an association that includes another one. The columns of the associated record are selected. The returned association requires that the associated database table contains a matching row.
Declaration
Swift
public func including<A>(required association: A) -> Self where A : Association, Self.RowDecoder == A.OriginRowDecoder
-
joining(optional:)
Extension methodCreates an association that joins another one. The columns of the associated record are not selected. The returned association does not require that the associated database table contains a matching row.
Declaration
Swift
public func joining<A>(optional association: A) -> Self where A : Association, Self.RowDecoder == A.OriginRowDecoder
-
joining(required:)
Extension methodCreates an association that joins another one. The columns of the associated record are not selected. The returned association requires that the associated database table contains a matching row.
Declaration
Swift
public func joining<A>(required association: A) -> Self where A : Association, Self.RowDecoder == A.OriginRowDecoder