diff --git a/Documentation/AssociationsBasics.md b/Documentation/AssociationsBasics.md index 348990721..1fab19016 100644 --- a/Documentation/AssociationsBasics.md +++ b/Documentation/AssociationsBasics.md @@ -101,7 +101,7 @@ let books = try author.books.fetchAll(db) As for loading all pairs of books and authors, it is not only easier, but also *far much efficient*: ```swift -struct BookInfo: FetchableRecord, Codable { +struct BookInfo: FetchableRecord, Decodable { let book: Book let author: Author? } @@ -295,7 +295,7 @@ struct Author: FetchableRecord, TableRecord { } // A pair made of a book and its author -struct BookInfo: FetchableRecord, Codable { +struct BookInfo: FetchableRecord, Decodable { let book: Book let author: Author? } @@ -1382,7 +1382,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. [Choosing Between BelongsTo and HasOne]: #choosing-between-belongsto-and-hasone [Self Joins]: #self-joins [The Types of Associations]: #the-types-of-associations -[Codable]: https://developer.apple.com/documentation/swift/codable [FetchableRecord]: ../README.md#fetchablerecord-protocols [migration]: ../README.md#migrations [Record]: ../README.md#records diff --git a/GRDB/QueryInterface/Association/BelongsToAssociation.swift b/GRDB/QueryInterface/Association/BelongsToAssociation.swift index b2f7b111d..187ff9e9d 100644 --- a/GRDB/QueryInterface/Association/BelongsToAssociation.swift +++ b/GRDB/QueryInterface/Association/BelongsToAssociation.swift @@ -37,7 +37,62 @@ extension BelongsToAssociation: TableRequest where Destination: TableRecord { } extension TableRecord { - /// TODO + /// Creates a "Belongs To" association between Self and the + /// destination type. + /// + /// struct Author: TableRecord { ... } + /// struct Book: TableRecord { + /// static let author = belongsTo(Author.self) + /// } + /// + /// The association will let you define requests that load both the source + /// and the destination type: + /// + /// // A request for all books with their authors: + /// let request = Book.including(optional: Book.author) + /// + /// To consume those requests, define a type that adopts both the + /// FetchableRecord and Decodable protocols: + /// + /// struct BookInfo: FetchableRecord, Decodable { + /// var book: Book + /// var author: Author? + /// } + /// + /// let bookInfos = try dbQueue.read { db in + /// return try BookInfo.fetchAll(db, request) + /// } + /// for bookInfo in bookInfos { + /// print("\(bookInfo.book.title) by \(bookInfo.author.name)") + /// } + /// + /// It is recommended that you define, alongside the association, a property + /// with the same name: + /// + /// struct Book: TableRecord { + /// static let author = belongsTo(Author.self) + /// var author: QueryInterfaceRequest { + /// return request(for: Book.author) + /// } + /// } + /// + /// This property will let you navigate from the source type to the + /// destination type: + /// + /// try dbQueue.read { db in + /// let book: Book = ... + /// let author = try book.author.fetchOne(db) // Author? + /// } + /// + /// - parameters: + /// - destination: The record type at the other side of the association. + /// - key: An eventual decoding key for the association. By default, it + /// is `destination.databaseTableName`. + /// - foreignKey: An eventual foreign key. You need to provide an + /// explicit foreign key when GRDB can't infer one from the database + /// schema. This happens when the schema does not define any foreign + /// key to the destination table, or when the schema defines several + /// foreign keys to the destination table. public static func belongsTo( _ destination: Destination.Type, key: String? = nil, diff --git a/GRDB/QueryInterface/Association/HasManyAssociation.swift b/GRDB/QueryInterface/Association/HasManyAssociation.swift index 249593787..b430a5886 100644 --- a/GRDB/QueryInterface/Association/HasManyAssociation.swift +++ b/GRDB/QueryInterface/Association/HasManyAssociation.swift @@ -37,7 +37,62 @@ extension HasManyAssociation: TableRequest where Destination: TableRecord { } extension TableRecord { - /// TODO + /// Creates a "Has many" association between Self and the + /// destination type. + /// + /// struct Book: TableRecord { ... } + /// struct Author: TableRecord { + /// static let books = hasMany(Book.self) + /// } + /// + /// The association will let you define requests that load both the source + /// and the destination type: + /// + /// // A request for all (author, book) pairs: + /// let request = Author.including(required: Author.books) + /// + /// To consume those requests, define a type that adopts both the + /// FetchableRecord and Decodable protocols: + /// + /// struct Authorship: FetchableRecord, Decodable { + /// var author: Author + /// var book: Book + /// } + /// + /// let authorships = try dbQueue.read { db in + /// return try Authorship.fetchAll(db, request) + /// } + /// for authorship in authorships { + /// print("\(authorship.author.name) wrote \(authorship.book.title)") + /// } + /// + /// It is recommended that you define, alongside the association, a property + /// with the same name: + /// + /// struct Author: TableRecord { + /// static let books = hasMany(Book.self) + /// var books: QueryInterfaceRequest { + /// return request(for: Author.books) + /// } + /// } + /// + /// This property will let you navigate from the source type to the + /// destination type: + /// + /// try dbQueue.read { db in + /// let author: Author = ... + /// let books = try author.books.fetchAll(db) // [Book] + /// } + /// + /// - parameters: + /// - destination: The record type at the other side of the association. + /// - key: An eventual decoding key for the association. By default, it + /// is `destination.databaseTableName`. + /// - foreignKey: An eventual foreign key. You need to provide an + /// explicit foreign key when GRDB can't infer one from the database + /// schema. This happens when the schema does not define any foreign + /// key from the destination table, or when the schema defines several + /// foreign keys from the destination table. public static func hasMany( _ destination: Destination.Type, key: String? = nil, diff --git a/GRDB/QueryInterface/Association/HasOneAssociation.swift b/GRDB/QueryInterface/Association/HasOneAssociation.swift index b5ef7be12..e53b35d62 100644 --- a/GRDB/QueryInterface/Association/HasOneAssociation.swift +++ b/GRDB/QueryInterface/Association/HasOneAssociation.swift @@ -37,7 +37,62 @@ extension HasOneAssociation: TableRequest where Destination: TableRecord { } extension TableRecord { - /// TODO + /// Creates a "Has one" association between Self and the + /// destination type. + /// + /// struct Demographics: TableRecord { ... } + /// struct Country: TableRecord { + /// static let demographics = hasOne(Demographics.self) + /// } + /// + /// The association will let you define requests that load both the source + /// and the destination type: + /// + /// // A request for all countries with their demographic profile: + /// let request = Country.including(optional: Country.demographics) + /// + /// To consume those requests, define a type that adopts both the + /// FetchableRecord and Decodable protocols: + /// + /// struct CountryInfo: FetchableRecord, Decodable { + /// var country: Country + /// var demographics: Demographics? + /// } + /// + /// let countryInfos = try dbQueue.read { db in + /// return try CountryInfo.fetchAll(db, request) + /// } + /// for countryInfo in countryInfos { + /// print("\(countryInfo.country.name) has \(countryInfo.demographics.population) citizens") + /// } + /// + /// It is recommended that you define, alongside the association, a property + /// with the same name: + /// + /// struct Country: TableRecord { + /// static let demographics = hasOne(Demographics.self) + /// var demographics: QueryInterfaceRequest { + /// return request(for: Country.demographics) + /// } + /// } + /// + /// This property will let you navigate from the source type to the + /// destination type: + /// + /// try dbQueue.read { db in + /// let country: Country = ... + /// let demographics = try country.demographics.fetchOne(db) // Demographics? + /// } + /// + /// - parameters: + /// - destination: The record type at the other side of the association. + /// - key: An eventual decoding key for the association. By default, it + /// is `destination.databaseTableName`. + /// - foreignKey: An eventual foreign key. You need to provide an + /// explicit foreign key when GRDB can't infer one from the database + /// schema. This happens when the schema does not define any foreign + /// key from the destination table, or when the schema defines several + /// foreign keys from the destination table. public static func hasOne( _ destination: Destination.Type, key: String? = nil,