import { copy, keys, is_array, deep_copy } from "@cocalc/util/misc";
import { SCHEMA } from "@cocalc/util/schema";
export function parse_query(query) {
query = deep_copy(query);
if (typeof query === "string") {
const s = SCHEMA[query];
if (s == null) throw Error(`no schema for query "${query}"`);
if (s.user_query == null)
throw Error(`user_query not defined for query "${query}"`);
if (s.user_query.get == null)
throw Error(`user_query.get not defined for query "${query}"`);
const v = copy(s.user_query.get.fields);
for (const k in v) {
v[k] = null;
}
return { [query]: [v] };
} else {
const k = keys(query);
if (k.length !== 1) {
throw Error("must specify exactly one table");
}
const table = k[0];
if (!is_array(query[table])) {
return { [table]: [query[table]] };
} else {
return { [table]: query[table] };
}
}
}
const json_stable_stringify = require("json-stable-stringify");
export function to_key(x): string | undefined {
if (x === undefined) {
return undefined;
} else if (typeof x === "object") {
return json_stable_stringify(x);
} else {
return `${x}`;
}
}