Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/sync/table/util.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { copy, keys, is_array, deep_copy } from "@cocalc/util/misc";6import { SCHEMA } from "@cocalc/util/schema";78// Parse query description to allow for some convenient shortcuts9// TODO: document them here!10export function parse_query(query) {11query = deep_copy(query);12if (typeof query === "string") {13// name of a table -- get all fields14const s = SCHEMA[query];15if (s == null) throw Error(`no schema for query "${query}"`);16if (s.user_query == null)17throw Error(`user_query not defined for query "${query}"`);18if (s.user_query.get == null)19throw Error(`user_query.get not defined for query "${query}"`);20const v = copy(s.user_query.get.fields);21for (const k in v) {22v[k] = null;23}24return { [query]: [v] };25} else {26const k = keys(query);27if (k.length !== 1) {28throw Error("must specify exactly one table");29}30const table = k[0];31if (!is_array(query[table])) {32return { [table]: [query[table]] };33} else {34return { [table]: query[table] };35}36}37}3839const json_stable_stringify = require("json-stable-stringify");40export function to_key(x): string | undefined {41if (x === undefined) {42return undefined;43} else if (typeof x === "object") {44return json_stable_stringify(x);45} else {46return `${x}`;47}48}495051