KeyValueStorage

open class KeyValueStorage<T : Any>(entityName: String, entityQueries: EntityQueries, metadataQueries: MetadataQueries, scope: CoroutineScope, type: KType, serializer: SqkonSerializer = KotlinSqkonSerializer(), config: KeyValueStorage.Config = Config(), readDispatcher: CoroutineDispatcher, writeDispatcher: CoroutineDispatcher, transacter: SqkonTransacter) : Transacter(source)

Base interaction to the database.

Parameters

serializer

if providing your own, recommend using SqkonJson to make sure you create fields consistently.

Constructors

Link copied to clipboard
constructor(entityName: String, entityQueries: EntityQueries, metadataQueries: MetadataQueries, scope: CoroutineScope, type: KType, serializer: SqkonSerializer = KotlinSqkonSerializer(), config: KeyValueStorage.Config = Config(), readDispatcher: CoroutineDispatcher, writeDispatcher: CoroutineDispatcher, transacter: SqkonTransacter)

Types

Link copied to clipboard
data class Config(val deserializePolicy: KeyValueStorage.Config.DeserializePolicy = DeserializePolicy.ERROR, val dispatcher: CoroutineDispatcher = Dispatchers.Default)

Functions

Link copied to clipboard
fun count(where: Where<T>? = null, expiresAfter: Instant? = null): Flow<Int>
Link copied to clipboard
fun delete(where: Where<T>? = null)

Delete using where clause. If where is null, all rows will be deleted.

Link copied to clipboard
fun deleteAll()

Delete all rows. Basically an alias for delete with no where set.

Link copied to clipboard

Delete by key.

Link copied to clipboard
fun deleteByKeys(vararg key: String)

Delete by keys.

Link copied to clipboard
fun deleteExpired(expiresAfter: Instant = Clock.System.now())

Purge all rows that have there expired_at field NOT null and less than (<) the date passed in. (Usually Clock.System.now).

Link copied to clipboard
fun deleteStale(writeInstant: Instant? = Clock.System.now(), readInstant: Instant? = Clock.System.now())

Unlike deleteExpired, this will clean up rows that have not been touched (read/written) before the passed in time.

Link copied to clipboard
fun deleteState(instant: Instant)

Unlike deleteExpired, this will clean up rows that have not been touched (read/written) before the passed in time.

Link copied to clipboard
fun insert(key: String, value: T, ignoreIfExists: Boolean = false, expiresAt: Instant? = null)

Insert a row.

Link copied to clipboard
fun insertAll(values: Map<String, T>, ignoreIfExists: Boolean = false, expiresAt: Instant? = null)

Insert multiple rows.

Link copied to clipboard

Metadata for the entity, this will tell you the last time the entity store was read and written to, useful for cache invalidation.

Link copied to clipboard
fun select(where: Where<T>? = null, orderBy: List<OrderBy<T>> = emptyList(), limit: Long? = null, offset: Long? = null, expiresAfter: Instant? = null): Flow<List<T>>

Select using where clause. If where is null, all rows will be selected.

Link copied to clipboard
fun selectAll(orderBy: List<OrderBy<T>> = emptyList(), expiresAfter: Instant? = null): Flow<List<T>>

Select all rows. Effectively an alias for select with no where set.

Link copied to clipboard
fun selectByKey(key: String): Flow<T?>

Select by key.

Link copied to clipboard
fun selectByKeys(keys: Collection<String>, orderBy: List<OrderBy<T>> = emptyList(), expiresAfter: Instant? = null): Flow<List<T>>

Select by keys with optional ordering

Link copied to clipboard
fun selectKeysetPagingSource(pageSize: Int, where: Where<T>? = null, orderBy: List<OrderBy<T>> = emptyList(), expiresAfter: Instant? = null): PagingSource<String, T>

Create a PagingSource that pages through results using keyset-based pagination. Unlike selectPagingSource, this avoids the O(n) cost of SQL OFFSET on large datasets by using pre-calculated page boundary keys.

Link copied to clipboard
fun selectPagingSource(where: Where<T>? = null, orderBy: List<OrderBy<T>> = emptyList(), initialOffset: Int = 0, expiresAfter: Instant? = null): PagingSource<Int, T>

Create a PagingSource that pages through results according to queries generated by from the passed in where and orderBy. initialOffset initial offset to start paging from.

Link copied to clipboard
fun selectResult(where: Where<T>? = null, orderBy: List<OrderBy<T>> = emptyList(), limit: Long? = null, offset: Long? = null, expiresAfter: Instant? = null): Flow<List<ResultRow<T>>>

Select using where clause. If where is null, all rows will be selected.

Link copied to clipboard
open override fun transaction(noEnclosing: Boolean, body: TransactionWithoutReturn.() -> Unit)
Link copied to clipboard
open override fun <R> transactionWithResult(noEnclosing: Boolean, bodyWithReturn: TransactionWithReturn<R>.() -> R): R
Link copied to clipboard
fun update(key: String, value: T, expiresAt: Instant? = null)

Update a row. If the row does not exist, it will update nothing, use insert if you want to insert if the row does not exist.

Link copied to clipboard
fun updateAll(values: Map<String, T>, expiresAt: Instant? = null)

Convenience function to insert collection of rows. If the row does not exist, ti will update nothing, use insert if you want to insert if the row does not exist.

Link copied to clipboard
fun upsert(key: String, value: T, expiresAt: Instant? = null)

Convenience function to insert a new row or update an existing row.

Link copied to clipboard
fun upsertAll(values: Map<String, T>, expiresAt: Instant? = null)

Convenience function to insert new or update existing multiple rows.