Hello Swift Community,
Version 4.1 of GRDB, the toolkit for SQLite databases, is out! RxGRDB 0.15.0, its reactive companion based on RxSwift, has been released as well.
Asynchronous APIs are now available for both GRDB and RxGRDB.
The DerivableRequest protocol, the GRDB equivalent of Active Record Scopes, can now perform joins and eager load association records.
/// Extends the query language for Book requests and associations:
extension DerivableRequest where RowDecoder == Book {
// Filters books by country
func filter(countryCode: String) -> Self {
// A book is from a country if it can be joined
// to an author from this country:
let filteredAuthor = Book.author
.filter(Column("countryCode") == countryCode)
return self.joining(required: filteredAuthor)
}
}
// Usage
// To fetch all French books, we use `filter(countryCode:)`
// on a request:
let frenchBooks: [Book] = try Book.all()
.filter(countryCode: "FR")
.fetchAll(db)
// To fetch all libraries that contain Italian books, we use
// `filter(countryCode:)` on an association:
let libraries: [Library] = try Library
.having(Library.books.filter(countryCode: "IT").isEmpty == false)
.fetchAll(db)
For more information about DerivableRequest, see the "Define Record Requests" chapter in the updated Good Practices for Designing Record Types.
And check the Release Notes for a full list of improvements and other changes.
Happy GRDB!