diff --git a/GRDB/QueryInterface/Association/BelongsToAssociation.swift b/GRDB/QueryInterface/Association/BelongsToAssociation.swift index 2145e31a8..ae5d40971 100644 --- a/GRDB/QueryInterface/Association/BelongsToAssociation.swift +++ b/GRDB/QueryInterface/Association/BelongsToAssociation.swift @@ -107,10 +107,10 @@ extension TableRecord { /// print("\(bookInfo.book.title) by \(bookInfo.author.name)") /// } /// - /// It is recommended that you define, alongside the association, a property - /// with the same name: + /// It is recommended that you define, alongside the static association, a + /// property with the same name: /// - /// struct Book: TableRecord { + /// struct Book: TableRecord, EncodableRecord { /// static let author = belongsTo(Author.self) /// var author: QueryInterfaceRequest { /// return request(for: Book.author) diff --git a/GRDB/QueryInterface/Association/HasManyAssociation.swift b/GRDB/QueryInterface/Association/HasManyAssociation.swift index df519169c..cd06c98a5 100644 --- a/GRDB/QueryInterface/Association/HasManyAssociation.swift +++ b/GRDB/QueryInterface/Association/HasManyAssociation.swift @@ -107,10 +107,10 @@ extension TableRecord { /// print("\(authorship.author.name) wrote \(authorship.book.title)") /// } /// - /// It is recommended that you define, alongside the association, a property - /// with the same name: + /// It is recommended that you define, alongside the static association, a + /// property with the same name: /// - /// struct Author: TableRecord { + /// struct Author: TableRecord, EncodableRecord { /// static let books = hasMany(Book.self) /// var books: QueryInterfaceRequest { /// return request(for: Author.books) diff --git a/GRDB/QueryInterface/Association/HasManyThroughAssociation.swift b/GRDB/QueryInterface/Association/HasManyThroughAssociation.swift index 8ae2e3e62..3570b5ece 100644 --- a/GRDB/QueryInterface/Association/HasManyThroughAssociation.swift +++ b/GRDB/QueryInterface/Association/HasManyThroughAssociation.swift @@ -1,3 +1,46 @@ +/// The **HasManyThrough** association is often used to set up a many-to-many +/// connection with another record. This association indicates that the +/// declaring record can be matched with zero or more instances of another +/// record by proceeding through a third record. +/// +/// For example, consider the practice of passport delivery. One coutry +/// "has many" citizens "through" its passports: +/// +/// struct Country: TableRecord { +/// static let passports = hasMany(Passport.self) +/// static let citizens = hasMany(Citizen.self, through: passports, using: Passport.citizen) +/// ... +/// } +/// +/// struct Passport: TableRecord { +/// static let citizen = belongsTo(Citizen.self) +/// ... +/// } +/// +/// struct Citizen: TableRecord { ... } +/// +/// The **HasManyThrough** association is also useful for setting up +/// "shortcuts" through nested HasMany associations. For example, if a document +/// has many sections, and a section has many paragraphs, you may sometimes want +/// to get a simple collection of all paragraphs in the document. You could set +/// that up this way: +/// +/// struct Document: TableRecord { +/// static let sections = hasMany(Section.self) +/// static let paragraphs = hasMany(Paragraph.self, through: sections, using: Section.paragraphs) +/// } +/// +/// struct Section: TableRecord { +/// static let paragraphs = hasMany(Paragraph.self) +/// } +/// +/// struct Paragraph: TableRecord { +/// } +/// +/// As in the examples above, **HasManyThrough** association is always built from +/// two other associations: the `through:` and `using:` arguments. Those +/// associations can be any other association (BelongsTo, HasMany, +/// HasManyThrough, etc). public struct HasManyThroughAssociation: AssociationToMany { /// :nodoc: public typealias OriginRowDecoder = Origin @@ -18,6 +61,65 @@ public struct HasManyThroughAssociation: AssociationToMany extension HasManyThroughAssociation: TableRequest where Destination: TableRecord { } extension TableRecord { + /// Creates a "Has Many Through" association between Self and the + /// destination type. + /// + /// struct Country: TableRecord { + /// static let passports = hasMany(Passport.self) + /// static let citizens = hasMany(Citizen.self, through: passports, using: Passport.citizen) + /// } + /// + /// struct Passport: TableRecord { + /// static let citizen = belongsTo(Citizen.self) + /// } + /// + /// struct Citizen: TableRecord { } + /// + /// The association will let you define requests that load both the source + /// and the destination type: + /// + /// // A request for all (country, citizen) pairs: + /// let request = Country.including(required: Coutry.citizens) + /// + /// To consume those requests, define a type that adopts both the + /// FetchableRecord and Decodable protocols: + /// + /// struct Citizenship: FetchableRecord, Decodable { + /// var country: Country + /// var citizen: Citizen + /// } + /// + /// let citizenships = try dbQueue.read { db in + /// return try Citizenship.fetchAll(db, request) + /// } + /// for citizenship in citizenships { + /// print("\(citizenship.citizen.name) is a citizen of \(citizenship.country.name)") + /// } + /// + /// It is recommended that you define, alongside the static association, a + /// property with the same name: + /// + /// struct Country: TableRecord, EncodableRecord { + /// static let passports = hasMany(Passport.self) + /// static let citizens = hasMany(Citizen.self, through: passports, using: Passport.citizen) + /// var citizens: QueryInterfaceRequest { + /// return request(for: Country.citizens) + /// } + /// } + /// + /// This property will let you navigate from the source type to the + /// destination type: + /// + /// try dbQueue.read { db in + /// let country: Country = ... + /// let citizens = try country.citizens.fetchAll(db) // [Country] + /// } + /// + /// - parameters: + /// - destination: The record type at the other side of the association. + /// - through: An association from Self to the intermediate type. + /// - using: An association from the intermediate type to the + /// destination type. public static func hasMany( _ destination: Target.RowDecoder.Type, through pivot: Pivot, diff --git a/GRDB/QueryInterface/Association/HasOneAssociation.swift b/GRDB/QueryInterface/Association/HasOneAssociation.swift index 2d791b129..9255f5426 100644 --- a/GRDB/QueryInterface/Association/HasOneAssociation.swift +++ b/GRDB/QueryInterface/Association/HasOneAssociation.swift @@ -109,10 +109,10 @@ extension TableRecord { /// print("\(countryInfo.country.name) has \(countryInfo.demographics.population) citizens") /// } /// - /// It is recommended that you define, alongside the association, a property - /// with the same name: + /// It is recommended that you define, alongside the static association, a + /// property with the same name: /// - /// struct Country: TableRecord { + /// struct Country: TableRecord, EncodableRecord { /// static let demographics = hasOne(Demographics.self) /// var demographics: QueryInterfaceRequest { /// return request(for: Country.demographics) diff --git a/GRDB/QueryInterface/Association/HasOneThroughAssociation.swift b/GRDB/QueryInterface/Association/HasOneThroughAssociation.swift index abcbc53b6..c5af394af 100644 --- a/GRDB/QueryInterface/Association/HasOneThroughAssociation.swift +++ b/GRDB/QueryInterface/Association/HasOneThroughAssociation.swift @@ -1,3 +1,26 @@ +/// A **HasOneThrough** association sets up a one-to-one connection with +/// another record. This association indicates that the declaring record can be +/// matched with one instance of another record by proceeding through a third +/// record. For example, if each book belongs to a library, and each library has +/// one address, then one knows where the book should be returned to: +/// +/// struct Book: TableRecord { +/// static let library = belongsTo(Library.self) +/// static let returnAddress = hasOne(Address.self, through: library, using: library.address) +/// ... +/// } +/// +/// struct Library: TableRecord { +/// static let address = hasOne(Address.self) +/// ... +/// } +/// +/// struct Address: TableRecord { ... } +/// +/// As in the example above, **HasOneThrough** association is always built from +/// two other associations: the `through:` and `using:` arguments. Those +/// associations can be any other association to one (BelongsTo, HasOne, +/// HasOneThrough). public struct HasOneThroughAssociation: AssociationToOne { /// :nodoc: public typealias OriginRowDecoder = Origin @@ -18,6 +41,65 @@ public struct HasOneThroughAssociation: AssociationToOne { extension HasOneThroughAssociation: TableRequest where Destination: TableRecord { } extension TableRecord { + /// Creates a "Has One Through" association between Self and the + /// destination type. + /// + /// struct Book: TableRecord { + /// static let library = belongsTo(Library.self) + /// static let returnAddress = hasOne(Address.self, through: library, using: library.address) + /// } + /// + /// struct Library: TableRecord { + /// static let address = hasOne(Address.self) + /// } + /// + /// struct Address: TableRecord { ... } + /// + /// The association will let you define requests that load both the source + /// and the destination type: + /// + /// // A request for all (book, returnAddress) pairs: + /// let request = Book.including(required: Book.returnAddress) + /// + /// To consume those requests, define a type that adopts both the + /// FetchableRecord and Decodable protocols: + /// + /// struct Todo: FetchableRecord, Decodable { + /// var book: Book + /// var address: Address + /// } + /// + /// let todos = try dbQueue.read { db in + /// return try Todo.fetchAll(db, request) + /// } + /// for todo in todos { + /// print("Please return \(todo.book) to \(todo.address)") + /// } + /// + /// It is recommended that you define, alongside the static association, a + /// property with the same name: + /// + /// struct Book: TableRecord, EncodableRecord { + /// static let library = belongsTo(Library.self) + /// static let returnAddress = hasOne(Address.self, through: library, using: library.address) + /// var returnAddress: QueryInterfaceRequest
{ + /// return request(for: Book.returnAddress) + /// } + /// } + /// + /// This property will let you navigate from the source type to the + /// destination type: + /// + /// try dbQueue.read { db in + /// let book: Book = ... + /// let address = try book.returnAddress.fetchOne(db) // Address? + /// } + /// + /// - parameters: + /// - destination: The record type at the other side of the association. + /// - through: An association from Self to the intermediate type. + /// - using: An association from the intermediate type to the + /// destination type. public static func hasOne( _ destination: Target.RowDecoder.Type, through pivot: Pivot,