Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/util/db-schema/client-db.ts
5787 views
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// Client side versions of some db functions, which are used, e.g., when setting fields.
7
8
import { is_array } from "../misc";
9
import { SCHEMA } from "./index";
10
import { sha1 } from "@cocalc/util/misc";
11
12
class ClientDB {
13
private _primary_keys_cache;
14
public r;
15
16
constructor() {
17
this.sha1 = this.sha1.bind(this);
18
this._user_set_query_project_users =
19
this._user_set_query_project_users.bind(this);
20
this._user_set_query_project_manage_users_owner_only =
21
this._user_set_query_project_manage_users_owner_only.bind(this);
22
this._user_set_query_project_change_after =
23
this._user_set_query_project_change_after.bind(this);
24
this._user_set_query_project_change_before =
25
this._user_set_query_project_change_before.bind(this);
26
this.primary_keys = this.primary_keys.bind(this);
27
this.r = {};
28
}
29
30
sha1(...args) {
31
let v;
32
try {
33
v = args
34
.map((x) => (typeof x === "string" ? x : JSON.stringify(x)))
35
.join("");
36
} catch (err) {
37
if (console != null && console.warn != null) {
38
console.warn("args=", args);
39
}
40
throw err;
41
}
42
return sha1(v);
43
}
44
45
_user_set_query_project_users(obj) {
46
// client allows anything; server may be more stringent
47
return obj.users;
48
}
49
50
_user_set_query_project_manage_users_owner_only(obj) {
51
return obj.manage_users_owner_only;
52
}
53
54
_user_set_query_project_change_after(_obj, _old_val, _new_val, cb) {
55
cb();
56
}
57
_user_set_query_project_change_before(_obj, _old_val, _new_val, cb) {
58
cb();
59
}
60
61
// table is either name of a table in the default SCHEMA, or
62
// it can be a TableSchema<any> object (for a non-virtual table).
63
primary_keys(table) {
64
if (this._primary_keys_cache == null) {
65
this._primary_keys_cache = {};
66
}
67
const key = typeof table == "string" ? table : table.name;
68
if (this._primary_keys_cache[key] != null) {
69
return this._primary_keys_cache[key];
70
}
71
let t = typeof table == "string" ? SCHEMA[table] : table;
72
if (typeof t.virtual == "string") {
73
t = SCHEMA[t.virtual];
74
}
75
const v = t != null ? t.primary_key : undefined;
76
if (v == null) {
77
throw Error(
78
`primary key for table '${table}' must be explicitly specified in schema`,
79
);
80
}
81
if (typeof v === "string") {
82
return (this._primary_keys_cache[key] = [v]);
83
} else if (is_array(v) && typeof v == "object") {
84
// the typeof is just to make typescript happy
85
if (v.length === 0) {
86
throw Error("at least one primary key must specified");
87
}
88
return (this._primary_keys_cache[key] = v);
89
} else {
90
throw Error("primary key must be a string or array of strings");
91
}
92
}
93
}
94
95
export const client_db = new ClientDB();
96
97