ts-patch-mongoose
Version:
Patch history & events for mongoose models
63 lines (57 loc) • 1.91 kB
text/typescript
import { HydratedDocument, Types, Query, Schema } from 'mongoose';
import { Operation } from 'fast-json-patch';
import EventEmitter from 'node:events';
import ms from 'ms';
interface History {
op: string;
modelName: string;
collectionName: string;
collectionId: Types.ObjectId;
version: number;
doc?: object;
user?: object;
reason?: string;
metadata?: object;
patch?: Operation[];
}
interface PatchEvent<T> {
oldDoc?: HydratedDocument<T>;
doc?: HydratedDocument<T>;
patch?: Operation[];
}
interface PatchContext<T> {
op: string;
modelName: string;
collectionName: string;
isNew?: boolean;
createdDocs?: HydratedDocument<T>[];
deletedDocs?: HydratedDocument<T>[];
ignoreEvent?: boolean;
ignorePatchHistory?: boolean;
}
type HookContext<T> = Query<T, T> & {
op: string;
_context: PatchContext<T>;
};
type User = Record<string, unknown>;
type Metadata = Record<string, unknown>;
interface PluginOptions<T> {
modelName?: string;
collectionName?: string;
eventUpdated?: string;
eventCreated?: string;
eventDeleted?: string;
getUser?: (doc: HydratedDocument<T>) => Promise<User> | User;
getReason?: (doc: HydratedDocument<T>) => Promise<string> | string;
getMetadata?: (doc: HydratedDocument<T>) => Promise<Metadata> | Metadata;
omit?: string[];
patchHistoryDisabled?: boolean;
preDelete?: (docs: HydratedDocument<T>[]) => Promise<void>;
}
declare class PatchEventEmitter extends EventEmitter {
}
declare const em: PatchEventEmitter;
declare const setPatchHistoryTTL: (ttl: number | ms.StringValue) => Promise<void>;
declare const patchHistoryPlugin: <T>(schema: Schema<T>, opts: PluginOptions<T>) => void;
export { em as patchEventEmitter, patchHistoryPlugin, setPatchHistoryTTL };
export type { History, HookContext, Metadata, PatchContext, PatchEvent, PluginOptions, User };