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/postgres/personal.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
Functionality related to running cocalc in personal mode.
8
*/
9
10
import { PostgreSQL } from "./types";
11
import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
12
import { uuid } from "@cocalc/util/misc";
13
14
async function _get_personal_user(database: PostgreSQL): Promise<string> {
15
// Get account_id of the one and only user, or if there is no user, create one and return its account_id.
16
17
const result = await database.async_query({
18
query:
19
"SELECT account_id FROM accounts WHERE created is not NULL ORDER BY created LIMIT 1",
20
});
21
for (const row of result.rows) {
22
return row.account_id;
23
}
24
// No results, so create THE account.
25
const account_id = uuid();
26
await database.async_query({
27
query: "INSERT INTO accounts",
28
values: {
29
account_id,
30
first_name: "Your",
31
last_name: "Name",
32
created: new Date(),
33
groups: ["admin"],
34
},
35
});
36
return account_id;
37
}
38
39
export const get_personal_user = reuseInFlight(_get_personal_user, {
40
createKey: () => "",
41
});
42
43