|
1 | 1 | import type { Document } from '../bson'; |
2 | 2 | import type { BulkWriteOptions } from '../bulk/common'; |
3 | 3 | import type { Collection } from '../collection'; |
4 | | -import { MongoInvalidArgumentError, MongoServerError } from '../error'; |
| 4 | +import { MongoServerError } from '../error'; |
5 | 5 | import type { InferIdType } from '../mongo_types'; |
6 | 6 | import type { Server } from '../sdam/server'; |
7 | 7 | import type { ClientSession } from '../sessions'; |
8 | 8 | import { type TimeoutContext } from '../timeout'; |
9 | 9 | import { maybeAddIdToDocuments, type MongoDBNamespace } from '../utils'; |
10 | | -import { WriteConcern } from '../write_concern'; |
11 | | -import { BulkWriteOperation } from './bulk_write'; |
12 | 10 | import { CommandOperation, type CommandOperationOptions } from './command'; |
13 | | -import { AbstractOperation, Aspect, defineAspects } from './operation'; |
| 11 | +import { Aspect, defineAspects } from './operation'; |
14 | 12 |
|
15 | 13 | /** @internal */ |
16 | 14 | export class InsertOperation extends CommandOperation<Document> { |
@@ -73,7 +71,7 @@ export interface InsertOneResult<TSchema = Document> { |
73 | 71 |
|
74 | 72 | export class InsertOneOperation extends InsertOperation { |
75 | 73 | constructor(collection: Collection, doc: Document, options: InsertOneOptions) { |
76 | | - super(collection.s.namespace, maybeAddIdToDocuments(collection, [doc], options), options); |
| 74 | + super(collection.s.namespace, [maybeAddIdToDocuments(collection, doc, options)], options); |
77 | 75 | } |
78 | 76 |
|
79 | 77 | override async execute( |
@@ -105,62 +103,5 @@ export interface InsertManyResult<TSchema = Document> { |
105 | 103 | insertedIds: { [key: number]: InferIdType<TSchema> }; |
106 | 104 | } |
107 | 105 |
|
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 | | - |
164 | 106 | defineAspects(InsertOperation, [Aspect.RETRYABLE, Aspect.WRITE_OPERATION]); |
165 | 107 | defineAspects(InsertOneOperation, [Aspect.RETRYABLE, Aspect.WRITE_OPERATION]); |
166 | | -defineAspects(InsertManyOperation, [Aspect.WRITE_OPERATION]); |
0 commit comments