Room Database
Room is a persistence library built into the Android Jetpack suite. It provides a simplified and type-safe approach to interacting with SQLite databases on Android. Room acts as an abstraction layer, automating repetitive tasks like schema definition, database creation, and SQL queries. This reduces boilerplate code and streamlines database access within your applications.
Usage
Dependencies
Within your app
module’s build.gradle.kts
file, incorporate the following dependencies:
implementation(project(":storage:room"))
Do note that in build.gradle.kts
for the :storage:room
module, we have specified that it should generate the code in Kotlin. This is a new feature in Room 2.6.0, if you prefer Java then you must remove it there.
ksp {
arg("room.schemaLocation", "$projectDir/schemas")
arg("room.generateKotlin", "true") // You can either remove this or set it to false for Java generation
}
Example
The AppDatabase
class serves as the entry point for accessing your Room database. You can define your database entities and access objects within this class.
This class has been created for you in the :storage:room
module all you need to do is get the instance from it.
We provide 2 ways of getting the instance of the database. You can use the getInstance
method to get the instance of the database or you can use the create
method to create a new instance of the database.
If you don’t use a Dependency Injection framework it is recommended to use the getInstance
method to get the instance of the database. This makes sure that you are using the same instance of the database throughout your app:
val db = AppDatabase.getInstance(context)
If you are using a Dependency Injection framework like Koin you can use the create
method to create a new instance of the database and provide it as a singleton:
single {
AppDatabase.create(androidContext())
}
The next step is to create your entities. An entity is a class that represents a table in your database. You can define your entities as data classes and annotate them with the @Entity
annotation.
@Entity(tableName = DummyEntity.TABLE_NAME)
data class DummyEntity(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = COLUMN_ID)
val id: Long = 0,
@ColumnInfo(name = COLUMN_NAME)
val name: String
) {
companion object {
const val TABLE_NAME = "dummy_table"
const val COLUMN_ID = "id"
const val COLUMN_NAME = "name"
}
}
Finally, you can create your DAOs. A DAO (Data Access Object) is an interface that defines the methods for interacting with your database. You can define your DAOs as interfaces and annotate them with the @Dao
annotation.
@Dao
interface DummyDao {
// Your methods here
}
We also provide a RoomDao
class that you can extend to create your own DAOs. This class provides some basic methods for performing CRUD operations on your database. You can extend this class to create a DAO for your entities like this:
@Dao
abstract class DummyDao: RoomDao<DummyEntity>(DummyEntity.TABLE_NAME) {
override val idColumnInfo: String
get() = DummyEntity.COLUMN_ID
}
More information on Room can be found in the official documentation .