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