1
0
silverbullet/plugos/syscalls/fulltext.sqlite.ts
Zef Hemel 0365673c41
Mobile app PoC (#281)
Initial checkin of mobile "native" app
2023-01-08 12:24:12 +01:00

65 lines
1.7 KiB
TypeScript

import { FullTextSearchOptions } from "../../plug-api/plugos-syscall/fulltext.ts";
import { ISQLite } from "../sqlite/sqlite_interface.ts";
import { SysCallMapping } from "../system.ts";
export async function ensureFTSTable(
db: ISQLite,
tableName: string,
) {
const result = await db.query(
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`,
tableName,
);
if (result.length === 0) {
await db.execute(
`CREATE VIRTUAL TABLE ${tableName} USING fts5(key, value);`,
);
// console.log(`Created fts5 table ${tableName}`);
}
}
export function fullTextSearchSyscalls(
db: ISQLite,
tableName: string,
): SysCallMapping {
return {
"fulltext.index": async (_ctx, key: string, value: string) => {
await db.execute(`DELETE FROM ${tableName} WHERE key = ?`, key);
await db.execute(
`INSERT INTO ${tableName} (key, value) VALUES (?, ?)`,
key,
value,
);
},
"fulltext.delete": async (_ctx, key: string) => {
await db.execute(`DELETE FROM ${tableName} WHERE key = ?`, key);
},
"fulltext.search": async (
_ctx,
phrase: string,
options: FullTextSearchOptions,
) => {
return (
await db.query(
`SELECT key, bm25(fts) AS score, snippet(fts, 1, ?, ?, ?, ?) as snippet
FROM ${tableName}
WHERE value
MATCH ?
ORDER BY score LIMIT ?`,
options.highlightPrefix || "",
options.highlightPostfix || "",
options.highlightEllipsis || "...",
options.summaryMaxLength || 50,
phrase,
options.limit || 20,
)
).map((item) => ({
name: item.key,
score: item.score,
snippet: item.snippet,
}));
},
};
}