import type { Document, BulkWriteOptions, DeleteOptions, Filter, InsertManyResult, InsertOneOptions, InsertOneResult, OptionalUnlessRequiredId, UpdateFilter, UpdateOptions, DeleteResult, UpdateResult, WithId, DistinctOptions, InferIdType, FindOptions, AggregateOptions, WithoutId, ReplaceOptions, CountOptions, EstimatedDocumentCountOptions, CountDocumentsOptions, FindOneAndDeleteOptions, ModifyResult, FindOneAndReplaceOptions, FindOneAndUpdateOptions } from "mongodb"; import { ProjectionOfTSchema, FilterOfTSchema } from "mongo-collection-helpers"; import { AfterInternalSuccessEmitArgs, AfterInternalErrorEmitArgs, BeforeAfterCallbackArgsAndReturn, BeforeInternalEmitArgs, CommonDefinition, NoReturns, ReturnsArgs, ReturnsNamedEmitArg, ReturnsResult, SkipDocument, AfterInternalEmitArgs, BeforeStar, AfterStar, ExtractStandardBeforeAfterEventDefinitions } from "./helpersTypes.js"; import { StandardDefineHookOptions, StandardInvokeHookOptions } from "../awaiatableEventEmitter.js"; import { Abortable, Args, ArgsOrig, Caller, ErrorT, InvocationSymbol, ParentInvocationSymbol, Result, ResultOrError, ThisArg } from "./commentedTypes.js"; import { HookedAggregationCursorInterface } from "./hookedAggregationCursorInterface.js"; import { HookedFindCursorInterface } from "./hookedFindCursorInterface.js"; import { HookedCollectionInterface } from "./hookedCollectionInterface.js"; import { BeforeAfterErrorFindOnlyCursorEventDefinitions, FindCursorHookedEventMap } from "./findCursorEvents.js"; import { AggregationCursorHookedEventMap, BeforeAfterErrorAggregationOnlyCursorEventDefinitions } from "./aggregationCursorEvents.js"; import { BeforeAfterErrorGenericCursorEventDefinitions } from "./genericCursorEvents.js"; import { BeforeAfterErrorSharedEventDefinitions, SharedCallbackArgsAndReturn } from "./sharedEvents.js"; import { BeforeAfterEventNamesOfName } from "./index.js"; type WithDocumentDefineHookOptions = { /** The projection used when you call `.fullDocument()` it will be combined with the `projection` of every other hook being ran */ projection?: ProjectionOfTSchema | (({ argsOrig, thisArg }: { argsOrig: ARGS; thisArg: HookedCollectionInterface; }) => ProjectionOfTSchema); }; type AllowGreedyDefineHookOptions = { /** Whether to fetch the entire document as part of the initial cursor (vs using the cursor just for _id and loading the document lazily). Set this to true if you always need every document. */ greedyFetch?: boolean; }; type WithPreviousDocumentDefineHookOptions = { /** fetch the document before updating */ fetchPrevious?: boolean; /** The projection used to populate the previousDoc it will be combined with the `fetchPreviousProjection` of every other hook being ran */ fetchPreviousProjection?: ProjectionOfTSchema | (({ argsOrig, thisArg }: { argsOrig: ARGS; thisArg: HookedCollectionInterface; }) => ProjectionOfTSchema); }; export type CollectionOnlyBeforeAfterErrorEventDefinitions = BeforeAfterErrorCollectionEventDefinitions; export type CollectionBeforeAfterErrorEventDefinitions = CollectionOnlyBeforeAfterErrorEventDefinitions & BeforeAfterErrorFindOnlyCursorEventDefinitions & BeforeAfterErrorAggregationOnlyCursorEventDefinitions & BeforeAfterErrorGenericCursorEventDefinitions & BeforeAfterErrorSharedEventDefinitions; type ShouldRun = { shouldRun?({ argsOrig, thisArg }: { argsOrig: ARGS; thisArg: HookedCollectionInterface; }): Promise | boolean; }; type UpdateOrDeleteNDefineHookOptions = StandardDefineHookOptions & ShouldRun; type BeforeUpdateDefineHookOptions = StandardDefineHookOptions & ShouldRun> & WithDocumentDefineHookOptions>; type AfterUpdateDefineHookOptions = StandardDefineHookOptions & ShouldRun> & WithDocumentDefineHookOptions> & WithPreviousDocumentDefineHookOptions>; type BeforeDeleteDefineHookOptions = StandardDefineHookOptions & ShouldRun> & WithDocumentDefineHookOptions>; type AfterDeleteDefineHookOptions = StandardDefineHookOptions & ShouldRun> & WithPreviousDocumentDefineHookOptions>; type AfterInsertOptions = StandardDefineHookOptions & WithDocumentDefineHookOptions>; type BeforeTopLevelEmitArgs = { emitArgs: ThisArg & Args & InvocationSymbol & Abortable & O["custom"]; }; type AfterTopLevelSuccessEmitArgs = { emitArgs: ThisArg & Args & ArgsOrig & Result & InvocationSymbol & Abortable & O["custom"]; }; type AfterTopLevelErrorEmitArgs = { emitArgs: ThisArg & Args & ArgsOrig & InvocationSymbol & ErrorT & Abortable & O["custom"]; }; type AfterTopLevelEmitArgs = { emitArgs: ThisArg & Args & ArgsOrig & InvocationSymbol & ResultOrError & Abortable & O["custom"]; }; type UpdateCommon = { caller: "updateOne" | "updateMany" | "replaceOne" | "findOneAndUpdate" | "findOneAndReplace"; args: UpdateCallArgs | ReplaceCallArgs | FindOneAndUpdateCallArgs | FindOneAndReplaceCallArgs; thisArg: HookedCollectionInterface; result: WithId | UpdateResult | ModifyResult | null; beforeHookReturns: UpdateCommon["custom"]["filterMutator"] | typeof SkipDocument; custom: { /** The ID of the document being updated */ _id: InferIdType; filterMutator: { /** The filter used to identify the document. Originally this will the main filter, but you can return a mutated version per document. It will be combined with the document ID for the final update */ filter: MaybeStrictFilter; /** The per document mutator. Originally this will the main mutator, but you can return a mutated version per document - mutex with replacement */ mutator?: UpdateFilter | Partial; /** In the case of replaceOne calls, this will be the provided replacement - mutex with mutator */ replacement?: WithoutId; }; } & FullDocument; isPromise: true; }; type UpdateCommonEmitArgs = { emitArgs: UpdateCommon["custom"] & Caller> & ThisArg> & Args> & ArgsOrig> & InvocationSymbol & ParentInvocationSymbol & PreviousDocument & Abortable & FullDocument; }; type UpdateCommonResultEmitArgs = { emitArgs: Omit["emitArgs"], "caller"> & ({ caller: "findOneAndUpdate" | "findOneAndReplace"; result: WithId | ModifyResult | null; } | { caller: "updateOne" | "updateMany"; result: UpdateResult; } | { caller: "replaceOne"; result: UpdateResult; }); }; type UpdateCommonErrorEmitArgs = { emitArgs: UpdateCommonEmitArgs["emitArgs"] & ErrorT; }; type UpdateCommonErrorOrResultEmitArgs = { emitArgs: Omit["emitArgs"], "caller"> & { /** The error caused by the action. Mutually exclusive with result */ error?: any; } & ({ caller: "findOneAndUpdate" | "findOneAndReplace"; result?: WithId | ModifyResult | null; } | { caller: "updateOne" | "updateMany"; result?: UpdateResult; } | { caller: "replaceOne"; result: UpdateResult; }); }; type InsertCommon = { caller: "insertOne" | "insertMany" | "updateOne" | "updateMany" | "replaceOne" | "findOneAndUpdate" | "findOneAndReplace"; args: InsertManyCallArgs | InsertOneCallArgs | UpdateCallArgs | ReplaceCallArgs | FindOneAndReplaceCallArgs | FindOneAndUpdateCallArgs; beforeHookReturns: OptionalUnlessRequiredId | typeof SkipDocument; thisArg: HookedCollectionInterface; result: InsertOneResult | UpdateResult; custom: { /** The document to be inserted */ doc: OptionalUnlessRequiredId; }; isPromise: true; }; type DeleteCommon = { caller: "deleteOne" | "deleteMany" | "findOneAndDelete"; args: DeleteCallArgs | FindOneAndDeleteCallArgs; thisArg: HookedCollectionInterface; result: DeleteResult | WithId | ModifyResult | null; beforeHookReturns: MaybeStrictFilter | typeof SkipDocument; custom: { /** The ID of the document to be deleted */ _id: InferIdType; /** The filter used to identify the document. Originally this will the main filter, but you can return a mutated version per document. It will be combined with the document ID for the final deletion */ filter: MaybeStrictFilter; }; isPromise: true; }; type DeleteCommonEmitArgs = { emitArgs: DeleteCommon["custom"] & Caller> & ThisArg> & Args> & ArgsOrig> & InvocationSymbol & ParentInvocationSymbol & Abortable; }; type DeleteCommonResultEmitArgs = { emitArgs: Omit["emitArgs"], "caller"> & PreviousDocument & ({ caller: "findOneAndDelete"; result: WithId | ModifyResult | null; } | { caller: "deleteOne" | "deleteMany"; result: DeleteResult; }); }; type DeleteCommonErrorEmitArgs = { emitArgs: DeleteCommonEmitArgs["emitArgs"] & ErrorT; }; type DeleteCommonErrorOrResultEmitArgs = { emitArgs: Omit["emitArgs"], "caller"> & PreviousDocument & { /** The error caused by the action. Mutually exclusive with result */ error?: any; } & ({ caller: "findOneAndDelete"; result?: WithId | ModifyResult | null; } | { caller: "deleteOne" | "deleteMany"; result?: DeleteResult; }); }; type FullDocument = { /** Returns a document the projection of which is the union of all hooks' projections. This function will result in at most one database operation per document, regardless of how many times it's called across all hooks. */ getDocument(): Promise; }; type PreviousDocument = { /** A copy of the document from before the update was made */ previousDocument?: Document | undefined | null; }; type MaybeOrderedBatch = { /** Whether to run the operations in parallel or serially. For operations that support this option, it will be sent to the DB too, otherwise only meaningful if there are individual hooks (e.g., after.insert for an insertMany) */ ordered?: boolean; /** If ordered: false, how many operations to run in parallel (defaults to 1000) - there is a potential memory cost to running too many in parallel, but a wallclock cost to running them all serially */ hookBatchSize?: number; }; type AllCollectionEventDefinitions = ExtractStandardBeforeAfterEventDefinitions>; type CollectionCallbackArgsAndReturn = BeforeAfterCallbackArgsAndReturn>; /** * @external */ export type CollectionHookedEventMap = CollectionCallbackArgsAndReturn & FindCursorHookedEventMap & AggregationCursorHookedEventMap & SharedCallbackArgsAndReturn; type ReplaceProjection = Omit & { projection?: ProjectionOfTSchema | Document; }; export type AmendedInsertOneOptions = StandardInvokeHookOptions> & InsertOneOptions; export type AmendedBulkWriteOptions = StandardInvokeHookOptions> & BulkWriteOptions & AlwaysAttemptOperation; export type AmendedUpdateOptions = StandardInvokeHookOptions> & UpdateOptions & AlwaysAttemptOperation & MaybeOrderedBatch; export type AmendedDeleteOptions = StandardInvokeHookOptions> & DeleteOptions & AlwaysAttemptOperation & MaybeOrderedBatch; export type AmendedAggregateOptions = StandardInvokeHookOptions> & AggregateOptions; export type AmendedReplaceOptions = StandardInvokeHookOptions> & ReplaceOptions & AlwaysAttemptOperation; export type AmendedDistinctOptions = StandardInvokeHookOptions> & DistinctOptions; export type AmendedFindOptions = StandardInvokeHookOptions> & ReplaceProjection, TSchema>; export type AmendedFindOneOptions = StandardInvokeHookOptions> & ReplaceProjection, TSchema>; export type AmendedEstimatedDocumentCountOptions = StandardInvokeHookOptions> & EstimatedDocumentCountOptions; export type AmendedCountOptions = StandardInvokeHookOptions> & CountOptions; export type AmendedCountDocumentsOptions = StandardInvokeHookOptions> & CountDocumentsOptions; export type AmendedFindOneAndDeleteOptions = StandardInvokeHookOptions> & ReplaceProjection & AlwaysAttemptOperation; export type AmendedFindOneAndUpdateOptions = StandardInvokeHookOptions> & ReplaceProjection & AlwaysAttemptOperation; export type AmendedFindOneAndReplaceOptions = StandardInvokeHookOptions> & ReplaceProjection & AlwaysAttemptOperation; type AlwaysAttemptOperation = { /** In the case of underlying implementations with a partial view (e.g., client side) always attempt the underlying operation, omitting those explicitly attempted. Only useful if `update` or `delete` hooks are in use */ alwaysAttemptOperation?: boolean; }; export type MaybeStrictFilter = FilterOfTSchema | Filter; type InsertOneCallArgs = readonly [OptionalUnlessRequiredId, AmendedInsertOneOptions | undefined]; type InsertManyCallArgs = readonly [OptionalUnlessRequiredId[], AmendedBulkWriteOptions | undefined]; type FindCallArgs = readonly [MaybeStrictFilter | undefined, AmendedFindOptions | undefined]; type AggregateCallArgs = readonly [Document[], AmendedAggregateOptions | undefined]; export type UpdateCallArgs = readonly [MaybeStrictFilter, UpdateFilter | Partial, AmendedUpdateOptions | undefined]; export type ReplaceCallArgs = readonly [MaybeStrictFilter, WithoutId, AmendedReplaceOptions | undefined]; type DeleteCallArgs = readonly [MaybeStrictFilter, AmendedDeleteOptions | undefined]; type DistinctCallArgs = readonly [keyof WithId, MaybeStrictFilter, AmendedDistinctOptions]; type CountCallArgs = readonly [MaybeStrictFilter | undefined, AmendedCountOptions | undefined]; type EstimatedDocumentCountCallArgs = readonly [AmendedEstimatedDocumentCountOptions | undefined]; type CountDocumentsCallArgs = readonly [MaybeStrictFilter | undefined, AmendedCountDocumentsOptions | undefined]; export type FindOneAndDeleteCallArgs = readonly [MaybeStrictFilter, AmendedFindOneAndDeleteOptions | undefined]; export type FindOneAndUpdateCallArgs = readonly [MaybeStrictFilter, UpdateFilter, AmendedFindOneAndUpdateOptions | undefined]; export type FindOneAndReplaceCallArgs = readonly [MaybeStrictFilter, WithoutId, AmendedFindOneAndReplaceOptions | undefined]; export type UpsertCallArgs = ActualCaller extends "updateOne" | "updateMany" ? UpdateCallArgs : ActualCaller extends "replaceOne" ? ReplaceCallArgs : ActualCaller extends "findOneAndUpdate" ? FindOneAndUpdateCallArgs : ActualCaller extends "findOneAndReplace" ? FindOneAndReplaceCallArgs : never; export type UpsertAndCallerCallArgs = ActualCaller extends "updateOne" | "updateMany" ? { caller: ActualCaller; args: UpdateCallArgs; } : ActualCaller extends "replaceOne" ? { caller: ActualCaller; args: ReplaceCallArgs; } : ActualCaller extends "findOneAndUpdate" ? { caller: ActualCaller; args: FindOneAndUpdateCallArgs; } : ActualCaller extends "findOneAndReplace" ? { caller: ActualCaller; args: FindOneAndReplaceCallArgs; } : never; type TopLevelCall = { before: ReturnsArgs & BeforeTopLevelEmitArgs & (O extends { options: StandardDefineHookOptions; } ? Pick : { options: StandardDefineHookOptions; }); success: ReturnsResult & AfterTopLevelSuccessEmitArgs & (O extends { options: StandardDefineHookOptions; } ? Pick : { options: StandardDefineHookOptions; }); error: NoReturns & AfterTopLevelErrorEmitArgs & (O extends { options: StandardDefineHookOptions; } ? Pick : { options: StandardDefineHookOptions; }); after: ReturnsResult & AfterTopLevelEmitArgs & (O extends { options: StandardDefineHookOptions; } ? Pick : { options: StandardDefineHookOptions; }); caller: never; options: O extends { options: StandardDefineHookOptions; } ? O["options"] : StandardDefineHookOptions; }; export type CountCommon = { args: CountCallArgs | CountDocumentsCallArgs | EstimatedDocumentCountCallArgs; thisArg: HookedCollectionInterface; isPromise: true; result: number; custom: { operation: "count" | "countDocuments" | "estimatedDocumentCount"; }; }; export type FindOneCommon = { args: FindCallArgs | FindOneAndDeleteCallArgs | FindOneAndUpdateCallArgs | FindOneAndReplaceCallArgs; thisArg: HookedCollectionInterface; isPromise: true; result: WithId | ModifyResult | null; custom: { operation: "findOne" | "findOneAndDelete" | "findOneAndUpdate" | "findOneAndReplace"; }; }; export type ExternalBeforeAfterEvent = { before: ReturnsArgs & BeforeTopLevelEmitArgs & (O extends { options: StandardDefineHookOptions; } ? Pick : { options: StandardDefineHookOptions; }); success: ReturnsResult & AfterTopLevelSuccessEmitArgs & (O extends { options: StandardDefineHookOptions; } ? Pick : { options: StandardDefineHookOptions; }); error: NoReturns & AfterTopLevelErrorEmitArgs & (O extends { options: StandardDefineHookOptions; } ? Pick : { options: StandardDefineHookOptions; }); after: ReturnsResult & AfterTopLevelEmitArgs & (O extends { options: StandardDefineHookOptions; } ? Pick : { options: StandardDefineHookOptions; }); caller?: any; options?: O extends { options: StandardDefineHookOptions; } ? O["options"] : StandardDefineHookOptions; forCollection: O["forCollection"] extends false ? false : true; forCursor: O["forCursor"] extends true ? true : false; }; export type BeforeAfterErrorCollectionEventDefinitions = { aggregate: TopLevelCall<{ args: AggregateCallArgs; thisArg: HookedCollectionInterface; result: HookedAggregationCursorInterface; isPromise: false; }>; findOne: TopLevelCall<{ args: FindCallArgs; thisArg: HookedCollectionInterface; result: Document | null; }>; find: TopLevelCall<{ args: FindCallArgs; thisArg: HookedCollectionInterface; result: HookedFindCursorInterface; isPromise: false; }>; insertOne: TopLevelCall<{ args: InsertOneCallArgs; thisArg: HookedCollectionInterface; result: InsertOneResult; options: StandardDefineHookOptions; }>; insertMany: TopLevelCall<{ args: InsertManyCallArgs; thisArg: HookedCollectionInterface; result: InsertManyResult; }>; insert: { before: ReturnsNamedEmitArg, "beforeHookReturns"> & BeforeInternalEmitArgs>, "doc"> & { options: StandardDefineHookOptions; caller: InsertCommon["caller"]; }; success: ReturnsResult> & AfterInternalSuccessEmitArgs> & { options: AfterInsertOptions; caller: InsertCommon["caller"]; }; error: NoReturns & AfterInternalErrorEmitArgs> & { caller: InsertCommon["caller"]; }; after: ReturnsResult> & AfterInternalEmitArgs> & { options: StandardDefineHookOptions; caller: InsertCommon["caller"]; }; caller: InsertCommon["caller"]; options: StandardDefineHookOptions; }; delete: { before: ReturnsNamedEmitArg, "beforeHookReturns"> & { emitArgs: DeleteCommonEmitArgs["emitArgs"] & FullDocument; }, "filter"> & { options: BeforeDeleteDefineHookOptions & AllowGreedyDefineHookOptions; caller: DeleteCommon["caller"]; }; success: ReturnsResult> & DeleteCommonResultEmitArgs & { options: AfterDeleteDefineHookOptions; caller: DeleteCommon["caller"]; }; error: NoReturns & DeleteCommonErrorEmitArgs & { options: AfterDeleteDefineHookOptions; caller: DeleteCommon["caller"]; }; after: ReturnsResult> & DeleteCommonErrorOrResultEmitArgs & { options: AfterDeleteDefineHookOptions; caller: DeleteCommon["caller"]; }; caller: DeleteCommon["caller"]; options: StandardDefineHookOptions; }; deleteOne: TopLevelCall<{ args: DeleteCallArgs; thisArg: HookedCollectionInterface; result: DeleteResult; custom: { _id?: InferIdType[]; }; options: StandardDefineHookOptions & UpdateOrDeleteNDefineHookOptions> & { includeId?: boolean; }; }>; deleteMany: TopLevelCall<{ args: DeleteCallArgs; thisArg: HookedCollectionInterface; result: DeleteResult; custom: { _ids?: InferIdType[]; }; options: StandardDefineHookOptions & UpdateOrDeleteNDefineHookOptions> & { includeIds?: boolean; }; }>; replaceOne: TopLevelCall<{ args: ReplaceCallArgs; thisArg: HookedCollectionInterface; result: UpdateResult | Document; }>; updateOne: TopLevelCall<{ args: UpdateCallArgs; thisArg: HookedCollectionInterface; result: UpdateResult; custom: { _id?: InferIdType; }; options: StandardDefineHookOptions & UpdateOrDeleteNDefineHookOptions> & { includeId?: boolean; }; }>; updateMany: TopLevelCall<{ args: UpdateCallArgs; thisArg: HookedCollectionInterface; result: UpdateResult; custom: { _ids?: InferIdType[]; }; options: StandardDefineHookOptions & UpdateOrDeleteNDefineHookOptions> & { includeIds?: boolean; }; }>; update: { before: ReturnsNamedEmitArg, "beforeHookReturns"> & BeforeInternalEmitArgs & FullDocument>, "filterMutator"> & { options: BeforeUpdateDefineHookOptions & AllowGreedyDefineHookOptions; caller: UpdateCommon["caller"]; }; success: ReturnsResult> & UpdateCommonResultEmitArgs & { options: AfterUpdateDefineHookOptions; caller: UpdateCommon["caller"]; }; error: NoReturns & UpdateCommonErrorEmitArgs & { options: AfterUpdateDefineHookOptions; caller: UpdateCommon["caller"]; }; after: ReturnsResult> & UpdateCommonErrorOrResultEmitArgs & { options: AfterUpdateDefineHookOptions; caller: UpdateCommon["caller"]; }; caller: UpdateCommon["caller"]; options: StandardDefineHookOptions; }; distinct: TopLevelCall<{ args: DistinctCallArgs; thisArg: HookedCollectionInterface; result: any[]; }>; count: TopLevelCall<{ args: CountCallArgs; thisArg: HookedCollectionInterface; result: number; }>; countDocuments: TopLevelCall<{ args: CountDocumentsCallArgs; thisArg: HookedCollectionInterface; result: number; }>; estimatedDocumentCount: TopLevelCall<{ args: EstimatedDocumentCountCallArgs; thisArg: HookedCollectionInterface; result: number; }>; "count*": { before: ReturnsArgs> & BeforeStar>; success: ReturnsResult> & AfterStar, Result>>; after: ReturnsResult> & AfterStar, ResultOrError>>; error: NoReturns & AfterStar, ErrorT>; caller: never; }; "findOne*": { before: ReturnsArgs> & BeforeStar>; success: ReturnsResult> & AfterStar, Result>>; after: ReturnsResult> & AfterStar, ResultOrError>>; error: NoReturns & AfterStar, ErrorT>; caller: never; }; findOneAndDelete: TopLevelCall<{ args: FindOneAndDeleteCallArgs; thisArg: HookedCollectionInterface; result: WithId | ModifyResult | null; }>; findOneAndUpdate: TopLevelCall<{ args: FindOneAndUpdateCallArgs; thisArg: HookedCollectionInterface; result: ModifyResult | WithId | null; }>; findOneAndReplace: TopLevelCall<{ args: FindOneAndReplaceCallArgs; thisArg: HookedCollectionInterface; result: ModifyResult | WithId | null; }>; }; export {};