UNPKG

mongochangestream

Version:

Sync MongoDB collections via change streams into any database.

79 lines (78 loc) 3.21 kB
import { EventEmitter } from 'eventemitter3'; import type { Redis } from 'ioredis'; import { type Collection, type Db } from 'mongodb'; import { type QueueOptions } from 'prom-utils'; import type { ChangeOptions, ChangeStreamOptions, Events, JSONSchema, OperationType, ProcessChangeStreamRecords, ProcessInitialScanRecords, ScanOptions, State, SyncOptions } from './types.js'; /** * Get Redis keys used for the collection. */ export declare const getKeys: (collection: Collection, options: SyncOptions) => { scanCompletedKey: string; lastScanIdKey: string; changeStreamTokenKey: string; schemaKey: string; lastChangeProcessedAtKey: string; lastScanProcessedAtKey: string; resyncKey: string; }; export declare function initSync<ExtendedEvents extends EventEmitter.ValidEventTypes>(redis: Redis, collection: Collection, options?: SyncOptions): { runInitialScan: <T = any>(processRecords: ProcessInitialScanRecords, options?: QueueOptions & ScanOptions<T>) => Promise<{ start: () => Promise<void>; stop: () => Promise<void>; restart: () => Promise<void>; state: Pick<{ get: () => State; change: (newState: State) => void; waitForChange: (...newStates: State[]) => Promise<void>; is: (...states: State[]) => boolean; canChange: (newState: State) => boolean; maybeChange: (newState: State) => boolean; getElapsedTime: () => number; }, "get" | "is">; }>; processChangeStream: <T extends OperationType[] | undefined = undefined>(processRecords: ProcessChangeStreamRecords<T>, options?: QueueOptions & ChangeStreamOptions<T>) => Promise<{ start: () => Promise<void>; stop: () => Promise<void>; restart: () => Promise<void>; state: Pick<{ get: () => State; change: (newState: State) => void; waitForChange: (...newStates: State[]) => Promise<void>; is: (...states: State[]) => boolean; canChange: (newState: State) => boolean; maybeChange: (newState: State) => boolean; getElapsedTime: () => number; }, "get" | "is">; }>; reset: () => Promise<void>; getCollectionSchema: (db: Db) => Promise<JSONSchema>; detectSchemaChange: (db: Db, options?: ChangeOptions) => Promise<{ start: () => void; stop: () => void; }>; detectResync: (resyncCheckInterval?: number) => { start: () => void; stop: () => void; }; keys: { scanCompletedKey: string; lastScanIdKey: string; changeStreamTokenKey: string; schemaKey: string; lastChangeProcessedAtKey: string; lastScanProcessedAtKey: string; resyncKey: string; }; emitter: EventEmitter<Events | ExtendedEvents, any>; /** * Pause and resume all syncing functions at once. A call to `stop` * for `runInitialScan` or `processChangeStream` will resume to prevent * hanging. */ pausable: { pause: () => void; resume: () => void; maybeBlock: () => Promise<void> | undefined; readonly isPaused: boolean; }; };