Storage

interface Storage

Typed key-value storage interface with observation capabilities.

Provides methods for storing and retrieving primitive values grouped by named scopes, as well as entity persistence via serialization adapters. All mutating operations are suspend functions and should be called from a coroutine context.

Observations return Kotlin Flow instances that emit the current value(s) on every change.

Example:

val storage = Keystone.Storage.create(name = "my_app")
storage.putString("settings", "key", "value")
val value = storage.getString("settings", "key")
storage.observe("settings") { string("key") }.collect { /* react */}

Types

Link copied to clipboard
object Companion

Factory for creating Storage instances.

Functions

Link copied to clipboard
abstract suspend fun clearAll()

Clears all keys and entities across every scope.

Link copied to clipboard
abstract suspend fun <T : Any> clearEntities(clazz: KClass<T>)

Removes all stored entities of type T.

Link copied to clipboard
abstract suspend fun clearScope(scope: String)

Clears all keys and entities within the given scope.

Link copied to clipboard
abstract suspend fun contains(scope: String, key: String): Boolean

Checks whether a key exists in the given scope.

Link copied to clipboard
abstract suspend fun getBoolean(scope: String, key: String, defaultValue: Boolean = false): Boolean

Retrieves a Boolean value by key from the given scope.

Link copied to clipboard
abstract suspend fun getDouble(scope: String, key: String, defaultValue: Double = 0.0): Double

Retrieves a Double value by key from the given scope.

Link copied to clipboard
abstract suspend fun <T : Any> getEntity(clazz: KClass<T>, id: String): T?

Retrieves a single entity of type T by its id.

Link copied to clipboard
abstract suspend fun getFloat(scope: String, key: String, defaultValue: Float = 0.0f): Float

Retrieves a Float value by key from the given scope.

Link copied to clipboard
abstract suspend fun getInt(scope: String, key: String, defaultValue: Int = 0): Int

Retrieves an Int value by key from the given scope.

Link copied to clipboard
abstract suspend fun getLong(scope: String, key: String, defaultValue: Long = 0): Long

Retrieves a Long value by key from the given scope.

Link copied to clipboard
abstract suspend fun getSnapshot(scope: String): Keystone.Snapshot

Returns a full Snapshot of all key-value pairs in the given scope.

Link copied to clipboard
abstract suspend fun getString(scope: String, key: String, defaultValue: String = ""): String

Retrieves a String value by key from the given scope.

Link copied to clipboard
abstract fun observe(scope: String): Flow<Keystone.Snapshot>

Observes all keys in the given scope, emitting a Snapshot on every change.

abstract fun observe(scope: String, block: Keystone.ObservationBuilder.() -> Unit): Flow<Keystone.Snapshot>

Observes keys defined by the block DSL in the given scope.

abstract fun observe(scope: String, keys: Set<Keystone.Observable>): Flow<Keystone.Snapshot>

Observes a specific set of keys in the given scope.

Link copied to clipboard
abstract fun observeAll(): Flow<Map<String, Keystone.Snapshot>>

Observes all scopes, emitting a map from scope name to Snapshot on every change.

Link copied to clipboard
abstract fun <T : Any> observeEntities(clazz: KClass<T>): Flow<List<T>>

Observes all entities of type T, emitting the full list on each change.

Link copied to clipboard
abstract fun <T : Any> observeEntity(clazz: KClass<T>, id: String): Flow<T?>

Observes a single entity of type T by its id, emitting the updated entity on changes.

Link copied to clipboard
abstract suspend fun putBoolean(scope: String, key: String, value: Boolean)

Stores a Boolean value under the given key in the specified scope.

Link copied to clipboard
abstract suspend fun putDouble(scope: String, key: String, value: Double)

Stores a Double value under the given key in the specified scope.

Link copied to clipboard
abstract suspend fun <T : Any> putEntity(entity: T)

Stores an entity, persisting all of its mapped properties.

Link copied to clipboard
abstract suspend fun putFloat(scope: String, key: String, value: Float)

Stores a Float value under the given key in the specified scope.

Link copied to clipboard
abstract suspend fun putInt(scope: String, key: String, value: Int)

Stores an Int value under the given key in the specified scope.

Link copied to clipboard
abstract suspend fun putLong(scope: String, key: String, value: Long)

Stores a Long value under the given key in the specified scope.

Link copied to clipboard
abstract suspend fun putString(scope: String, key: String, value: String)

Stores a String value under the given key in the specified scope.

Link copied to clipboard
abstract suspend fun remove(scope: String, key: String)

Removes the value at the given key from the specified scope.

Link copied to clipboard
abstract suspend fun <T : Any> removeEntity(clazz: KClass<T>, id: String)

Removes the entity of type T with the given id.