upend/webui/src/util/search.ts
2023-04-25 19:57:23 +02:00

42 lines
1,007 B
TypeScript

import type { PutInput } from "upend/types";
import { putEntry, queryOnce } from "../lib/api";
import { query as queryFn } from "../lib/entity";
export function baseSearch(query: string) {
return queryFn(
`(or (matches (contains "${query}") ? ?) (matches ? (contains "${query}") ?) (matches ? ? (contains "${query}")))`
);
}
export function baseSearchOnce(query: string) {
return queryOnce(
`(or (matches (contains "${query}") ? ?) (matches ? (contains "${query}") ?) (matches ? ? (contains "${query}")))`
);
}
export async function createLabelled(label: string) {
let body: PutInput;
if (label.match("^[\\w]+://[\\w]")) {
body = {
entity: {
t: "Url",
c: label,
},
};
} else {
body = {
attribute: "LBL",
value: {
t: "String",
c: label,
},
};
}
try {
const [_, entry] = await putEntry(body);
return entry;
} catch (error) {
throw new Error(`Failed to create object: ${error}`);
}
}