AssociationToMany

public protocol AssociationToMany : Association

Experimental

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

  • 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

    public func average(_ 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: 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: 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)
    

    Declaration

    Swift

    public func sum(_ expression: SQLExpressible) -> AssociationAggregate<OriginRowDecoder>