upend/webui/src/util/search.ts

44 lines
1.1 KiB
TypeScript

import type { PutInput } from "upend/types";
import { query as queryFn } from "../lib/entity";
import api from "../lib/api";
import { ATTR_LABEL } from "upend/constants";
export function baseSearch(query: string) {
return queryFn(
`(or (matches (contains "${query}") ? ?) (matches ? (contains "${query}") ?) (matches ? ? (contains "${query}")))`
);
}
export function baseSearchOnce(query: string) {
return api.query(
`(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 {
// TODO - don't create invariants, create UUIDs instead, maybe with keys?
body = {
attribute: ATTR_LABEL,
value: {
t: "String",
c: label,
},
};
}
try {
const [_, entry] = await api.putEntry(body);
return entry;
} catch (error) {
throw new Error(`Failed to create object: ${error}`);
}
}