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/database/index.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
/*
7
PostgreSQL database entry point.
8
Do not import any of the submodules directly unless you
9
know exactly what you're doing.
10
11
COPYRIGHT : (c) 2021 SageMath, Inc.
12
*/
13
14
import { setupRecordConnectErrors } from "./postgres/record-connect-error";
15
16
import { PostgreSQL } from "./postgres/types";
17
18
const base = require("./postgres-base");
19
20
export const {
21
pg_type,
22
expire_time,
23
one_result,
24
all_results,
25
count_result,
26
PROJECT_COLUMNS,
27
PUBLIC_PROJECT_COLUMNS,
28
} = base;
29
30
// Add further functionality to PostgreSQL class -- must be at the bottom of this file.
31
// Each of the following calls composes the PostgreSQL class with further important functionality.
32
// Order matters.
33
34
let theDB: PostgreSQL | undefined = undefined;
35
36
export function db(opts = {}): PostgreSQL {
37
if (theDB === undefined) {
38
let PostgreSQL = base.PostgreSQL;
39
40
for (const module of [
41
"server-queries",
42
"blobs",
43
"synctable",
44
"user-queries",
45
"ops",
46
]) {
47
PostgreSQL = require(`./postgres-${module}`).extend_PostgreSQL(
48
PostgreSQL,
49
);
50
}
51
const theDBnew = new PostgreSQL(opts);
52
setupRecordConnectErrors(theDBnew);
53
theDB = theDBnew;
54
}
55
56
if (theDB == null) {
57
throw new Error("Fatal error setting up PostgreSQL instance");
58
}
59
return theDB;
60
}
61
62
import getPool from "./pool";
63
export { stripNullFields } from "./postgres/util";
64
export { getPool };
65
66