Associations: new "DerivableRequest Protocol" documentation chapter

This commit is contained in:
Gwendal Roué committed 2018-05-20 16:14:23 +02:00
1 parent 67e48f8ad7
commit 7be345521b
2 files changed
+93 -10

No files matched your search

+38 -1
View File
@@ -26,6 +26,7 @@ GRDB Associations
- [The Structure of a Joined Request]
- [Decoding a Joined Request with a Decodable Record]
- [Decoding a Joined Request with FetchableRecord]
- [DerivableRequest Protocol]
- [Known Issues]
- [Future Directions]
@@ -1245,6 +1246,41 @@ let country: Country? = row["country"]
You can also perform custom navigation in the tree by using *row scopes*. See [Row Adapters] for more information.
## DerivableRequest Protocol
The `DerivableRequest` protocol is adopted by both [query interface requests] such as `Author.all()` and associations such as `Book.author`. It is intended for you to use as a customization point when you want to extend the built-in GRDB apis.
For example:
```swift
extension DerivableRequest where RowDecoder == Author {
func filter(country: String) -> Self {
return filter(Column("country") == country)
}
func orderByFullName() -> Self {
return order(
Column("lastName").collating(.localizedCaseInsensitiveCompare),
Column("firstName").collating(.localizedCaseInsensitiveCompare))
}
}
```
Thanks to DerivableRequest, both the `filter(country:)` and `orderByFullName()` methods are now available for both Author-based requests and associations:
```swift
// French authors sorted by full name:
let request = Author.all()
.filter(country: "FR")
.orderByFullName()
// French books, sorted by full name of author:
let request = Book.joining(required: Book.author
.filter(country: "FR")
.orderByFullName())
```
## Known Issues
**You can't chain a required association on an optional association:**
@@ -1373,4 +1409,5 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[Custom Requests]: ../README.md#custom-requests
[Known Issues]: #known-issues
[Future Directions]: #future-directions
[Row Adapters]: ../README.md#row-adapters
[Row Adapters]: ../README.md#row-adapters
[query interface requests]: ../README.md#requests
+55 -9
View File
@@ -9,8 +9,14 @@ import GRDB
private struct Author: FetchableRecord, PersistableRecord, Codable {
var id: Int64
var name: String
var firstName: String?
var lastName: String?
var country: String
var fullName: String {
return [firstName, lastName]
.compactMap { $0 }
.joined(separator: " ")
}
static let databaseTableName = "author"
static let books = hasMany(Book.self)
@@ -35,7 +41,8 @@ private var libraryMigrator: DatabaseMigrator = {
migrator.registerMigration("library") { db in
try db.create(table: "author") { t in
t.autoIncrementedPrimaryKey("id")
t.column("name", .text).notNull()
t.column("firstName", .text)
t.column("lastName", .text)
t.column("country", .text).notNull()
}
try db.create(table: "book") { t in
@@ -45,11 +52,10 @@ private var libraryMigrator: DatabaseMigrator = {
}
}
migrator.registerMigration("fixture") { db in
try Author(id: 1, name: "Melville", country: "US").insert(db)
try Author(id: 2, name: "Proust", country: "FR").insert(db)
try Author(id: 1, firstName: "Herman", lastName: "Melville", country: "US").insert(db)
try Author(id: 2, firstName: "Marcel", lastName: "Proust", country: "FR").insert(db)
try Book(id: 1, authorId: 1, title: "Moby-Dick").insert(db)
try Book(id: 2, authorId: 2, title: "Du côté de chez Swann").insert(db)
try Book(id: 3, authorId: 2, title: "Le Côté de Guermantes").insert(db)
}
return migrator
}()
@@ -59,10 +65,16 @@ extension DerivableRequest where RowDecoder == Author {
func filter(country: String) -> Self {
return filter(Column("country") == country)
}
func orderByFullName() -> Self {
return order(
Column("lastName").collating(.localizedCaseInsensitiveCompare),
Column("firstName").collating(.localizedCaseInsensitiveCompare))
}
}
class DerivableRequestTests: GRDBTestCase {
func testDerivation() throws {
func testFilter() throws {
let dbQueue = try makeDatabaseQueue()
try libraryMigrator.migrate(dbQueue)
try dbQueue.inDatabase { db in
@@ -70,8 +82,8 @@ class DerivableRequestTests: GRDBTestCase {
let frenchAuthorNames = try Author.all()
.filter(country: "FR")
.fetchAll(db)
.map { $0.name }
XCTAssertEqual(frenchAuthorNames, ["Proust"])
.map { $0.fullName }
XCTAssertEqual(frenchAuthorNames, ["Marcel Proust"])
// ... for two requests (2)
let frenchBookTitles = try Book
@@ -79,7 +91,41 @@ class DerivableRequestTests: GRDBTestCase {
.order(Column("title"))
.fetchAll(db)
.map { $0.title }
XCTAssertEqual(frenchBookTitles, ["Du côté de chez Swann", "Le Côté de Guermantes"])
XCTAssertEqual(frenchBookTitles, ["Du côté de chez Swann"])
}
}
func testOrder() throws {
let dbQueue = try makeDatabaseQueue()
try libraryMigrator.migrate(dbQueue)
try dbQueue.inDatabase { db in
// ... for two requests (1)
let authorNames = try Author.all()
.orderByFullName()
.fetchAll(db)
.map { $0.fullName }
XCTAssertEqual(authorNames, ["Herman Melville", "Marcel Proust"])
let reversedAuthorNames = try Author.all()
.orderByFullName()
.reversed()
.fetchAll(db)
.map { $0.fullName }
XCTAssertEqual(reversedAuthorNames, ["Marcel Proust", "Herman Melville"])
// ... for two requests (2)
let bookTitles = try Book
.joining(required: Book.author.orderByFullName())
.fetchAll(db)
.map { $0.title }
XCTAssertEqual(bookTitles, ["Moby-Dick", "Du côté de chez Swann"])
let reversedBookTitles = try Book
.joining(required: Book.author.orderByFullName())
.reversed()
.fetchAll(db)
.map { $0.title }
XCTAssertEqual(reversedBookTitles, ["Du côté de chez Swann", "Moby-Dick"])
}
}
}