2024-07-30 23:24:17 +08:00
|
|
|
import type {
|
|
|
|
KV,
|
|
|
|
KvQuery,
|
|
|
|
ObjectQuery,
|
|
|
|
ObjectValue,
|
|
|
|
} from "../../plug-api/types.ts";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { ttlCache } from "$lib/memory_cache.ts";
|
2024-08-07 02:11:38 +08:00
|
|
|
import { system } from "@silverbulletmd/silverbullet/syscalls";
|
2023-10-03 20:16:33 +08:00
|
|
|
|
|
|
|
export function indexObjects<T>(
|
|
|
|
page: string,
|
|
|
|
objects: ObjectValue<T>[],
|
|
|
|
): Promise<void> {
|
2024-02-09 04:00:45 +08:00
|
|
|
return system.invokeFunction("index.indexObjects", page, objects);
|
2023-10-03 20:16:33 +08:00
|
|
|
}
|
|
|
|
|
2023-10-03 21:24:07 +08:00
|
|
|
export function batchSet(page: string, kvs: KV[]): Promise<void> {
|
2024-02-09 04:00:45 +08:00
|
|
|
return system.invokeFunction("index.batchSet", page, kvs);
|
2023-10-03 21:24:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function query(
|
|
|
|
query: KvQuery,
|
|
|
|
): Promise<KV[]> {
|
2024-02-09 04:00:45 +08:00
|
|
|
return system.invokeFunction("index.query", query);
|
2023-10-03 21:24:07 +08:00
|
|
|
}
|
|
|
|
|
2023-10-03 20:16:33 +08:00
|
|
|
export function queryObjects<T>(
|
|
|
|
tag: string,
|
|
|
|
query: ObjectQuery,
|
2024-01-15 23:43:12 +08:00
|
|
|
ttlSecs?: number,
|
2023-10-03 20:16:33 +08:00
|
|
|
): Promise<ObjectValue<T>[]> {
|
2024-01-15 23:43:12 +08:00
|
|
|
return ttlCache(
|
|
|
|
query,
|
2024-02-09 04:00:45 +08:00
|
|
|
() => system.invokeFunction("index.queryObjects", tag, query),
|
2024-01-15 23:43:12 +08:00
|
|
|
ttlSecs, // no-op when undefined
|
|
|
|
);
|
2023-10-03 20:16:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getObjectByRef<T>(
|
|
|
|
page: string,
|
|
|
|
tag: string,
|
|
|
|
ref: string,
|
2023-11-06 16:14:16 +08:00
|
|
|
): Promise<T | undefined> {
|
2024-02-09 04:00:45 +08:00
|
|
|
return system.invokeFunction("index.getObjectByRef", page, tag, ref);
|
2023-10-03 20:16:33 +08:00
|
|
|
}
|