upend/webui/src/util/search.ts

41 lines
1 KiB
TypeScript
Raw Normal View History

2023-04-25 19:57:15 +02:00
import type { PutInput } from "upend/types";
import { query as queryFn } from "../lib/entity";
import api from "../lib/api";
2023-06-24 16:26:14 +02:00
import { ATTR_LABEL } from "upend/constants";
export function baseSearch(query: string) {
2022-02-04 23:04:38 +01:00
return queryFn(
2023-08-01 22:02:52 +02:00
`(or (matches (contains "${query}") ? ?) (matches ? (contains "${query}") ?) (matches ? ? (contains "${query}")))`,
2022-02-04 23:04:38 +01:00
);
}
export function baseSearchOnce(query: string) {
return api.query(
2023-08-01 22:02:52 +02: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) {
2023-04-25 19:57:15 +02:00
let body: PutInput;
if (label.match("^[\\w]+://[\\w]")) {
body = {
entity: {
t: "Url",
c: label,
},
};
const [_, entity] = await api.putEntry(body);
return entity;
} else {
const [_, entity] = await api.putEntry({
entity: {
t: "Uuid",
},
});
await api.putEntityAttribute(entity, ATTR_LABEL, { t: "String", c: label });
return entity;
}
}