diff --git a/src/bulk/common.ts b/src/bulk/common.ts index 077fd698f3..d25fe09753 100644 --- a/src/bulk/common.ts +++ b/src/bulk/common.ts @@ -1216,9 +1216,15 @@ export abstract class BulkOperationBase { return batches; } - /** An internal helper method. Do not invoke directly. Will be going away in the future */ + execute(options?: BulkWriteOptions): Promise; + execute(callback: Callback): void; + execute(options: BulkWriteOptions | undefined, callback: Callback): void; execute( - options?: BulkWriteOptions, + options?: BulkWriteOptions | Callback, + callback?: Callback + ): Promise | void; + execute( + options?: BulkWriteOptions | Callback, callback?: Callback ): Promise | void { if (typeof options === 'function') (callback = options), (options = {}); diff --git a/test/types/community/bulk/bulk-operation-base.test-d.ts b/test/types/community/bulk/bulk-operation-base.test-d.ts new file mode 100644 index 0000000000..45e7c44aaf --- /dev/null +++ b/test/types/community/bulk/bulk-operation-base.test-d.ts @@ -0,0 +1,81 @@ +import { expectType } from 'tsd'; + +import { + BulkWriteResult, + MongoClient, + BatchType, + UpdateStatement, + DeleteStatement, + AnyError, + BulkWriteOptions, + Callback, + Document +} from '../../../../src/index'; +import { BulkOperationBase, Batch } from '../../../../src/bulk/common'; + +const client = new MongoClient(''); +const db = client.db('test'); +const collection = db.collection('test'); + +class TestBulkOperation extends BulkOperationBase { + constructor() { + super(collection, {}, true); + } + + addToOperationsList( + batchType: BatchType, + document: Document | UpdateStatement | DeleteStatement + ): this { + this.s.currentBatch = new Batch(batchType, 0); + this.s.currentBatch.operations.push(document); + return this; + } +} + +const bulkOperation = new TestBulkOperation(); + +// execute + +const options: BulkWriteOptions = {}; + +expectType>(bulkOperation.execute()); + +expectType>(bulkOperation.execute(options)); + +// ensure we can use the bulk operation execute in a callback based wrapper function +function extendedPromiseBasedBulkExecute( + optionalOptions?: BulkWriteOptions +): Promise { + return bulkOperation.execute(optionalOptions); +} + +expectType>(extendedPromiseBasedBulkExecute()); + +expectType( + bulkOperation.execute((error, bulkWriteResult) => { + expectType(error); + expectType(bulkWriteResult); + }) +); + +expectType( + bulkOperation.execute(options, (error, bulkWriteResult) => { + expectType(error); + expectType(bulkWriteResult); + }) +); + +// ensure we can use the bulk operation execute in a callback based wrapper function +function extendedCallbackBasedBulkExecute( + callback: Callback, + optionalOptions?: BulkWriteOptions +): void { + bulkOperation.execute(optionalOptions, callback); +} + +expectType( + extendedCallbackBasedBulkExecute((error, bulkWriteResult) => { + expectType(error); + expectType(bulkWriteResult); + }) +);