mongo-oplog2
Version:
Simple monitoring of MongoDB oplog.
149 lines (148 loc) • 5.86 kB
TypeScript
import { Cursor, Db, MongoClientOptions, Timestamp } from "mongodb";
import EventEmitter from "./eventemitter";
import { FilteredMongoOplog } from "./filter";
import { OplogDoc, OplogQuery, PrettyOplogDoc } from "./util";
export { getOpName, getTimestamp, OplogDoc, PrettyOplogDoc, prettify } from "./util";
export { FilteredMongoOplog } from "./filter";
export interface MongoOplogInterface<isPretty extends boolean> extends EventEmitter<Events<isPretty>> {
ignore: boolean;
pretty: boolean;
filter(): FilteredMongoOplog<isPretty>;
filter(ns: string): FilteredMongoOplog<isPretty>;
isCurrent(): Promise<boolean>;
isCurrent(ts: Timestamp): Promise<boolean>;
stop(): Promise<this>;
tail(): Promise<Cursor | undefined>;
destroy(): Promise<this>;
}
declare type PrettyOption = {
pretty: true;
};
export declare type OplogType<O extends boolean> = O extends true ? PrettyOplogDoc : OplogDoc;
declare type OptionsType<O extends boolean> = O extends true ? Options & PrettyOption : Options;
declare type OptTypeIsPretty<T extends Options> = T extends PrettyOption ? true : false;
/**
* Allows tailing the MongoDB oplog.
*/
declare class MongoOplogImpl<isPretty extends boolean = false> extends EventEmitter<Events<isPretty>> implements MongoOplogInterface<isPretty> {
ignore: boolean;
pretty: boolean;
private _client;
private _db?;
private dbOpts?;
private uri;
private ns;
private collectionName;
private _stream?;
private _ts;
private tailing;
private _oplogFilter?;
/**
* @param uriOrDb a connection string or existing database connection
* @param opts options for the `MongoOplog` instance. Any options not
* beyond thosed used by `MongoOplog` will be stored and
* passed along to the `mongodb` driver when creating a
* database connection.
*/
constructor(uriOrDb?: string, opts?: OptionsType<isPretty>);
/**
* Returns `true` if database is connected; false otherwise.
*/
get connected(): boolean;
/**
* The database connection.
*/
get db(): Db | undefined;
/**
* The underlying MongoDB cursor stream.
*/
get stream(): Cursor | undefined;
/**
* Last processed timestamp.
*/
get ts(): Timestamp;
/**
* Returns an event emitter that will emit database events for the specified
* name space.
* @param ns namespace for the filter.
*/
filter(ns?: string): FilteredMongoOplog<isPretty>;
/**
* Stop tailing the oplog, disconnect from the database, and emit the
* "destroy" event.
*/
destroy(): Promise<this>;
/**
* If a timestamp is not provided then returns `true` if either no
* document was found or the `ts` value of the document matches the
* internally tracked ts; `false` otherwise.
* If timestamp is provided returs `true` only if the document returned
* matches the specified timestamp. If `null` is returned an Error is
* raised. If a document is returned but the `ts` property does not match
* the specified `ts` returns `false`.
* @param ts optional timestamp to check, if not supplied use internal
*/
isCurrent(ts?: Timestamp): Promise<boolean>;
/**
* Stop tailing the oplog and destroy the underlying cursor. Tailing
* can be resumed by calling the `tail` function.
*/
stop(): Promise<this>;
/**
* Start tailing the oplog.
*/
tail(): Promise<Cursor | undefined>;
/**
* Connect to the database.
*/
private connect;
/**
* Disconnect from the database by calling `close`.
* If database connection is externally supplied do NOT call `close`.
*/
private disconnect;
}
export declare const MongoOplog: MongoOplogConstructor;
export declare type MongoOplog<isPretty extends boolean> = MongoOplogImpl<isPretty>;
export interface MongoOplogConstructor<isPretty extends boolean = false> {
new <T extends Options>(uriOrDb?: string | Db, opts?: T): MongoOplog<OptTypeIsPretty<T>>;
}
export interface Options {
coll?: string;
ns?: string;
pretty?: boolean;
since?: number | string;
filter?: OplogQuery;
mongo?: MongoClientOptions;
}
/**
* Creates an instance of MongoOplog. This method exists for backwards compatibility
* with the `mongo-oplog` package. Normal expected behavior would be to call new
* directly.
*/
export declare function createInstance(): MongoOplog<false>;
export declare function createInstance(uri: string): MongoOplog<false>;
export declare function createInstance<OptType extends Options>(uri: string, opts: OptType): MongoOplog<OptTypeIsPretty<OptType>>;
export declare function createInstance(db: Db): MongoOplog<false>;
export declare function createInstance<OptType extends Options>(db: Db, opts: OptType): MongoOplog<OptTypeIsPretty<OptType>>;
export declare const OplogEvents: readonly ["delete", "insert", "op", "update", "noop"];
export declare type OplogEvents<isPretty extends boolean = false> = {
delete: [OplogType<isPretty>];
insert: [OplogType<isPretty>];
update: [OplogType<isPretty>];
op: [OplogType<isPretty>];
noop: [void];
};
export declare const MongoOplogStatus: readonly ["connect", "disconnect", "destroy", "end", "error", "tail-start", "tail-end"];
export declare type MongoOplogStatus = {
connect: [void];
disconnect: [void];
destroy: [void];
error: [Error];
end: [void];
"tail-start": [void];
"tail-end": [void];
};
export declare const Events: readonly ("disconnect" | "error" | "end" | "connect" | "insert" | "update" | "delete" | "noop" | "op" | "destroy" | "tail-start" | "tail-end")[];
export declare type Events<isPretty extends boolean> = OplogEvents<isPretty> & MongoOplogStatus;
export default createInstance;