2023-10-03 20:16:33 +08:00
|
|
|
import { KV, KvKey, KvQuery } from "$sb/types.ts";
|
|
|
|
import type { DataStore } from "../lib/datastore.ts";
|
2024-01-15 23:43:12 +08:00
|
|
|
import type { SysCallMapping } from "../system.ts";
|
2023-10-03 20:16:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2024-01-27 00:05:10 +08:00
|
|
|
export function dataStoreReadSyscalls(ds: DataStore): SysCallMapping {
|
|
|
|
return {
|
|
|
|
"datastore.batchGet": (
|
|
|
|
_ctx,
|
|
|
|
keys: KvKey[],
|
|
|
|
): Promise<(any | undefined)[]> => {
|
|
|
|
return ds.batchGet(keys);
|
|
|
|
},
|
|
|
|
|
|
|
|
"datastore.get": (_ctx, key: KvKey): Promise<any | null> => {
|
|
|
|
return ds.get(key);
|
|
|
|
},
|
|
|
|
|
|
|
|
"datastore.query": async (_ctx, query: KvQuery): Promise<KV[]> => {
|
|
|
|
return (await ds.query(query));
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function dataStoreWriteSyscalls(ds: DataStore): SysCallMapping {
|
2023-10-03 20:16:33 +08:00
|
|
|
return {
|
2024-01-15 23:43:12 +08:00
|
|
|
"datastore.delete": (_ctx, key: KvKey) => {
|
|
|
|
return ds.delete(key);
|
2023-10-03 20:16:33 +08:00
|
|
|
},
|
|
|
|
|
2024-01-15 23:43:12 +08:00
|
|
|
"datastore.set": (_ctx, key: KvKey, value: any) => {
|
|
|
|
return ds.set(key, value);
|
2023-10-03 20:16:33 +08:00
|
|
|
},
|
|
|
|
|
2024-01-15 23:43:12 +08:00
|
|
|
"datastore.batchSet": (_ctx, kvs: KV[]) => {
|
|
|
|
return ds.batchSet(kvs);
|
2023-10-03 20:16:33 +08:00
|
|
|
},
|
|
|
|
|
2024-01-15 23:43:12 +08:00
|
|
|
"datastore.batchDelete": (_ctx, keys: KvKey[]) => {
|
|
|
|
return ds.batchDelete(keys);
|
2023-10-03 20:16:33 +08:00
|
|
|
},
|
|
|
|
|
2024-01-15 23:43:12 +08:00
|
|
|
"datastore.queryDelete": (_ctx, query: KvQuery): Promise<void> => {
|
|
|
|
return ds.queryDelete(query);
|
2023-10-03 20:16:33 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|