From 5c4d3811b498d50fde5e949a4ec5ed5f8868b314 Mon Sep 17 00:00:00 2001 From: skrtheboss Date: Thu, 28 Oct 2021 10:08:32 +0200 Subject: [PATCH 1/3] fix: add overloads for BulkOperationBase's execute function This adds overloads to the execute function, to ensure void is returned if a callback is provided, and a promise is returned if no callback is specified. --- src/bulk/common.ts | 10 ++- .../bulk/bulk-operation-base.test-d.ts | 81 +++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 test/types/community/bulk/bulk-operation-base.test-d.ts diff --git a/src/bulk/common.ts b/src/bulk/common.ts index 077fd698f38..d25fe097533 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 00000000000..71d571c7449 --- /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 create collection 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 create collection 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); + }) +); From 147ef5af4c70dbe4c94c649596896381f7ad4a79 Mon Sep 17 00:00:00 2001 From: Daria Pardue Date: Tue, 2 Nov 2021 16:05:17 -0400 Subject: [PATCH 2/3] Update test/types/community/bulk/bulk-operation-base.test-d.ts --- test/types/community/bulk/bulk-operation-base.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/community/bulk/bulk-operation-base.test-d.ts b/test/types/community/bulk/bulk-operation-base.test-d.ts index 71d571c7449..f0e3348959e 100644 --- a/test/types/community/bulk/bulk-operation-base.test-d.ts +++ b/test/types/community/bulk/bulk-operation-base.test-d.ts @@ -42,7 +42,7 @@ expectType>(bulkOperation.execute()); expectType>(bulkOperation.execute(options)); -// ensure we can use the create collection in a callback based wrapper function +// ensure we can use the bulk operation execute in a callback based wrapper function function extendedPromiseBasedBulkExecute( optionalOptions?: BulkWriteOptions ): Promise { From 1706932c5d63be34114d3ee6a19cf2ab47f9ca1e Mon Sep 17 00:00:00 2001 From: Daria Pardue Date: Tue, 2 Nov 2021 16:05:23 -0400 Subject: [PATCH 3/3] Update test/types/community/bulk/bulk-operation-base.test-d.ts --- test/types/community/bulk/bulk-operation-base.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/community/bulk/bulk-operation-base.test-d.ts b/test/types/community/bulk/bulk-operation-base.test-d.ts index f0e3348959e..45e7c44aaf2 100644 --- a/test/types/community/bulk/bulk-operation-base.test-d.ts +++ b/test/types/community/bulk/bulk-operation-base.test-d.ts @@ -65,7 +65,7 @@ expectType( }) ); -// ensure we can use the create collection in a callback based wrapper function +// ensure we can use the bulk operation execute in a callback based wrapper function function extendedCallbackBasedBulkExecute( callback: Callback, optionalOptions?: BulkWriteOptions