Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/conat/sync/synctable.ts
1453 views
1
import { SyncTableKV } from "./synctable-kv";
2
import { SyncTableStream } from "./synctable-stream";
3
import refCache from "@cocalc/util/refcache";
4
import { type KVLimits } from "./limits";
5
import { type FilteredStreamLimitOptions } from "./limits";
6
import jsonStableStringify from "json-stable-stringify";
7
import { type Client } from "@cocalc/conat/core/client";
8
9
export type ConatSyncTable = SyncTableStream | SyncTableKV;
10
11
export type ConatSyncTableFunction = (
12
query: { [table: string]: { [field: string]: any }[] },
13
options?: {
14
obj?: object;
15
atomic?: boolean;
16
immutable?: boolean;
17
stream?: boolean;
18
pubsub?: boolean;
19
throttleChanges?: number;
20
// for tables specific to a project, e.g., syncstrings in a project
21
project_id?: string;
22
},
23
) => Promise<ConatSyncTable>;
24
25
// When the database is watching tables for changefeeds, if it doesn't
26
// get a clear expression of interest from a client every this much time,
27
// it stops managing the changefeed to save resources.
28
29
export const CHANGEFEED_INTEREST_PERIOD_MS = 120000;
30
// export const CHANGEFEED_INTEREST_PERIOD_MS = 3000;
31
32
export interface SyncTableOptions {
33
query;
34
client?: Client;
35
account_id?: string;
36
project_id?: string;
37
atomic?: boolean;
38
stream?: boolean;
39
immutable?: boolean; // if true, then get/set works with immutable.js objects instead.
40
noCache?: boolean;
41
limits?: Partial<KVLimits> | Partial<FilteredStreamLimitOptions>;
42
desc?: any;
43
start_seq?: number;
44
noInventory?: boolean;
45
ephemeral?: boolean;
46
}
47
48
export const createSyncTable = refCache<SyncTableOptions, ConatSyncTable>({
49
name: "synctable",
50
createKey: (opts: SyncTableOptions) =>
51
jsonStableStringify({ ...opts, client: undefined })!,
52
createObject: async (options: SyncTableOptions & { client: Client }) => {
53
let t;
54
if (options.stream) {
55
t = new SyncTableStream(options);
56
} else {
57
t = new SyncTableKV(options);
58
}
59
await t.init();
60
return t;
61
},
62
});
63
64