2023-04-25 19:57:15 +02:00
|
|
|
import type { PutInput } from "upend/types";
|
2022-02-20 13:06:01 +01:00
|
|
|
import { query as queryFn } from "../lib/entity";
|
2023-05-22 20:57:06 +02:00
|
|
|
import api from "../lib/api";
|
2023-06-24 16:26:14 +02:00
|
|
|
import { ATTR_LABEL } from "upend/constants";
|
2022-01-27 20:56:00 +01:00
|
|
|
|
|
|
|
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
|
|
|
);
|
2022-01-27 20:56:00 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 21:28:42 +01:00
|
|
|
export function baseSearchOnce(query: string) {
|
2023-05-22 20:57:06 +02:00
|
|
|
return api.query(
|
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
|
|
|
);
|
2022-01-27 21:28:42 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 20:56:00 +01:00
|
|
|
export async function createLabelled(label: string) {
|
2023-04-25 19:57:15 +02:00
|
|
|
let body: PutInput;
|
2022-02-09 20:34:52 +01:00
|
|
|
if (label.match("^[\\w]+://[\\w]")) {
|
|
|
|
body = {
|
|
|
|
entity: {
|
|
|
|
t: "Url",
|
|
|
|
c: label,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} else {
|
2023-06-19 13:04:15 +02:00
|
|
|
// TODO - don't create invariants, create UUIDs instead, maybe with keys?
|
2022-02-09 20:34:52 +01:00
|
|
|
body = {
|
2023-06-24 16:26:14 +02:00
|
|
|
attribute: ATTR_LABEL,
|
2022-01-27 20:56:00 +01:00
|
|
|
value: {
|
2022-01-28 20:51:34 +01:00
|
|
|
t: "String",
|
2022-01-27 20:56:00 +01:00
|
|
|
c: label,
|
|
|
|
},
|
2022-02-09 20:34:52 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-20 13:06:01 +01:00
|
|
|
try {
|
2023-05-22 20:57:06 +02:00
|
|
|
const [_, entry] = await api.putEntry(body);
|
2022-02-20 13:06:01 +01:00
|
|
|
return entry;
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`Failed to create object: ${error}`);
|
2022-01-27 20:56:00 +01:00
|
|
|
}
|
|
|
|
}
|