Skip to content

Commit e75c6a0

Browse files
remove bulk write operation
1 parent 175d791 commit e75c6a0

File tree

4 files changed

+31
-95
lines changed

4 files changed

+31
-95
lines changed

src/collection.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { type BSONSerializeOptions, type Document, resolveBSONOptions } from './bson';
2-
import type { AnyBulkWriteOperation, BulkWriteOptions, BulkWriteResult } from './bulk/common';
2+
import type {
3+
AnyBulkWriteOperation,
4+
BulkOperationBase,
5+
BulkWriteOptions,
6+
BulkWriteResult
7+
} from './bulk/common';
38
import { OrderedBulkOperation } from './bulk/ordered';
49
import { UnorderedBulkOperation } from './bulk/unordered';
510
import { ChangeStream, type ChangeStreamDocument, type ChangeStreamOptions } from './change_stream';
@@ -24,7 +29,6 @@ import type {
2429
WithoutId
2530
} from './mongo_types';
2631
import type { AggregateOptions } from './operations/aggregate';
27-
import { BulkWriteOperation } from './operations/bulk_write';
2832
import { CountOperation, type CountOptions } from './operations/count';
2933
import {
3034
DeleteManyOperation,
@@ -307,20 +311,17 @@ export class Collection<TSchema extends Document = Document> {
307311
if (!Array.isArray(docs)) {
308312
throw new MongoInvalidArgumentError('Argument "docs" must be an array of documents');
309313
}
314+
options = options ?? {};
310315

311-
const writeConcern = WriteConcern.fromOptions(options);
312-
const bulkWriteOperation = new BulkWriteOperation(
313-
this as unknown as Collection<Document>,
314-
docs.map(document => ({
315-
insertOne: { document }
316-
})),
317-
options ?? {}
318-
);
316+
const acknowledged = WriteConcern.fromOptions(options)?.w !== 0;
319317

320318
try {
321-
const res = await executeOperation(this.client, bulkWriteOperation);
319+
const res = await this.bulkWrite(
320+
docs.map(doc => ({ insertOne: { document: doc } })),
321+
options
322+
);
322323
return {
323-
acknowledged: writeConcern?.w !== 0,
324+
acknowledged,
324325
insertedCount: res.insertedCount,
325326
insertedIds: res.insertedIds
326327
};
@@ -361,14 +362,21 @@ export class Collection<TSchema extends Document = Document> {
361362
throw new MongoInvalidArgumentError('Argument "operations" must be an array of documents');
362363
}
363364

364-
return await executeOperation(
365-
this.client,
366-
new BulkWriteOperation(
367-
this as TODO_NODE_3286,
368-
operations,
369-
resolveOptions(this, options ?? { ordered: true })
370-
)
371-
);
365+
options = options ?? {};
366+
367+
// Create the bulk operation
368+
const bulk: BulkOperationBase =
369+
options.ordered === false
370+
? this.initializeUnorderedBulkOp(options)
371+
: this.initializeOrderedBulkOp(options);
372+
373+
// for each op go through and add to the bulk
374+
for (let i = 0; i < operations.length; i++) {
375+
bulk.raw(operations[i]);
376+
}
377+
378+
// Execute the bulk
379+
return await bulk.execute({ ...options });
372380
}
373381

374382
/**

src/operations/bulk_write.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/operations/insert.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import type { Document } from '../bson';
22
import type { BulkWriteOptions } from '../bulk/common';
33
import type { Collection } from '../collection';
4-
import { MongoInvalidArgumentError, MongoServerError } from '../error';
4+
import { MongoServerError } from '../error';
55
import type { InferIdType } from '../mongo_types';
66
import type { Server } from '../sdam/server';
77
import type { ClientSession } from '../sessions';
88
import { type TimeoutContext } from '../timeout';
99
import { maybeAddIdToDocuments, type MongoDBNamespace } from '../utils';
10-
import { WriteConcern } from '../write_concern';
11-
import { BulkWriteOperation } from './bulk_write';
1210
import { CommandOperation, type CommandOperationOptions } from './command';
13-
import { AbstractOperation, Aspect, defineAspects } from './operation';
11+
import { Aspect, defineAspects } from './operation';
1412

1513
/** @internal */
1614
export class InsertOperation extends CommandOperation<Document> {
@@ -106,4 +104,4 @@ export interface InsertManyResult<TSchema = Document> {
106104
}
107105

108106
defineAspects(InsertOperation, [Aspect.RETRYABLE, Aspect.WRITE_OPERATION]);
109-
defineAspects(InsertOneOperation, [Aspect.RETRYABLE, Aspect.WRITE_OPERATION]);
107+
defineAspects(InsertOneOperation, [Aspect.RETRYABLE, Aspect.WRITE_OPERATION]);

test/integration/crud/abstract_operation.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ describe('abstract operation', function () {
3737
subclassType: mongodb.AggregateOperation,
3838
correctCommandName: 'aggregate'
3939
},
40-
{
41-
subclassCreator: () =>
42-
new mongodb.BulkWriteOperation(collection, [{ insertOne: { document: { a: 1 } } }], {}),
43-
subclassType: mongodb.BulkWriteOperation,
44-
correctCommandName: 'bulkWrite'
45-
},
4640
{
4741
subclassCreator: () => new mongodb.CollectionsOperation(db, {}),
4842
subclassType: mongodb.CollectionsOperation,

0 commit comments

Comments
 (0)