CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync/table/util.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { copy, keys, is_array, deep_copy } from "@cocalc/util/misc";
7
import { SCHEMA } from "@cocalc/util/schema";
8
9
// Parse query description to allow for some convenient shortcuts
10
// TODO: document them here!
11
export function parse_query(query) {
12
query = deep_copy(query);
13
if (typeof query === "string") {
14
// name of a table -- get all fields
15
const s = SCHEMA[query];
16
if (s == null) throw Error(`no schema for query "${query}"`);
17
if (s.user_query == null)
18
throw Error(`user_query not defined for query "${query}"`);
19
if (s.user_query.get == null)
20
throw Error(`user_query.get not defined for query "${query}"`);
21
const v = copy(s.user_query.get.fields);
22
for (const k in v) {
23
v[k] = null;
24
}
25
return { [query]: [v] };
26
} else {
27
const k = keys(query);
28
if (k.length !== 1) {
29
throw Error("must specify exactly one table");
30
}
31
const table = k[0];
32
if (!is_array(query[table])) {
33
return { [table]: [query[table]] };
34
} else {
35
return { [table]: query[table] };
36
}
37
}
38
}
39
40
const json_stable_stringify = require("json-stable-stringify");
41
export function to_key(x): string | undefined {
42
if (x === undefined) {
43
return undefined;
44
} else if (typeof x === "object") {
45
return json_stable_stringify(x);
46
} else {
47
return `${x}`;
48
}
49
}
50
51