Jetpack Compose: Using Room Database

How to use Room Database in Jetpack Compose Apps?

Mansi Shah
Jetpack Composers
Published in
3 min readSep 26, 2022

--

Room Persistence Library

Room Database is now highly recommended for local storage in Android development and one of the android jetpack’s architecture components. It is a wrapper for SQL DB, maps database objects to java objects automatically, and reduces boilerplate code.

It has 3 main components:
1. Database — Abstract class or Singleton
2. Entity (table) — Data class
3. DAO (Database Access Object) — Interface class, it makes queries on the Entity using API.

Room Library works with Observable frameworks like Rxjava & Live data.

Add/Edit Employee Detail 📝

We have an HR Operations app where HR can Add/Update Employee details. For this purpose, we will use Room DB to store all employee’s details. I have used a dagger-hilt for dependency injection.

Let’s see step by step how it works with Compose.

  • Setup Room: Add the below dependencies in your build.gradle file.
  • Prepare a Table: Make a Data class Employee.kt using annotation Entity. In this example, we have made employees data Entity, which represents the employees table in DB, and each variable inside it represents a column inside the table. You can declare column details using the annotation ColumnInfo.
  • Prepare Queries: Make an interface class EmployeeDao.kt and annotate with Dao (Data Access Object). In this class, we need to declare all queries to fetch data from our table. We have annotations available to Insert, Update or Delete the record from the table in DB. For others, we need to use Query annotation.

DAO checks for queries on compile time & gives an error if found any.

  • Create a Room DB: Create an abstract class EmployeeRoomDatabase.kt using the annotation Database and extend RoomDatabase. Declare a list of entities, version, etc. EmployeeRoomDatabase class must define an abstract method that has zero arguments and returns an instance of the EmployeeDao class.
  • Prepare Repository to access DAO: Make a Repository class EmployeeRepository.kt and add our Dao class to the constructor. Using the coroutine repository will access Dao to fire a query on the table. In the below code, the addEmployee method of employeeDao class is accessed inside a coroutine scope.
  • Make a ViewModel class HomeViewModel.kt, which will interact with the repository class to get data from DB.
  • Make a function in ComponentActivity: In AddEditEmployeeScreen class, make a function to fire a query & get a result from EmployeeRoomDatabase. For this purpose, we will use our HomeViewModel class where all methods are already defined.
  • Call the addEmployeeInDB function on the button click and add an employee detail to our DB.

That’s a wrap for now! Many more Jetpack Compose topics to come🔥
You can access the source code from Github.
I hope you find this blog helpful.

--

--

Mansi Shah
Jetpack Composers

Lead Android Engineer. Hands-on experience in Java, Kotlin, and Dart. Obsessed with Jetpack Compose! Travel Geek. Painting Lover. https://linktr.ee/mansik15