mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Associations: inline documentation
This commit is contained in:
1 parent
9be001533c
commit
aa81b992c4
4 files changed
+170
-6
No files matched your search
@@ -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
|
||||
|
||||
@@ -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<Author> {
|
||||
/// 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: Destination.Type,
|
||||
key: String? = nil,
|
||||
|
||||
@@ -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<Book> {
|
||||
/// 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: Destination.Type,
|
||||
key: String? = nil,
|
||||
|
||||
@@ -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<Demographics> {
|
||||
/// 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: Destination.Type,
|
||||
key: String? = nil,
|
||||
|
||||
Reference in new issue
Block a user