JoinableRequest

The protocol for all requests that can be associated.

JoinableRequest

  • including(all:) Extension method

    Creates a request that prefetches an association.

  • 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.

  • 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.

  • 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.

  • 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.

  • 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))
    
  • 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))