Skip to content

Commit 175d791

Browse files
remove insertmany operation
1 parent 2ef6c10 commit 175d791

File tree

3 files changed

+28
-71
lines changed

3 files changed

+28
-71
lines changed

src/collection.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ import {
6161
type ListIndexesOptions
6262
} from './operations/indexes';
6363
import {
64-
InsertManyOperation,
6564
type InsertManyResult,
6665
InsertOneOperation,
6766
type InsertOneOptions,
@@ -305,14 +304,34 @@ export class Collection<TSchema extends Document = Document> {
305304
docs: ReadonlyArray<OptionalUnlessRequiredId<TSchema>>,
306305
options?: BulkWriteOptions
307306
): Promise<InsertManyResult<TSchema>> {
308-
return await executeOperation(
309-
this.client,
310-
new InsertManyOperation(
311-
this as TODO_NODE_3286,
312-
docs,
313-
resolveOptions(this, options ?? { ordered: true })
314-
) as TODO_NODE_3286
307+
if (!Array.isArray(docs)) {
308+
throw new MongoInvalidArgumentError('Argument "docs" must be an array of documents');
309+
}
310+
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 ?? {}
315318
);
319+
320+
try {
321+
const res = await executeOperation(this.client, bulkWriteOperation);
322+
return {
323+
acknowledged: writeConcern?.w !== 0,
324+
insertedCount: res.insertedCount,
325+
insertedIds: res.insertedIds
326+
};
327+
} catch (err) {
328+
if (err && err.message === 'Operation must be an object with an operation key') {
329+
throw new MongoInvalidArgumentError(
330+
'Collection.insertMany() cannot be called with an array that has null/undefined values'
331+
);
332+
}
333+
throw err;
334+
}
316335
}
317336

318337
/**

src/operations/insert.ts

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -105,62 +105,5 @@ export interface InsertManyResult<TSchema = Document> {
105105
insertedIds: { [key: number]: InferIdType<TSchema> };
106106
}
107107

108-
/** @internal */
109-
export class InsertManyOperation extends AbstractOperation<InsertManyResult> {
110-
override options: BulkWriteOptions;
111-
collection: Collection;
112-
docs: ReadonlyArray<Document>;
113-
114-
constructor(collection: Collection, docs: ReadonlyArray<Document>, options: BulkWriteOptions) {
115-
super(options);
116-
117-
if (!Array.isArray(docs)) {
118-
throw new MongoInvalidArgumentError('Argument "docs" must be an array of documents');
119-
}
120-
121-
this.options = options;
122-
this.collection = collection;
123-
this.docs = docs;
124-
}
125-
126-
override get commandName() {
127-
return 'insert' as const;
128-
}
129-
130-
override async execute(
131-
server: Server,
132-
session: ClientSession | undefined,
133-
timeoutContext: TimeoutContext
134-
): Promise<InsertManyResult> {
135-
const coll = this.collection;
136-
const options = { ...this.options, ...this.bsonOptions, readPreference: this.readPreference };
137-
const writeConcern = WriteConcern.fromOptions(options);
138-
const bulkWriteOperation = new BulkWriteOperation(
139-
coll,
140-
this.docs.map(document => ({
141-
insertOne: { document }
142-
})),
143-
options
144-
);
145-
146-
try {
147-
const res = await bulkWriteOperation.execute(server, session, timeoutContext);
148-
return {
149-
acknowledged: writeConcern?.w !== 0,
150-
insertedCount: res.insertedCount,
151-
insertedIds: res.insertedIds
152-
};
153-
} catch (err) {
154-
if (err && err.message === 'Operation must be an object with an operation key') {
155-
throw new MongoInvalidArgumentError(
156-
'Collection.insertMany() cannot be called with an array that has null/undefined values'
157-
);
158-
}
159-
throw err;
160-
}
161-
}
162-
}
163-
164108
defineAspects(InsertOperation, [Aspect.RETRYABLE, Aspect.WRITE_OPERATION]);
165-
defineAspects(InsertOneOperation, [Aspect.RETRYABLE, Aspect.WRITE_OPERATION]);
166-
defineAspects(InsertManyOperation, [Aspect.WRITE_OPERATION]);
109+
defineAspects(InsertOneOperation, [Aspect.RETRYABLE, Aspect.WRITE_OPERATION]);

test/integration/crud/abstract_operation.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,6 @@ describe('abstract operation', function () {
161161
subclassType: mongodb.InsertOneOperation,
162162
correctCommandName: 'insert'
163163
},
164-
{
165-
subclassCreator: () => new mongodb.InsertManyOperation(collection, [{ a: 1 }], {}),
166-
subclassType: mongodb.InsertManyOperation,
167-
correctCommandName: 'insert'
168-
},
169164
{
170165
subclassCreator: () => new mongodb.IsCappedOperation(collection, {}),
171166
subclassType: mongodb.IsCappedOperation,

0 commit comments

Comments
 (0)