Skip to content

A powerful data fetching and caching library for Kotlin Multiplatform, built for speed, simplicity, and reliability.

License

Notifications You must be signed in to change notification settings

abappi19/kmp-query

Repository files navigation

KMP Query (Kotlin Multiplatform Query)

Maven Central

A powerful data fetching and caching library for Kotlin Multiplatform, built for speed, simplicity, and reliability.

Features

  • Declarative query definition & execution
  • Automatic background refetching
  • Multiplatform caching layer
  • Observables for UI integration
  • Customizable retry policies
  • Automatic garbage collection
  • Type-safe API surface

Installation

Add the dependency to your common source set:

commonMain.dependencies {
    implementation("io.github.abappi19:kmp-query:1.0.0")
}

Basic Usage

val queryClient = QueryClient(config = QueryConfig(
    cacheTime = Duration.minutes(5),
    staleTime = Duration.seconds(30)
))

// Create query
val userQuery = queryClient.createQuery(
    queryKey = listOf("user", userId),
    queryFn = { fetchUser(userId) }
)

// React to state changes
userQuery.observe { state ->
    when {
        state.isLoading -> showLoading()
        state.isError -> showError(state.error)
        else -> displayUser(state.data)
    }
}

// Manual invalidation
fun updateProfile() {
    queryClient.invalidateQueries(listOf("user", userId))
}

Advanced Patterns

Parallel Queries

val postsQuery = queryClient.createQuery(listOf("posts"), fetchPosts)
val commentsQuery = queryClient.createQuery(listOf("comments"), fetchComments)

// Combine using coroutines
val combinedData = suspend {
    awaitAll(postsQuery.await(), commentsQuery.await())
}

Mutations

val updateUser = queryClient.createMutation(
    mutationFn = { user -> api.updateUser(user) },
    onSuccess = { invalidateUserQueries(it.id) }
)

Contributing

See for guidelines.

License

Distributed under Apache 2.0 License

About

A powerful data fetching and caching library for Kotlin Multiplatform, built for speed, simplicity, and reliability.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 10

Languages