upend/webui/src/util/search.ts

43 lines
1,005 B
TypeScript
Raw Normal View History

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