GRDB Stories

Gwendal Roué
1 min readMar 31, 2016

--

The little things that make the SQLite library GRDB.swift different.

Given two database tables, persons and books, one often expects that an object mapping should give two isolated classes: Person and Book.

This is the very common pattern in Object Relational Mappings, such as Core Data, ActiveRecord, and many others. One table, one class.

Well, this does not have to be that way.

GRDB distinguishes the ability to be fetched from the ability to be persisted in the database. They can match, of course, and involve a single identical database table. But fetchable types can actually feed on any fetch request.

For example, our Person type below can load a “bookCount” property that is not a column of the “persons” table:

Now it’s natural to fetch persons and their book count without fetching all their books, using a simple join query:

Oh, and since the bookCount property is optional, a Person can be loaded from requests that do not contain the “bookCount” column as well:

https://github.com/groue/GRDB.swift

--

--