2022-04-05 15:02:17 +00:00
|
|
|
import { Knex } from "knex";
|
|
|
|
import { SysCallMapping } from "../system";
|
2022-03-25 11:03:06 +00:00
|
|
|
|
|
|
|
type Item = {
|
|
|
|
page: string;
|
|
|
|
key: string;
|
|
|
|
value: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type KV = {
|
|
|
|
key: string;
|
|
|
|
value: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function ensureTable(db: Knex<any, unknown>, tableName: string) {
|
|
|
|
if (!(await db.schema.hasTable(tableName))) {
|
|
|
|
await db.schema.createTable(tableName, (table) => {
|
|
|
|
table.string("key");
|
|
|
|
table.text("value");
|
|
|
|
table.primary(["key"]);
|
|
|
|
});
|
|
|
|
console.log(`Created table ${tableName}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 16:42:12 +00:00
|
|
|
export function storeSyscalls(
|
2022-03-25 11:03:06 +00:00
|
|
|
db: Knex<any, unknown>,
|
|
|
|
tableName: string
|
|
|
|
): SysCallMapping {
|
|
|
|
const apiObj: SysCallMapping = {
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.delete": async (ctx, key: string) => {
|
2022-03-28 13:25:05 +00:00
|
|
|
await db<Item>(tableName).where({ key }).del();
|
2022-03-25 11:03:06 +00:00
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.deletePrefix": async (ctx, prefix: string) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
return db<Item>(tableName).andWhereLike("key", `${prefix}%`).del();
|
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.deleteAll": async (ctx) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
await db<Item>(tableName).del();
|
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.set": async (ctx, key: string, value: any) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
let changed = await db<Item>(tableName)
|
|
|
|
.where({ key })
|
|
|
|
.update("value", JSON.stringify(value));
|
|
|
|
if (changed === 0) {
|
|
|
|
await db<Item>(tableName).insert({
|
|
|
|
key,
|
|
|
|
value: JSON.stringify(value),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2022-03-28 13:25:05 +00:00
|
|
|
// TODO: Optimize
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.batchSet": async (ctx, kvs: KV[]) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
for (let { key, value } of kvs) {
|
2022-04-03 16:42:12 +00:00
|
|
|
await apiObj["store.set"](ctx, key, value);
|
2022-03-28 13:25:05 +00:00
|
|
|
}
|
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.batchDelete": async (ctx, keys: string[]) => {
|
2022-03-28 13:25:05 +00:00
|
|
|
for (let key of keys) {
|
2022-04-03 16:42:12 +00:00
|
|
|
await apiObj["store.delete"](ctx, key);
|
2022-03-25 11:03:06 +00:00
|
|
|
}
|
|
|
|
},
|
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 db<Item>(tableName).where({ key }).select("value");
|
|
|
|
if (result.length) {
|
|
|
|
return JSON.parse(result[0].value);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
2022-04-03 16:42:12 +00:00
|
|
|
"store.queryPrefix": async (ctx, prefix: string) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
return (
|
|
|
|
await db<Item>(tableName)
|
|
|
|
.andWhereLike("key", `${prefix}%`)
|
|
|
|
.select("key", "value")
|
|
|
|
).map(({ key, value }) => ({
|
|
|
|
key,
|
|
|
|
value: JSON.parse(value),
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
};
|
2022-04-03 16:42:12 +00:00
|
|
|
return apiObj;
|
2022-03-25 11:03:06 +00:00
|
|
|
}
|