import { Knex } from "knex"; import { SysCallMapping } from "../system"; type Item = { key: string; value: string; }; export async function ensureFTSTable( db: Knex, tableName: string ) { if (!(await db.schema.hasTable(tableName))) { await db.raw(`CREATE VIRTUAL TABLE ${tableName} USING fts5(key, value);`); console.log(`Created fts5 table ${tableName}`); } } export function fullTextSearchSyscalls( db: Knex, tableName: string ): SysCallMapping { return { "fulltext.index": async (ctx, key: string, value: string) => { await db(tableName).where({ key }).del(); await db(tableName).insert({ key, value }); }, "fulltext.delete": async (ctx, key: string) => { await db(tableName).where({ key }).del(); }, "fulltext.search": async (ctx, phrase: string, limit: number) => { return ( await db(tableName) .whereRaw(`value MATCH ?`, [phrase]) .select(["key", "rank"]) .orderBy("rank") .limit(limit) ).map((item) => ({ name: item.key, rank: item.rank })); }, }; }