JoinableRequest

public protocol JoinableRequest : TypedRequest, _JoinableRequest

The protocol for all requests that can be associated.

JoinableRequest

  • including(all:) Extension method

    Creates a request that prefetches an association.

    Declaration

    Swift

    public func including<A>(all association: A) -> Self where A : AssociationToMany, Self.RowDecoder == A.OriginRowDecoder
  • including(optional:) Extension method

    Creates a request that includes an association. The columns of the associated record are selected. The returned request 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 method

    Creates a request that includes an association. The columns of the associated record are selected. The returned request 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 method

    Creates a request that joins an association. The columns of the associated record are not selected. The returned request 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 method

    Creates a request that joins an association. The columns of the associated record are not selected. The returned request 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

Available where Self: SelectionRequest

  • annotated(withOptional:) Extension method

    Creates a request which appends columns of an associated record to the current selection.

    // SELECT player.*, team.color
    // FROM player LEFT JOIN team ...
    let teamColor = Player.team.select(Column("color"))
    let request = Player.all().annotated(withOptional: teamColor)
    

    This method performs the same SQL request as including(optional:). The difference is in the shape of Decodable records that decode such a request: the associated columns can be decoded at the same level as the main record:

    struct PlayerWithTeamColor: FetchableRecord, Decodable {
        var player: Player
        var color: String?
    }
    let players = try dbQueue.read { db in
        try request
            .asRequest(of: PlayerWithTeamColor.self)
            .fetchAll(db)
    }
    

    Note: this is a convenience method. You can build the same request with TableAlias, annotated(with:), and joining(optional:):

    let teamAlias = TableAlias()
    let request = Player.all()
        .annotated(with: teamAlias[Column("color")])
        .joining(optional: Player.team.aliased(teamAlias))
    

    Declaration

    Swift

    public func annotated<A>(withOptional association: A) -> Self where A : Association, Self.RowDecoder == A.OriginRowDecoder
  • annotated(withRequired:) Extension method

    Creates a request which appends columns of an associated record to the current selection.

    // SELECT player.*, team.color
    // FROM player JOIN team ...
    let teamColor = Player.team.select(Column("color"))
    let request = Player.all().annotated(withRequired: teamColor)
    

    This method performs the same SQL request as including(required:). The difference is in the shape of Decodable records that decode such a request: the associated columns can be decoded at the same level as the main record:

    struct PlayerWithTeamColor: FetchableRecord, Decodable {
        var player: Player
        var color: String
    }
    let players = try dbQueue.read { db in
        try request
            .asRequest(of: PlayerWithTeamColor.self)
            .fetchAll(db)
    }
    

    Note: this is a convenience method. You can build the same request with TableAlias, annotated(with:), and joining(required:):

    let teamAlias = TableAlias()
    let request = Player.all()
        .annotated(with: teamAlias[Column("color")])
        .joining(required: Player.team.aliased(teamAlias))
    

    Declaration

    Swift

    public func annotated<A>(withRequired association: A) -> Self where A : Association, Self.RowDecoder == A.OriginRowDecoder