Skip to content

Commit

Permalink
[ci skip] update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kotlitecture committed Jul 13, 2024
1 parent 41578b3 commit 47062c3
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ The generated project will include a similar table in its README.MD file, but wi
| Dataflow | Facade Analytics API | [Link](docs/Dataflow/Facade%20Analytics%20API/overview.md) | - | [Link](docs/Dataflow/Facade%20Analytics%20API/usage.md) |
| Dataflow | Facade Config API | [Link](docs/Dataflow/Facade%20Config%20API/overview.md) | - | [Link](docs/Dataflow/Facade%20Config%20API/usage.md) |
| Dataflow | SQLDelight | [Link](docs/Dataflow/SQLDelight/overview.md) | - | [Link](docs/Dataflow/SQLDelight/usage.md) |
| Dataflow | SQLite (Room) | [Link](docs/Dataflow/SQLite%20%28Room%29/overview.md) | - | [Link](docs/Dataflow/SQLite%20%28Room%29/usage.md) |
| Dataflow | Multiplatform Settings | [Link](docs/Dataflow/Multiplatform%20Settings/overview.md) | - | [Link](docs/Dataflow/Multiplatform%20Settings/usage.md) |
| Dataflow | Basic In-Memory Cache API | [Link](docs/Dataflow/Basic%20In-Memory%20Cache%20API/overview.md) | - | [Link](docs/Dataflow/Basic%20In-Memory%20Cache%20API/usage.md) |
| Dataflow | Ktor Client | [Link](docs/Dataflow/Ktor%20Client/overview.md) | - | [Link](docs/Dataflow/Ktor%20Client/usage.md) |
Expand Down
1 change: 0 additions & 1 deletion docs/Dataflow/Basic In-Memory Cache API/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Facade **CacheSource** provides the following methods:
- `invalidate(key: K)` - Invalidates the cache entry associated with the specified key.
- `remove(type: Class<K>)` - Removes all cache entries associated with the specified key type.
- `remove(key: K)` - Removes the cache entry associated with the specified key.
- `put(key: CacheKey<T>, value: T)` - Associates the specified value with the specified key in the cache.
- `clear()` - Clears all entries from the cache.

## Example
Expand Down
8 changes: 8 additions & 0 deletions docs/Dataflow/SQLite (Room)/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# About

Save data in a local database using Room. The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

# Links

- [Official Site](https://developer.android.com/kotlin/multiplatform/room)
- [Integration Guide](https://developer.android.com/kotlin/multiplatform/room#defining-database)
116 changes: 116 additions & 0 deletions docs/Dataflow/SQLite (Room)/usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Usage

## Overview

- Component package: `app.data.source.database.room`
- DI integration: `app.di.data.RoomSourceModule`

The integration includes the following components:

- **AppDatabase**: A pre-configured Room database designed to register all entities and DAO objects.
- **AppRoomSource**: Serves as a holder of the AppDatabase instance and acts as a service locator for all DAO objects.
- **User** and **UserDao**: An example entity and its associated DAO object. These classes serve as templates that can be used to create your own entities and DAOs.

## Create new Entity and DAO

The official guide for defining entities can be found here: [https://developer.android.com/training/data-storage/room/defining-data](https://developer.android.com/training/data-storage/room/defining-data)

Imagine you need to add a new entity called **Address**. Here are the steps to follow.

### 1. Create the `Address` entity class

You can use the `User` class in `app.data.source.database.room.entity` as a template.

```kotlin
@Entity(tableName = "address")
data class Address(
@PrimaryKey(autoGenerate = true)
var id: Int = 0,
@ColumnInfo(name = "country")
var country: String? = null,
@ColumnInfo(name = "city")
var city: String? = null,
@ColumnInfo(name = "street")
var street: String? = null,
)
```

### 2. Create the `AddressDao` interface

You can use the `UserDao` interface in `app.data.source.database.room.dao` as a template.

```kotlin
@Dao
interface AddressDao {
@Insert
fun create(vararg addresses: Address)
@Update
fun update(vararg addresses: Address)
@Delete
fun delete(vararg addresses: Address)
@Query("SELECT * FROM address WHERE id = :id LIMIT 1")
fun get(id: Long): Address?
@Query("SELECT * FROM address")
fun getAll(): List<Address>
@Query("SELECT * FROM address")
fun getAllAsFlow(): Flow<Address>
}
```

### 3. Register `Address` and `AddressDao` in `AppDatabase`

```kotlin
@Database(
entities = [
Address::class
],
version = 1
)
abstract class AppDatabase : RoomDatabase() {

abstract fun getAddressDao(): AddressDao

}
```

### 4. Register `AddressDao` in `AppRoomSource`

```kotlin
class AppRoomSource(
private val databaseName: String = "db"
) {
...
val addressDao by lazy { db.getAddressDao() }
...
}
```

## Usage of DAO in your code

In this example, we will directly use `AppRoomSource` from the `ViewModel` to access `AddressDao`. However, it is recommended to create a separate `Repository` layer and call all data sources from there.

```kotlin
class AddressViewModel(
private val roomSource: AppRoomSource,
private val appState: AppState
) : BaseViewModel() {

val addressesStore = DataState<List<Address>>()

override fun doBind() = launchAsync("getAll") {
val addressDao = roomSource.addressDao
addressDao.getAllAsFlow().collectLatest(addressesStore::set)
}

fun onCreate(address: Address) = launchAsync("onCreate", appState) {
val addressDao = roomSource.addressDao
addressDao.create(address)
}

fun onDelete(address: Address) = launchAsync("onRemove", appState) {
val addressDao = roomSource.addressDao
addressDao.delete(address)
}

}
```

0 comments on commit 47062c3

Please sign in to comment.