KeyValueStore
KeyValueStore =
object
KeyValueStore is a key-value storage mechanism. It provides an asynchronous API for storing, retrieving, updating,
and deleting key-value pairs. The store is persistent; the data is stored in a SQLite database that's
automatically backed up and restored by Root.
The key is a string and the value is generic type parameter. When loading data into the key-value store,
Root uses JSON.stringify to convert the value into a string for storage. On the way out, Root uses JSON.parse
to convert back to the type-parameter type.
Root automatically creates an instance of KeyValueStore and exposes it via the rootServer.dataStore.appData property.
Methods
delete()
delete(
key:string):Promise<void>
Deletes a key-value pair from the store by key.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key to delete. |
Returns
Promise<void>
A promise that resolves when the key is deleted.
Example
import { rootServer } from "@rootsdk/server-app";
await rootServer.dataStore.appData.delete("myKey");
deleteLike()
deleteLike(
keyPattern:string):Promise<void>
Deletes key-value pairs from the store that match a pattern.
Parameters
| Parameter | Type | Description |
|---|---|---|
keyPattern | string | The pattern to match keys against ('_' matches a single character, '%' matches any sequence of zero or more characters, use '\' to escape the special characters '_' and '%'). |
Returns
Promise<void>
A promise that resolves when the matching keys are deleted.
Example
import { rootServer } from "@rootsdk/server-app";
await rootServer.dataStore.appData.deleteLike("user%");
get()
get<
T>(key:string):Promise<T|undefined>
Retrieves a stored value associated with the given key.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The expected type of the stored value. |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | string | The key associated with the stored value. |
Returns
Promise<T | undefined>
A promise that resolves to the stored value or undefined if not found.
Example
import { rootServer } from "@rootsdk/server-app";
const value = await rootServer.dataStore.appData.get<string>("myKey");
select()
select<
T>(keyPattern:string):Promise<KeyValue<T>[]>
Selects key-value pairs from the store that match a key pattern.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of the values to retrieve. |