GRDB.swift
A toolkit for SQLite databases, with a focus on application development
A Swift library for working with SQLite on iOS, macOS, tvOS, and watchOS that lets you read and write databases using Swift types, with built-in concurrency and live query observation.
GRDB.swift is a Swift library for working with SQLite databases in Apple platform applications, covering iOS, macOS, tvOS, and watchOS. SQLite is a small, self-contained database that stores data in a single file on the device. GRDB provides a layer on top of SQLite that makes it easier to use from Swift code without having to write raw SQL for most common operations, while still letting you write SQL directly when you need to.
The library gives you several ways to interact with a database. You can define Swift structs or classes that map to database tables, and GRDB handles converting between your Swift types and the database rows. Once you define a record type, you can insert, update, delete, and fetch records using Swift syntax instead of SQL strings. For developers who prefer or need to write SQL directly, that is also fully supported. The two approaches can be mixed freely within the same project.
One of the library's notable features is database observation. You can set up a query to watch, and GRDB will call your code automatically whenever the result of that query changes, for example when a record is inserted or updated. This integrates with Swift concurrency, Combine, and RxSwift, which are different ways of handling asynchronous updates in iOS and macOS apps.
Concurrency handling is built in, so multiple parts of an application can read and write to the database at the same time without corrupting data. The library also includes a migrations system, which is a structured way to update your database structure when you release a new version of your app without losing existing data.
Installation is available through Swift Package Manager, which is the standard dependency manager for Swift projects. The library has been actively maintained since 2015 and is on version 7, with detailed documentation hosted in the repository itself covering every feature area. The full README is longer than what was shown.
Where it fits
- Define Swift structs that map to SQLite tables and insert, fetch, and delete records without writing SQL strings.
- Set up a live database query that automatically refreshes your SwiftUI view when records are inserted or updated.
- Run database migrations safely when shipping a new version of your iOS or macOS app without losing existing data.
- Mix raw SQL queries with Swift record types in the same app for maximum control and flexibility.