explaingit

groue/grdb.swift

8,382SwiftAudience · developerComplexity · 3/5Setup · moderate

TLDR

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.

Mindmap

mindmap
  root((grdb.swift))
    What it does
      SQLite for Apple apps
      Swift type mapping
      Live query observation
    Core Features
      Record types
      Raw SQL support
      Migrations system
      Concurrency safe
    Platforms
      iOS
      macOS
      tvOS and watchOS
    Integrations
      Swift concurrency
      Combine
      RxSwift
    Audience
      iOS developers
      macOS developers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Define Swift structs that map to SQLite tables and insert, fetch, and delete records without writing SQL strings.

USE CASE 2

Set up a live database query that automatically refreshes your SwiftUI view when records are inserted or updated.

USE CASE 3

Run database migrations safely when shipping a new version of your iOS or macOS app without losing existing data.

USE CASE 4

Mix raw SQL queries with Swift record types in the same app for maximum control and flexibility.

Tech stack

SwiftSQLiteSwift Package Manager

Getting it running

Difficulty · moderate Time to first run · 30min

Install via Swift Package Manager, no external services required, but schema design and migrations need upfront planning.

In plain English

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.

Copy-paste prompts

Prompt 1
Show me how to define a Swift struct as a GRDB record type for a 'Task' table with id, title, and completed fields, then insert and fetch records.
Prompt 2
Using GRDB.swift, write code to observe a database query in a SwiftUI view so the list updates automatically whenever tasks are added or deleted.
Prompt 3
How do I set up database migrations in GRDB.swift to add a new column to an existing table without losing data when my iOS app updates?
Prompt 4
Write a GRDB.swift database manager class that safely handles concurrent reads and writes in a multi-threaded iOS app.
Prompt 5
Show me how to write a raw SQL SELECT query in GRDB.swift and map the results to a custom Swift struct.
Open on GitHub → Explain another repo

← groue on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.