|
| 1 | +import { expectType } from 'tsd'; |
| 2 | + |
| 3 | +import { |
| 4 | + BulkWriteResult, |
| 5 | + MongoClient, |
| 6 | + BatchType, |
| 7 | + UpdateStatement, |
| 8 | + DeleteStatement, |
| 9 | + AnyError, |
| 10 | + BulkWriteOptions, |
| 11 | + Callback, |
| 12 | + Document |
| 13 | +} from '../../../../src/index'; |
| 14 | +import { BulkOperationBase, Batch } from '../../../../src/bulk/common'; |
| 15 | + |
| 16 | +const client = new MongoClient(''); |
| 17 | +const db = client.db('test'); |
| 18 | +const collection = db.collection('test'); |
| 19 | + |
| 20 | +class TestBulkOperation extends BulkOperationBase { |
| 21 | + constructor() { |
| 22 | + super(collection, {}, true); |
| 23 | + } |
| 24 | + |
| 25 | + addToOperationsList( |
| 26 | + batchType: BatchType, |
| 27 | + document: Document | UpdateStatement | DeleteStatement |
| 28 | + ): this { |
| 29 | + this.s.currentBatch = new Batch<Document>(batchType, 0); |
| 30 | + this.s.currentBatch.operations.push(document); |
| 31 | + return this; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +const bulkOperation = new TestBulkOperation(); |
| 36 | + |
| 37 | +// execute |
| 38 | + |
| 39 | +const options: BulkWriteOptions = {}; |
| 40 | + |
| 41 | +expectType<Promise<BulkWriteResult>>(bulkOperation.execute()); |
| 42 | + |
| 43 | +expectType<Promise<BulkWriteResult>>(bulkOperation.execute(options)); |
| 44 | + |
| 45 | +// ensure we can use the bulk operation execute in a callback based wrapper function |
| 46 | +function extendedPromiseBasedBulkExecute( |
| 47 | + optionalOptions?: BulkWriteOptions |
| 48 | +): Promise<BulkWriteResult> { |
| 49 | + return bulkOperation.execute(optionalOptions); |
| 50 | +} |
| 51 | + |
| 52 | +expectType<Promise<BulkWriteResult>>(extendedPromiseBasedBulkExecute()); |
| 53 | + |
| 54 | +expectType<void>( |
| 55 | + bulkOperation.execute((error, bulkWriteResult) => { |
| 56 | + expectType<AnyError | undefined>(error); |
| 57 | + expectType<BulkWriteResult | undefined>(bulkWriteResult); |
| 58 | + }) |
| 59 | +); |
| 60 | + |
| 61 | +expectType<void>( |
| 62 | + bulkOperation.execute(options, (error, bulkWriteResult) => { |
| 63 | + expectType<AnyError | undefined>(error); |
| 64 | + expectType<BulkWriteResult | undefined>(bulkWriteResult); |
| 65 | + }) |
| 66 | +); |
| 67 | + |
| 68 | +// ensure we can use the bulk operation execute in a callback based wrapper function |
| 69 | +function extendedCallbackBasedBulkExecute( |
| 70 | + callback: Callback<BulkWriteResult>, |
| 71 | + optionalOptions?: BulkWriteOptions |
| 72 | +): void { |
| 73 | + bulkOperation.execute(optionalOptions, callback); |
| 74 | +} |
| 75 | + |
| 76 | +expectType<void>( |
| 77 | + extendedCallbackBasedBulkExecute((error, bulkWriteResult) => { |
| 78 | + expectType<AnyError | undefined>(error); |
| 79 | + expectType<BulkWriteResult | undefined>(bulkWriteResult); |
| 80 | + }) |
| 81 | +); |
0 commit comments