DataAdapter

abstract class DataAdapter<ID : Any>

Abstract class representing a data adapter. In charge of caching, storing and loading data for extensions, plugins, and other parts of your bot.

This class exists because it's intended for you to be able to create your own data adapters. As your bot is configured to use a single, global adapter, this allows you to switch up how your configuration is stored, as long as the eventual data translation happens via kotlinx.serialization.

As storage units may provide contextual information but may not necessarily point to different actual data objects, data adapters also include the concept of data IDs. These IDs are identifiers that provide a mapping between storage units and the actual data objects they refer to. As an example, a file-backed data adapter will likely use the path to the file here - so all storage units pointing to the same file will point at the same data object.

Data IDs allow you to point at contextual data sources when required, allowing for quite a few different possibilities in configuration. For example, different guilds, users, and so on may have their own separate configurations, if you so desire. It's up to your extensions to work with this system, and only provide the extra storage unit context that makes sense for the specific use-case.

Parameters

ID

A typevar representing what you use to identify specific instances of data storage, referred to as data IDs. This will often be a string, but you can use anything that can reasonably be used as the key for a map. File-based data adapters will likely just use the file path here.

Inheritors

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
abstract suspend fun <R : Data> delete(unit: StorageUnit<R>): Boolean

Entirely removes the data represented by the given storage unit from the dataCache, the disk if this is an adapter that stores data in files, or from whatever relevant persistent storage is being used.

Link copied to clipboard
abstract suspend fun <R : Data> get(unit: StorageUnit<R>): R?

Retrieve and return the data object represented by the given storage unit.

Link copied to clipboard
open suspend fun <R : Data> getOrSaveDefault(unit: StorageUnit<R>, data: suspend () -> R): R

Retrieve the data object represented by the given storage unit, or store the data object returned by the callback if no respective data could be found.

Link copied to clipboard
abstract suspend fun <R : Data> reload(unit: StorageUnit<R>): R?

Retrieve the data represented by the given storage unit from persistent storage, storing it in the dataCache and returning it if it was found.

Link copied to clipboard
open suspend fun reloadAll()

Reload all data objects stored in dataCache by calling reload against each storage unit.

Link copied to clipboard
abstract suspend fun <R : Data> save(unit: StorageUnit<R>): R?

Save the cached data represented by the given storage unit to persistent storage, creating any files and folders as needed.

abstract suspend fun <R : Data> save(unit: StorageUnit<R>, data: R): R

Save the given data represented by the given storage unit to persistent storage, creating any files and folders as needed, and storing the given data object in the dataCache.

Link copied to clipboard
open suspend fun saveAll()

Save all data objects stored in dataCache to persistent storage by calling save against each.