import type { UpEntry } from "upend"; import type { PutResult } from "upend/types"; import { query as queryFn, queryOnce } 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 getObjects( entries: UpEntry[], limit = 50 ): Promise<[string, string][]> { return entries .slice(0, limit) .filter((e) => e.attribute == "LBL") .map((e) => [e.entity, String(e.value.c)] as [string, string]); } export async function createLabelled(label: string) { let body: unknown; if (label.match("^[\\w]+://[\\w]")) { body = { entity: { t: "Url", c: label, }, }; } else { body = { attribute: "LBL", value: { t: "String", c: label, }, }; } const response = await fetch(`api/obj`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); if (!response.ok) { throw new Error(`Failed to create object: ${await response.text()}`); } const [_, entry] = (await response.json()) as PutResult; return entry; }