2022-03-25 11:03:06 +00:00
|
|
|
import Dexie from "dexie";
|
2022-04-03 16:42:12 +00:00
|
|
|
import {SysCallMapping} from "../system";
|
2022-03-25 11:03:06 +00:00
|
|
|
|
|
|
|
export type KV = {
|
|
|
|
key: string;
|
|
|
|
value: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function storeSyscalls(
|
|
|
|
dbName: string,
|
|
|
|
tableName: string
|
|
|
|
): SysCallMapping {
|
|
|
|
const db = new Dexie(dbName);
|
|
|
|
db.version(1).stores({
|
|
|
|
test: "key",
|
|
|
|
});
|
|
|
|
const items = db.table(tableName);
|
|
|
|
|
|
|
|
return {
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.delete": async (ctx, key: string) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
await items.delete(key);
|
|
|
|
},
|
|
|
|
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.deletePrefix": async (ctx, prefix: string) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
await items.where("key").startsWith(prefix).delete();
|
|
|
|
},
|
|
|
|
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.deleteAll": async () => {
|
2022-03-25 11:03:06 +00:00
|
|
|
await items.clear();
|
|
|
|
},
|
|
|
|
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.set": async (ctx, key: string, value: any) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
await items.put({
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.batchSet": async (ctx, kvs: KV[]) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
await items.bulkPut(
|
|
|
|
kvs.map(({ key, value }) => ({
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.get": async (ctx, key: string): Promise<any | null> => {
|
2022-03-25 11:03:06 +00:00
|
|
|
let result = await items.get({
|
|
|
|
key,
|
|
|
|
});
|
|
|
|
return result ? result.value : null;
|
|
|
|
},
|
|
|
|
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.queryPrefix": async (
|
2022-03-25 11:03:06 +00:00
|
|
|
ctx,
|
|
|
|
keyPrefix: string
|
2022-04-03 16:42:12 +00:00
|
|
|
): Promise<{ key: string; value: any }[]> => {
|
2022-03-25 11:03:06 +00:00
|
|
|
let results = await items.where("key").startsWith(keyPrefix).toArray();
|
|
|
|
return results.map((result) => ({
|
|
|
|
key: result.key,
|
|
|
|
value: result.value,
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|