GRDB Stories

Gwendal Roué
Mar 31, 2016 · 1 min read

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:

Written by

I’m an iOS app developer at, and author of a few open source libraries:

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade