SQLDelight in Android: Getting Started
In this course, we will introduce you to SQLDelight, a generator for typesafe Kotlin code based on SQL database statements, with built-in support for migrations, schema validation and IDE integrations. From initial installation all the way to customization, you’ll learn everything there is to know about this latest invention from Square. By Marcel Schnelle.
Learning path
This is part of the Advanced Android Data & Networking learning path. View path.
Who is this for?
This course is for Android developers with an existing knowledge of and interest in SQLite, and those eager to learn about a new library to assist with the data persistence layer of their applications. If they have prior experience with comparable solutions for data persistence (e.g. Room, Realm), this course will be of value to them as well.
Covered concepts
- Installation
- Database Configuration
- Model Creation & Relationships
- CRUD Functions
- Column Types
- SQL Views
- Grouping
- Migrations
- Testing & Schema Validation
- RxJava Integration
- Coroutines Integration
- Paging Integration
Part 1: Preparation & Setup
Learn how SQLDelight is constructed and how it works on a high level on a multitude of platforms. Afterwards, install the required IDE and Gradle plugins to your environment and add the Android-specific driver library as a new dependency to the provided sample project used in this course.
Learn how to deviate from SQLDelight’s default configuration and tailor it to your specific needs via the domain-specific language exposed by its Gradle plugin.
Familiarize yourself with the .sq file format, used by SQLDelight to describe a table in the generated database. From the scope of the sample app, introduce the intended database relationships and construct the CREATE TABLE statements that make up these models.
Instantiate the Database object generated by SQLDelight and connect it to the existing sample app. Learn about how SQL Drivers are the backbone of the library and how to configure the Android driver specifically. Check out how the underlying AndroidX database can be surfaced for customization and fine-grained configuration.
Learn how SQLDelight stores the columns of generated tables in the underlying SQLite database. Show how different type aliases can affect the generated model classes. For advanced users, show how the library supports custom types through imports and column adapters.
Familiarize yourself with the syntax of functions in SQLDelight and how they add methods to the generated model classes. Using an exemplary workflow of the sample app, create code to insert data into the database and query it with a different function. Furthermore, learn how to get notified whenever the underlying dataset is changed.
Learn about a feature in SQLDelight called grouping statements, wherein multiple SQL statements can be combined under a single name to reduce complexity of the calling Kotlin code.
Learn about the Transaction API in SQLDelight, which allows you to execute multiple statements in Kotlin and commit them all at once. Furthermore, explore how the transaction can be rolled back in case of errors and how to be notified of events related to the ongoing transaction.
Learn how SQL migrations are supported in SQLDelight. This episode introduces the .sqm file format with which the database schema can be migrated to a later version.
Explore how the CREATE VIEW syntax can be used to add SQL Views to your database for common queries. Learn how this can help reduce the amount of types generated by SQLDelight.
Learn about some best practices for unit testing a database schema generated by SQLDelight. Furthermore, familiarize yourself with some ways to automatically validate SQL migrations by means of the Gradle plugin.
Part 2: Advanced SQLDelight Integrations
Learn about the integration library with RxJava 2 and 3, which allow SQLDelight’s Query type to be observed as RxJava Observables. Apply this extension to the core API of the sample app.
Learn about the integration library with Kotlin Coroutines, which allow SQLDelight’s Query type to be converted into Flows or suspending functions. Apply this extension to the core API of the sample app.
Learn about the integration library with Android Paging, which allows SQLDelight to provide a DataSource implementation that automatically pulls data from a table in a paginated fashion. Refactor the lists of the app to allow for potentially infinite scrolling, powered by Android Paging.