AssociationToMany

public protocol AssociationToMany : Association

The base protocol for all associations that define a one-to-many connection.

AssociationToMany

  • forKey(_:) Extension method

    Declaration

    Swift

    public func forKey(_ key: String) -> Self
  • count Extension method

    The number of associated records.

    It has a default name, which is “[key]Count”, where key is the key of the association. For example:

    For example:

    struct TeamInfo: FetchableRecord, Decodable {
        var team: Team
        var playerCount: Int
    }
    let request = Team.annotated(with: Team.players.count())
    let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
    
    let teams: [Team] = try Team.having(Team.players.count() > 10).fetchAll(db)
    

    Declaration

    Swift

    public var count: AssociationAggregate<OriginRowDecoder> { get }
  • isEmpty Extension method

    Creates an aggregate that is true if there exists no associated records.

    It has a default name, which is “hasNo[Key]”, where key is the key of the association. For example:

    struct TeamInfo: FetchableRecord, Decodable {
        var team: Team
        var hasNoPlayer: Bool
    }
    let request = Team.annotated(with: Team.players.isEmpty())
    let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
    
    let teams: [Team] = try Team.having(Team.players.isEmpty()).fetchAll(db)
    let teams: [Team] = try Team.having(!Team.players.isEmpty())
    let teams: [Team] = try Team.having(Team.players.isEmpty() == false)
    

    Declaration

    Swift

    public var isEmpty: AssociationAggregate<OriginRowDecoder> { get }
  • average(_:) Extension method

    Creates an aggregate which evaluate to the average value of the given expression in associated records.

    When the averaged expression is a column, the aggregate has a default name which is “average[Key][Column]”, where key is the key of the association. For example:

    For example:

    struct TeamInfo: FetchableRecord, Decodable {
        var team: Team
        var averagePlayerScore: Double
    }
    let request = Team.annotated(with: Team.players.average(Column("score")))
    let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
    
    let teams: [Team] = try Team.having(Team.players.average(Column("score")) > 100).fetchAll(db)
    

    Declaration

    Swift

    @available(*, deprecated, message: "Did you mean average(Column(...﹚﹚? If not, prefer average(value.databaseValue﹚ instead.")
    public func average(_ expression: SQLExpressible) -> AssociationAggregate<OriginRowDecoder>
  • average(_:) Extension method

    Creates an aggregate which evaluate to the average value of the given expression in associated records.

    When the averaged expression is a column, the aggregate has a default name which is “average[Key][Column]”, where key is the key of the association. For example:

    For example:

    struct TeamInfo: FetchableRecord, Decodable {
        var team: Team
        var averagePlayerScore: Double
    }
    let request = Team.annotated(with: Team.players.average(Column("score")))
    let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
    
    let teams: [Team] = try Team.having(Team.players.average(Column("score")) > 100).fetchAll(db)
    

    Declaration

    Swift

    public func average(_ expression: SQLSpecificExpressible) -> AssociationAggregate<OriginRowDecoder>
  • max(_:) Extension method

    Creates an aggregate which evaluate to the maximum value of the given expression in associated records.

    When the maximized expression is a column, the aggregate has a default name which is “maximum[Key][Column]”, where key is the key of the association. For example:

    For example:

    struct TeamInfo: FetchableRecord, Decodable {
        var team: Team
        var maxPlayerScore: Double
    }
    let request = Team.annotated(with: Team.players.max(Column("score")))
    let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
    
    let teams: [Team] = try Team.having(Team.players.max(Column("score")) < 100).fetchAll(db)
    

    Declaration

    Swift

    @available(*, deprecated, message: "Did you mean max(Column(...﹚﹚? If not, prefer max(value.databaseValue﹚ instead.")
    public func max(_ expression: SQLExpressible) -> AssociationAggregate<OriginRowDecoder>
  • max(_:) Extension method

    Creates an aggregate which evaluate to the maximum value of the given expression in associated records.

    When the maximized expression is a column, the aggregate has a default name which is “maximum[Key][Column]”, where key is the key of the association. For example:

    For example:

    struct TeamInfo: FetchableRecord, Decodable {
        var team: Team
        var maxPlayerScore: Double
    }
    let request = Team.annotated(with: Team.players.max(Column("score")))
    let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
    
    let teams: [Team] = try Team.having(Team.players.max(Column("score")) < 100).fetchAll(db)
    

    Declaration

    Swift

    public func max(_ expression: SQLSpecificExpressible) -> AssociationAggregate<OriginRowDecoder>
  • min(_:) Extension method

    Creates an aggregate which evaluate to the minimum value of the given expression in associated records.

    When the minimized expression is a column, the aggregate has a default name which is “minimum[Key][Column]”, where key is the key of the association. For example:

    For example:

    struct TeamInfo: FetchableRecord, Decodable {
        var team: Team
        var minPlayerScore: Double
    }
    let request = Team.annotated(with: Team.players.min(Column("score")))
    let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
    
    let teams: [Team] = try Team.having(Team.players.min(Column("score")) > 100).fetchAll(db)
    

    Declaration

    Swift

    @available(*, deprecated, message: "Did you mean min(Column(...﹚﹚? If not, prefer min(value.databaseValue﹚ instead.")
    public func min(_ expression: SQLExpressible) -> AssociationAggregate<OriginRowDecoder>
  • min(_:) Extension method

    Creates an aggregate which evaluate to the minimum value of the given expression in associated records.

    When the minimized expression is a column, the aggregate has a default name which is “minimum[Key][Column]”, where key is the key of the association. For example:

    For example:

    struct TeamInfo: FetchableRecord, Decodable {
        var team: Team
        var minPlayerScore: Double
    }
    let request = Team.annotated(with: Team.players.min(Column("score")))
    let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
    
    let teams: [Team] = try Team.having(Team.players.min(Column("score")) > 100).fetchAll(db)
    

    Declaration

    Swift

    public func min(_ expression: SQLSpecificExpressible) -> AssociationAggregate<OriginRowDecoder>
  • sum(_:) Extension method

    Creates an aggregate which evaluate to the sum of the given expression in associated records.

    When the summed expression is a column, the aggregate has a default name which is “[key][Column]Sum”, where key is the key of the association. For example:

    For example:

    struct TeamInfo: FetchableRecord, Decodable {
        var team: Team
        var playerScoreSum: Double
    }
    let request = Team.annotated(with: Team.players.sum(Column("score")))
    let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
    
    let teams: [Team] = try Team.having(Team.players.sum(Column("score")) > 100).fetchAll(db)
    

    This aggregate invokes the SUM SQL function. See also total(_:), and https://www.sqlite.org/lang_aggfunc.html#sumunc.

    Declaration

    Swift

    @available(*, deprecated, message: "Did you mean sum(Column(...﹚﹚? If not, prefer sum(value.databaseValue﹚ instead.")
    public func sum(_ expression: SQLExpressible) -> AssociationAggregate<OriginRowDecoder>
  • sum(_:) Extension method

    Creates an aggregate which evaluate to the sum of the given expression in associated records.

    When the summed expression is a column, the aggregate has a default name which is “[key][Column]Sum”, where key is the key of the association. For example:

    For example:

    struct TeamInfo: FetchableRecord, Decodable {
        var team: Team
        var playerScoreSum: Double
    }
    let request = Team.annotated(with: Team.players.sum(Column("score")))
    let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
    
    let teams: [Team] = try Team.having(Team.players.sum(Column("score")) > 100).fetchAll(db)
    

    This aggregate invokes the SUM SQL function. See also total(_:), and https://www.sqlite.org/lang_aggfunc.html#sumunc.

    Declaration

    Swift

    public func sum(_ expression: SQLSpecificExpressible) -> AssociationAggregate<OriginRowDecoder>
  • total(_:) Extension method

    Creates an aggregate which evaluate to the sum of the given expression in associated records.

    When the summed expression is a column, the aggregate has a default name which is “[key][Column]Sum”, where key is the key of the association. For example:

    For example:

    struct TeamInfo: FetchableRecord, Decodable {
        var team: Team
        var playerScoreSum: Double
    }
    let request = Team.annotated(with: Team.players.total(Column("score")))
    let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
    
    let teams: [Team] = try Team.having(Team.players.total(Column("score")) > 100).fetchAll(db)
    

    This aggregate invokes the TOTAL SQL function. See also sum(_:), and https://www.sqlite.org/lang_aggfunc.html#sumunc.

    Declaration

    Swift

    public func total(_ expression: SQLSpecificExpressible) -> AssociationAggregate<OriginRowDecoder>