2022-03-20 08:56:28 +00:00
|
|
|
import { ApiProvider, ClientConnection } from "./api_server";
|
|
|
|
import knex, { Knex } from "knex";
|
|
|
|
import path from "path";
|
|
|
|
import pageIndexSyscalls from "./syscalls/page_index";
|
|
|
|
|
|
|
|
type IndexItem = {
|
|
|
|
page: string;
|
|
|
|
key: string;
|
|
|
|
value: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class IndexApi implements ApiProvider {
|
|
|
|
db: Knex;
|
|
|
|
|
|
|
|
constructor(rootPath: string) {
|
|
|
|
this.db = knex({
|
|
|
|
client: "better-sqlite3",
|
|
|
|
connection: {
|
|
|
|
filename: path.join(rootPath, "data.db"),
|
|
|
|
},
|
|
|
|
useNullAsDefault: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
if (!(await this.db.schema.hasTable("page_index"))) {
|
|
|
|
await this.db.schema.createTable("page_index", (table) => {
|
|
|
|
table.string("page");
|
|
|
|
table.string("key");
|
|
|
|
table.text("value");
|
|
|
|
table.primary(["page", "key"]);
|
|
|
|
});
|
|
|
|
console.log("Created table page_index");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
api() {
|
|
|
|
const syscalls = pageIndexSyscalls(this.db);
|
2022-03-25 11:03:06 +00:00
|
|
|
const nullContext = { plug: null };
|
2022-03-20 08:56:28 +00:00
|
|
|
return {
|
|
|
|
clearPageIndexForPage: async (
|
|
|
|
clientConn: ClientConnection,
|
|
|
|
page: string
|
|
|
|
) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
return syscalls.clearPageIndexForPage(nullContext, page);
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
|
|
|
set: async (
|
|
|
|
clientConn: ClientConnection,
|
|
|
|
page: string,
|
|
|
|
key: string,
|
|
|
|
value: any
|
|
|
|
) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
return syscalls.set(nullContext, page, key, value);
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
|
|
|
get: async (clientConn: ClientConnection, page: string, key: string) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
return syscalls.get(nullContext, page, key);
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
|
|
|
delete: async (
|
|
|
|
clientConn: ClientConnection,
|
|
|
|
page: string,
|
|
|
|
key: string
|
|
|
|
) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
return syscalls.delete(nullContext, page, key);
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
|
|
|
scanPrefixForPage: async (
|
|
|
|
clientConn: ClientConnection,
|
|
|
|
page: string,
|
|
|
|
prefix: string
|
|
|
|
) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
return syscalls.scanPrefixForPage(nullContext, page, prefix);
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
|
|
|
scanPrefixGlobal: async (
|
|
|
|
clientConn: ClientConnection,
|
|
|
|
prefix: string
|
|
|
|
) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
return syscalls.scanPrefixGlobal(nullContext, prefix);
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
|
|
|
deletePrefixForPage: async (
|
|
|
|
clientConn: ClientConnection,
|
|
|
|
page: string,
|
|
|
|
prefix: string
|
|
|
|
) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
return syscalls.deletePrefixForPage(nullContext, page, prefix);
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
clearPageIndex: async (clientConn: ClientConnection) => {
|
2022-03-25 11:03:06 +00:00
|
|
|
return syscalls.clearPageIndex(nullContext);
|
2022-03-20 08:56:28 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|