This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
ColumnAdapter Generation
Kaustubh Patange edited this page Aug 23, 2021
·
4 revisions
API might be unstable if you find any issues feel free to report them.
Eliminates the need for writing a custom ColumnAdapter
for SQLDelight, check sample-ktx app to see a running sample.
Supports serialization with Gson, Moshi, kotlinx-serialization.
implementation 'io.github.kaustubhpatange:autobindings-sqldelight:<version>'
// Kotlin
apply plugin: 'kotlin-kapt' // at top of your module build.gradle file
kapt 'io.github.kaustubhpatange:autobindings-compiler:<version>'
// Java
annotationProcessor 'io.github.kaustubhpatange:autobindings-compiler:<version>'
- Each column adapter is backed (enclosed) by an
interface
. So you can declare multiple adapters in the sameinterface
which will generate aImpl
class ready for use.
@JsonClass(generateAdapter = true) // Required for Moshi
data class User(val name: String, val age: Int)
// Serialize with Moshi. Make sure to add the dependency
@AutoGenerateSQLDelightAdapters(using = ConverterType.MOSHI)
interface SQLDelightAdapters {
fun dataConverter(): ColumnAdapter<User, String>
// Support for List<T>
fun dataListConverter(): ColumnAdapter<List<User>, String>
// Support for Map<K, V>, Pair<K, V>, Dictionary<K, V>
fun dataMapConverter(): ColumnAdapter<Map<String, User>, String>
}
- You must always ensure that the second argument for
ColumnAdapter
must beString
so they can be mapped to aTEXT
data otherwise processor will throw an exception. - The resulting class will implement the
interface
& will generate all the necessary code. - The
Impl
class can then be used as shown below. You can also inject thisinterface
with a dependency injection framework and use it (similar to retrofit).
// usage
val database = Database(driver, DataDomainAdapter = DataDomain.Adapter(
dataListAdapter = SQLDelightAdaptersImpl.dataListConverter
))