mongochangestream
Version:
Sync MongoDB collections via change streams into any database.
65 lines (64 loc) • 2.58 kB
TypeScript
import { ChangeStreamInsertDocument, type ChangeStreamUpdateDocument, type Collection, Document } from 'mongodb';
import { type Options } from 'p-retry';
import type { CursorError, JSONSchema } from './types.js';
export declare const setDefaults: (keys: string[], val: any) => Record<string, any>;
export declare const generatePipelineFromOmit: (omit: string[]) => {
$unset: string[];
}[];
/**
* Dotted path updates like { $set: {'a.b.c': 'foo'} } result in the following:
* ```ts
* {
* updatedDescription: {
* updateFields: {
* 'a.b.c': 'foo'
* }
* }
* }
* ```
* Therefore, to remove 'a.b' we have to walk the `updateFields` object
* and unset the omitted paths.
*/
export declare const omitFieldsForUpdate: (omittedPaths: string[], event: ChangeStreamUpdateDocument) => void;
export declare const getCollectionKey: (collection: Collection) => string;
export declare const traverseSchema: (x: JSONSchema) => any;
/**
* Remove unused schema fields
*/
export declare const removeUnusedFields: (schema: JSONSchema) => JSONSchema;
export declare function when<T, R>(condition: any, fn: (x: T) => R): (x: T) => T | R;
/**
* Check if error message indicates a missing or invalid oplog entry.
*/
export declare const missingOplogEntry: (error: CursorError) => boolean;
/**
* Creates a delayed function that only invokes fn at most once every ms.
* The first invocation will be the one that gets executed. Subsequent calls
* to the delayed function will be dropped if there is a pending invocation.
*
* The delayed function comes with a cancel method to cancel the delayed fn
* invocation and a flush method to immediately invoke it.
*/
export declare const delayed: (fn: (...args: any[]) => void, ms: number) => {
(...args: any[]): void;
cancel(): void;
flush(): void;
};
/**
* Convert an initial scan document to a change stream insert document
* suitable for downstream consumption. Note: not all fields are present,
* such as, _id (resume token).
*/
export declare const docToChangeStreamInsert: (collection: Collection) => (doc: Document) => ChangeStreamInsertDocument;
/**
* Rename keys, mutating the given document. Used in downstream libraries.
*/
export declare const renameKeys: (doc: Document, keys: Record<string, string>) => void;
/**
* Wrap non-Error exceptions in an Error.
*/
export declare const castError: (err: unknown) => Error;
/**
* Safely call retry, wrapping non-Error exceptions in an Error.
*/
export declare const safeRetry: <T>(fn: () => PromiseLike<T> | T, options?: Options) => Promise<T>;