2024-03-16 22:29:24 +08:00
|
|
|
import { syscall } from "../syscall.ts";
|
2024-07-30 23:33:33 +08:00
|
|
|
import type { KV, KvKey, KvQuery } from "../types.ts";
|
2023-10-03 20:16:33 +08:00
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Exposes a key value story with query capabilities.
|
2024-08-09 15:27:58 +08:00
|
|
|
* @module
|
2024-08-07 19:27:25 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a value in the key value store.
|
|
|
|
* @param key the key to set
|
|
|
|
* @param value the value to set
|
|
|
|
*/
|
2023-10-03 20:16:33 +08:00
|
|
|
export function set(key: KvKey, value: any): Promise<void> {
|
|
|
|
return syscall("datastore.set", key, value);
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Sets multiple values in the key value store.
|
|
|
|
* @param kvs the key value pairs to set
|
|
|
|
*/
|
2023-10-03 20:16:33 +08:00
|
|
|
export function batchSet(kvs: KV[]): Promise<void> {
|
|
|
|
return syscall("datastore.batchSet", kvs);
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Gets a value from the key value store.
|
|
|
|
* @param key the key to get
|
|
|
|
* @returns the value associated with the key (or undefined if not found)
|
|
|
|
*/
|
|
|
|
export function get(key: KvKey): Promise<any | undefined> {
|
2023-10-03 20:16:33 +08:00
|
|
|
return syscall("datastore.get", key);
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Gets multiple values from the key value store.
|
|
|
|
* @param keys the keys to get
|
|
|
|
* @returns the values associated with the keys (or undefined if not found)
|
|
|
|
*/
|
2023-10-03 20:16:33 +08:00
|
|
|
export function batchGet(keys: KvKey[]): Promise<(any | undefined)[]> {
|
|
|
|
return syscall("datastore.batchGet", keys);
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Deletes a value from the key value store.
|
|
|
|
* @param key the key to delete
|
|
|
|
*/
|
2023-10-03 20:16:33 +08:00
|
|
|
export function del(key: KvKey): Promise<void> {
|
|
|
|
return syscall("datastore.delete", key);
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Deletes multiple values from the key value store.
|
|
|
|
* @param keys the keys to delete
|
|
|
|
*/
|
2023-10-03 20:16:33 +08:00
|
|
|
export function batchDel(keys: KvKey[]): Promise<void> {
|
|
|
|
return syscall("datastore.batchDelete", keys);
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Queries the key value store.
|
|
|
|
* @param query the query to run
|
|
|
|
* @param variables the variables that can be referenced inside the query
|
|
|
|
* @returns the results of the query
|
|
|
|
*/
|
2023-10-03 20:16:33 +08:00
|
|
|
export function query(
|
|
|
|
query: KvQuery,
|
2024-02-03 02:19:07 +08:00
|
|
|
variables: Record<string, any> = {},
|
2023-10-03 20:16:33 +08:00
|
|
|
): Promise<KV[]> {
|
2024-02-03 02:19:07 +08:00
|
|
|
return syscall("datastore.query", query, variables);
|
2023-10-03 20:16:33 +08:00
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Queries the key value store and deletes all matching items
|
|
|
|
* @param query the query to run
|
|
|
|
* @param variables the variables that can be referenced inside the query
|
|
|
|
*/
|
2023-10-03 20:16:33 +08:00
|
|
|
export function queryDelete(
|
|
|
|
query: KvQuery,
|
2024-02-03 02:19:07 +08:00
|
|
|
variables?: Record<string, any>,
|
2023-10-03 20:16:33 +08:00
|
|
|
): Promise<void> {
|
2024-02-03 02:19:07 +08:00
|
|
|
return syscall("datastore.queryDelete", query, variables);
|
|
|
|
}
|
|
|
|
|
2024-08-07 19:27:25 +08:00
|
|
|
/**
|
|
|
|
* Lists all functions currently defined and available for use in queries
|
|
|
|
* @returns the names of all functions in the key value store
|
|
|
|
*/
|
2024-02-03 02:19:07 +08:00
|
|
|
export function listFunctions(): Promise<string[]> {
|
|
|
|
return syscall("datastore.listFunctions");
|
2023-10-03 20:16:33 +08:00
|
|
|
}
|