diff --git a/GRDB/QueryInterface/Request/Association/HasManyAssociation.swift b/GRDB/QueryInterface/Request/Association/HasManyAssociation.swift index b60074152..fc6310fe5 100644 --- a/GRDB/QueryInterface/Request/Association/HasManyAssociation.swift +++ b/GRDB/QueryInterface/Request/Association/HasManyAssociation.swift @@ -100,13 +100,68 @@ public struct HasManyAssociation: cardinality: .toMany) } - /// TODO + /// Creates a "Has one" association between the source and + /// destination types. + /// + /// struct Book: TableRecord { ... } + /// struct Author: TableRecord { + /// static let firstBook = hasMany(Book.self).order(Column("year")).first + /// } + /// + /// The association lets you define requests that load both the source + /// and the destination type: + /// + /// // A request for all (author, firstBook) pairs: + /// let request = Author.including(required: Author.firstBook) + /// + /// To consume those requests, define a type that adopts both the + /// FetchableRecord and Decodable protocols: + /// + /// struct Authorship: FetchableRecord, Decodable { + /// var author: Author + /// var firstBook: Book + /// } + /// + /// let authorships = try dbQueue.read { db in + /// return try Authorship.fetchAll(db, request) + /// } + /// for authorship in authorships { + /// print("\(authorship.author.name) wrote \(authorship.firstBook.title) first") + /// } public var first: HasOneAssociation { HasOneAssociation(sqlAssociation: sqlAssociation.associationForFirst()) + .forKey("first\(key.singularizedName.uppercasingFirstCharacter)") } - /// TODO + /// Creates a "Has one" association between the source and + /// destination types. + /// + /// struct Book: TableRecord { ... } + /// struct Author: TableRecord { + /// static let lastBook = hasMany(Book.self).order(Column("year")).last + /// } + /// + /// The association lets you define requests that load both the source + /// and the destination type: + /// + /// // A request for all (author, firstBook) pairs: + /// let request = Author.including(required: Author.firstBook) + /// + /// To consume those requests, define a type that adopts both the + /// FetchableRecord and Decodable protocols: + /// + /// struct Authorship: FetchableRecord, Decodable { + /// var author: Author + /// var lastBook: Book + /// } + /// + /// let authorships = try dbQueue.read { db in + /// return try Authorship.fetchAll(db, request) + /// } + /// for authorship in authorships { + /// print("\(authorship.author.name) wrote \(authorship.lastBook.title) last") + /// } public var last: HasOneAssociation { - reversed().first + reversed().first.forKey("last\(key.singularizedName.uppercasingFirstCharacter)") } } diff --git a/GRDB/QueryInterface/Request/Association/HasManyThroughAssociation.swift b/GRDB/QueryInterface/Request/Association/HasManyThroughAssociation.swift index f9f385004..669839715 100644 --- a/GRDB/QueryInterface/Request/Association/HasManyThroughAssociation.swift +++ b/GRDB/QueryInterface/Request/Association/HasManyThroughAssociation.swift @@ -56,13 +56,14 @@ public struct HasManyThroughAssociation { HasOneThroughAssociation(sqlAssociation: sqlAssociation.associationForFirst()) + .forKey("first\(key.singularizedName.uppercasingFirstCharacter)") } - /// TODO + // TODO public var last: HasOneThroughAssociation { - reversed().first + reversed().first.forKey("last\(key.singularizedName.uppercasingFirstCharacter)") } }