2023-10-03 12:16:33 +00:00
|
|
|
import { applyQueryNoFilterKV, evalQueryExpression } from "$sb/lib/query.ts";
|
|
|
|
import { FunctionMap, KV, KvKey, KvQuery } from "$sb/types.ts";
|
|
|
|
import { builtinFunctions } from "$sb/lib/builtin_query_functions.ts";
|
|
|
|
import { KvPrimitives } from "./kv_primitives.ts";
|
2023-09-03 19:15:17 +00:00
|
|
|
/**
|
|
|
|
* This is the data store class you'll actually want to use, wrapping the primitives
|
|
|
|
* in a more user-friendly way
|
|
|
|
*/
|
|
|
|
export class DataStore {
|
2023-10-03 12:16:33 +00:00
|
|
|
constructor(
|
2023-12-06 17:44:48 +00:00
|
|
|
readonly kv: KvPrimitives,
|
2023-10-03 12:16:33 +00:00
|
|
|
private functionMap: FunctionMap = builtinFunctions,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
async get<T = any>(key: KvKey): Promise<T | null> {
|
|
|
|
return (await this.batchGet([key]))[0];
|
2023-09-03 19:15:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
batchGet<T = any>(keys: KvKey[]): Promise<(T | null)[]> {
|
2024-01-13 17:12:48 +00:00
|
|
|
if (keys.length === 0) {
|
|
|
|
return Promise.resolve([]);
|
|
|
|
}
|
2023-12-10 12:23:42 +00:00
|
|
|
return this.kv.batchGet(keys);
|
2023-09-03 19:15:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
set(key: KvKey, value: any): Promise<void> {
|
|
|
|
return this.batchSet([{ key, value }]);
|
2023-09-03 19:15:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
batchSet<T = any>(entries: KV<T>[]): Promise<void> {
|
2024-01-13 17:12:48 +00:00
|
|
|
if (entries.length === 0) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
const allKeyStrings = new Set<string>();
|
|
|
|
const uniqueEntries: KV[] = [];
|
|
|
|
for (const { key, value } of entries) {
|
|
|
|
const keyString = JSON.stringify(key);
|
|
|
|
if (allKeyStrings.has(keyString)) {
|
|
|
|
console.warn(`Duplicate key ${keyString} in batchSet, skipping`);
|
|
|
|
} else {
|
|
|
|
allKeyStrings.add(keyString);
|
2023-12-10 12:23:42 +00:00
|
|
|
uniqueEntries.push({ key, value });
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.kv.batchSet(uniqueEntries);
|
2023-09-03 19:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(key: KvKey): Promise<void> {
|
2023-10-03 12:16:33 +00:00
|
|
|
return this.batchDelete([key]);
|
2023-09-03 19:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
batchDelete(keys: KvKey[]): Promise<void> {
|
2024-01-13 17:12:48 +00:00
|
|
|
if (keys.length === 0) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2023-12-10 12:23:42 +00:00
|
|
|
return this.kv.batchDelete(keys);
|
2023-09-03 19:15:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
async query<T = any>(query: KvQuery): Promise<KV<T>[]> {
|
|
|
|
const results: KV<T>[] = [];
|
2023-09-03 19:15:17 +00:00
|
|
|
let itemCount = 0;
|
2023-10-03 12:16:33 +00:00
|
|
|
// Accumulate results
|
|
|
|
let limit = Infinity;
|
|
|
|
if (query.limit) {
|
|
|
|
limit = evalQueryExpression(query.limit, {}, this.functionMap);
|
|
|
|
}
|
|
|
|
for await (
|
2023-12-10 12:23:42 +00:00
|
|
|
const entry of this.kv.query(query)
|
2023-10-03 12:16:33 +00:00
|
|
|
) {
|
2023-09-03 19:15:17 +00:00
|
|
|
// Filter
|
2023-10-03 12:16:33 +00:00
|
|
|
if (
|
|
|
|
query.filter &&
|
|
|
|
!evalQueryExpression(query.filter, entry.value, this.functionMap)
|
|
|
|
) {
|
2023-09-03 19:15:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
results.push(entry);
|
|
|
|
itemCount++;
|
|
|
|
// Stop when the limit has been reached
|
2023-10-03 14:54:03 +00:00
|
|
|
if (itemCount === limit && !query.orderBy) {
|
|
|
|
// Only break when not also ordering in which case we need all results
|
2023-09-03 19:15:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
// Apply order by, limit, and select
|
2024-01-15 15:43:12 +00:00
|
|
|
return applyQueryNoFilterKV(query, results, this.functionMap);
|
2023-10-03 12:16:33 +00:00
|
|
|
}
|
2023-09-03 19:15:17 +00:00
|
|
|
|
2023-10-03 12:16:33 +00:00
|
|
|
async queryDelete(query: KvQuery): Promise<void> {
|
|
|
|
const keys: KvKey[] = [];
|
|
|
|
for (
|
2023-12-10 12:23:42 +00:00
|
|
|
const { key } of await this.query(query)
|
2023-10-03 12:16:33 +00:00
|
|
|
) {
|
|
|
|
keys.push(key);
|
2023-09-03 19:15:17 +00:00
|
|
|
}
|
2023-10-03 12:16:33 +00:00
|
|
|
return this.batchDelete(keys);
|
|
|
|
}
|
2023-09-03 19:15:17 +00:00
|
|
|
}
|