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/editor/db/sync.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 { SyncDoc, SyncOpts0, SyncOpts } from "../generic/sync-doc";
7
import { from_str, DBDocument } from "./doc";
8
import { Document, DocType } from "../generic/types";
9
10
export interface SyncDBOpts0 extends SyncOpts0 {
11
primary_keys: string[];
12
string_cols: string[];
13
}
14
15
export interface SyncDBOpts extends SyncDBOpts0 {
16
from_str: (str: string) => Document;
17
doctype: DocType;
18
}
19
20
export class SyncDB extends SyncDoc {
21
constructor(opts: SyncDBOpts0) {
22
// Typescript question -- What is the right way to do this?
23
const opts1: SyncDBOpts = opts as unknown as SyncDBOpts;
24
if (opts1.primary_keys == null || opts1.primary_keys.length <= 0) {
25
throw Error("primary_keys must have length at least 1");
26
}
27
opts1.from_str = (str) =>
28
from_str(str, opts1.primary_keys, opts1.string_cols);
29
opts1.doctype = {
30
type: "db",
31
patch_format: 1,
32
opts: {
33
primary_keys: opts1.primary_keys,
34
string_cols: opts1.string_cols,
35
},
36
};
37
super(opts1 as SyncOpts);
38
}
39
40
get_one(arg?) {
41
// I know it is really of type DBDocument.
42
return (this.get_doc() as DBDocument).get_one(arg);
43
}
44
}
45
46