annotated(with:)
Appends association aggregates to the selected columns.
func annotated(with aggregates: [AssociationAggregate<RowDecoder>]) -> Self
For example:
struct Author: TableRecord, FetchableRecord, Decodable {
static let books = hasMany(Book.self)
}
struct Book: TableRecord, FetchableRecord, Decodable { }
struct AuthorInfo: FetchableRecord, Decodable {
var author: Author
var bookCount: Int
}
// SELECT author.*, COUNT(DISTINCT book.id) AS bookCount
// FROM author
// LEFT JOIN book ON book.authorId = author.id
// GROUP BY author.id
let authorInfos = try Author.all()
.annotated(with: [Author.books.count])
.asRequest(of: AuthorInfo.self)
.fetchAll(db)