mongo2elastic
Version:
Sync MongoDB collections to Elasticsearch
60 lines (59 loc) • 1.98 kB
TypeScript
import type { JSONSchema } from 'mongochangestream';
import type { ChangeStreamDocument, Document } from 'mongodb';
import { type Mapper } from 'obj-walker';
interface RenameOption {
/** Dotted path to renamed dotted path */
rename?: Record<string, string>;
}
export interface SyncOptions extends RenameOption {
mapper?: (doc: Document) => Document;
index?: string;
}
export interface Override extends Record<string, any> {
path: string;
mapper?: (obj: JSONSchema, path: string) => JSONSchema;
}
export interface ConvertOptions extends RenameOption {
/**
* An obj-walker Mapper function that can be used as an "escape hatch" to
* preprocess each node in the object (using `map`) before using `walk` to
* convert the object into the output Elastic mapping.
*
* This can be useful in situations where you want to replace or remove a
* non-leaf node.
*
* @example
* ```typescript
* const mapSchema = ({ path, val }) => {
* if (_.isEqual(path, ['properties', 'addresses', 'items'])) {
* // This should result in removing all other properties (e.g.
* // `properties`, `additionalProperties`) besides `bsonType`.
* return { bsonType: 'object' }
* }
*
* return val
* }
* ```
*/
mapSchema?: Mapper;
omit?: string[];
overrides?: Override[];
passthrough?: string[];
}
export type Events = 'process';
export type OperationCounts = Partial<Record<ChangeStreamDocument['operationType'], number>>;
interface BaseProcessEvent {
type: 'process';
success: number;
fail?: number;
errors?: unknown[];
operationCounts: OperationCounts;
}
export interface InitialScanProcessEvent extends BaseProcessEvent {
initialScan: true;
}
export interface ChangeStreamProcessEvent extends BaseProcessEvent {
changeStream: true;
}
export type ProcessEvent = InitialScanProcessEvent | ChangeStreamProcessEvent;
export {};