An open API service indexing awesome lists of open source software.

https://github.com/androidpoet/kdb


https://github.com/androidpoet/kdb

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

          

# KDB: Kotlin Multiplatform SQLite Wrapper


KDB Logo

KDB is a lightweight, type-safe SQLite wrapper for Kotlin Multiplatform.

## Modules

| Module | Description |
| :--- | :--- |
| `kdb` | Core library (driver + schema + query + paging + client API) |
| `kdb-paging3` | Optional Paging 3 integration |

## API Design

- Primary API returns `KdbResult`:
- `open`, `migrate`, `close`
- `insert`, `updateById`, `deleteById`, `getById`, `list`
- Convenience `suspend` background APIs throw on failure:
- `openOrThrow`, `migrateOrThrow`, `closeOrThrow`
- `insertOrThrow`, `updateByIdOrThrow`, `deleteByIdOrThrow`, `getByIdOrThrow`, `listOrThrow`
- Transaction helper:
- `suspend fun tx { ... }`

## Configuration

```kotlin
val kdb = createKdb(driver) {
entities(Task.serializer())
// optional, default: Dispatchers.Default
// dispatcher = Dispatchers.IO
}
```

## Quick Start (Result-first)

```kotlin
@Serializable
data class Task(val id: Long, val title: String, val done: Boolean)

val driver = CommonKdbDriver("my_app.db")
val kdb = createKdb(driver) {
entities(Task.serializer())
}

kdb.open()
kdb.migrate(
migration(1) {
createTable("task")
// or raw SQL when needed:
// sql("CREATE INDEX IF NOT EXISTS idx_task_done ON task(done)")
}
)

kdb.insert(Task(1, "Ship KDB", false))
kdb.updateById(1, Task(1, "Ship KDB v1", false))
val one = kdb.getById(1)
kdb.deleteById(1)

val page = kdb.list(limit = 20, afterId = null) { it.id }
```

## Convenience (Suspend + Throwing)

```kotlin
kdb.openOrThrow()
kdb.insertOrThrow(Task(2, "A", false))

val page = kdb.listOrThrow(limit = 20) { it.id }

kdb.tx {
insert(Task(3, "B", false)).getOrThrow()
insert(Task(4, "C", false)).getOrThrow()
}
```

## License

KDB is licensed under the MIT License. See [LICENSE](LICENSE).