Complete redo of content indexing and querying (#517)

Complete redo of data store
Introduces live queries and live templates
This commit is contained in:
Zef Hemel
2023-10-03 14:16:33 +02:00
committed by GitHub
parent 7af98e7c7b
commit 0313565610
200 changed files with 4675 additions and 4363 deletions
+75
View File
@@ -0,0 +1,75 @@
import { KV, KvKey, KvQuery } from "$sb/types.ts";
import type { DataStore } from "../lib/datastore.ts";
import type { SyscallContext, SysCallMapping } from "../system.ts";
/**
* Exposes the datastore API to plugs, but scoping everything to a prefix based on the plug's name
* @param ds the datastore to wrap
* @param prefix prefix to scope all keys to to which the plug name will be appended
*/
export function dataStoreSyscalls(
ds: DataStore,
prefix: KvKey = ["ds"],
): SysCallMapping {
return {
"datastore.delete": (ctx, key: KvKey) => {
return ds.delete(applyPrefix(ctx, key));
},
"datastore.set": (ctx, key: KvKey, value: any) => {
return ds.set(applyPrefix(ctx, key), value);
},
"datastore.batchSet": (ctx, kvs: KV[]) => {
return ds.batchSet(
kvs.map((kv) => ({ key: applyPrefix(ctx, kv.key), value: kv.value })),
);
},
"datastore.batchDelete": (ctx, keys: KvKey[]) => {
return ds.batchDelete(keys.map((k) => applyPrefix(ctx, k)));
},
"datastore.batchGet": (
ctx,
keys: KvKey[],
): Promise<(any | undefined)[]> => {
return ds.batchGet(keys.map((k) => applyPrefix(ctx, k)));
},
"datastore.get": (ctx, key: KvKey): Promise<any | null> => {
return ds.get(applyPrefix(ctx, key));
},
"datastore.query": async (
ctx,
query: KvQuery,
): Promise<KV[]> => {
return (await ds.query({
...query,
prefix: applyPrefix(ctx, query.prefix),
})).map((kv) => ({
key: stripPrefix(kv.key),
value: kv.value,
}));
},
"datastore.queryDelete": (
ctx,
query: KvQuery,
): Promise<void> => {
return ds.queryDelete({
...query,
prefix: applyPrefix(ctx, query.prefix),
});
},
};
function applyPrefix(ctx: SyscallContext, key?: KvKey): KvKey {
return [...prefix, ctx.plug.name!, ...(key ? key : [])];
}
function stripPrefix(key: KvKey): KvKey {
return key.slice(prefix.length + 1);
}
}
-54
View File
@@ -1,54 +0,0 @@
import { SysCallMapping } from "../system.ts";
import { KV, KVStore } from "../lib/kv_store.ts";
export function storeSyscalls(
db: KVStore,
): SysCallMapping {
return {
"store.delete": (_ctx, key: string) => {
return db.del(key);
},
"store.deletePrefix": (_ctx, prefix: string) => {
return db.deletePrefix(prefix);
},
"store.deleteAll": () => {
return db.deleteAll();
},
"store.set": (_ctx, key: string, value: any) => {
return db.set(key, value);
},
"store.batchSet": (_ctx, kvs: KV[]) => {
return db.batchSet(kvs);
},
"store.batchDelete": (_ctx, keys: string[]) => {
return db.batchDelete(keys);
},
"store.batchGet": (
_ctx,
keys: string[],
): Promise<(any | undefined)[]> => {
return db.batchGet(keys);
},
"store.get": (_ctx, key: string): Promise<any | null> => {
return db.get(key);
},
"store.has": (_ctx, key: string): Promise<boolean> => {
return db.has(key);
},
"store.queryPrefix": (
_ctx,
keyPrefix: string,
): Promise<{ key: string; value: any }[]> => {
return db.queryPrefix(keyPrefix);
},
};
}