AssociationToMany

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

AssociationToMany

  • forKey(_:) Extension method
  • 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)
    
  • 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)
    
  • 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)
    
  • 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)
    
  • 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)
    
  • 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.

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