HasManyThroughAssociation and HasOneThroughAssociation doc

This commit is contained in:
Gwendal Roué committed 2019-03-13 21:19:13 +01:00
1 parent 916d3931aa
commit cf837855f2
5 files changed
+193 -9

No files matched your search

@@ -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<Author> {
/// return request(for: Book.author)
@@ -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<Book> {
/// return request(for: Author.books)
@@ -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<Origin, Destination>: AssociationToMany {
/// :nodoc:
public typealias OriginRowDecoder = Origin
@@ -18,6 +61,65 @@ public struct HasManyThroughAssociation<Origin, Destination>: 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<Citizen> {
/// 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<Pivot, Target>(
_ destination: Target.RowDecoder.Type,
through pivot: Pivot,
@@ -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<Demographics> {
/// return request(for: Country.demographics)
@@ -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<Origin, Destination>: AssociationToOne {
/// :nodoc:
public typealias OriginRowDecoder = Origin
@@ -18,6 +41,65 @@ public struct HasOneThroughAssociation<Origin, Destination>: 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<Address> {
/// 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<Pivot, Target>(
_ destination: Target.RowDecoder.Type,
through pivot: Pivot,