(options: BulkWriteOptions = {})
| 1147 | } |
| 1148 | |
| 1149 | async execute(options: BulkWriteOptions = {}): Promise<BulkWriteResult> { |
| 1150 | if (this.s.executed) { |
| 1151 | throw new MongoBatchReExecutionError(); |
| 1152 | } |
| 1153 | |
| 1154 | const writeConcern = WriteConcern.fromOptions(options); |
| 1155 | if (writeConcern) { |
| 1156 | this.s.writeConcern = writeConcern; |
| 1157 | } |
| 1158 | |
| 1159 | // If we have current batch |
| 1160 | if (this.isOrdered) { |
| 1161 | if (this.s.currentBatch) this.s.batches.push(this.s.currentBatch); |
| 1162 | } else { |
| 1163 | if (this.s.currentInsertBatch) this.s.batches.push(this.s.currentInsertBatch); |
| 1164 | if (this.s.currentUpdateBatch) this.s.batches.push(this.s.currentUpdateBatch); |
| 1165 | if (this.s.currentRemoveBatch) this.s.batches.push(this.s.currentRemoveBatch); |
| 1166 | } |
| 1167 | // If we have no operations in the bulk raise an error |
| 1168 | if (this.s.batches.length === 0) { |
| 1169 | throw new MongoInvalidArgumentError('Invalid BulkOperation, Batch cannot be empty'); |
| 1170 | } |
| 1171 | |
| 1172 | this.s.executed = true; |
| 1173 | const finalOptions = resolveOptions(this.collection, { ...this.s.options, ...options }); |
| 1174 | |
| 1175 | // if there is no timeoutContext provided, create a timeoutContext and use it for |
| 1176 | // all batches in the bulk operation |
| 1177 | finalOptions.timeoutContext ??= TimeoutContext.create({ |
| 1178 | session: finalOptions.session, |
| 1179 | timeoutMS: finalOptions.timeoutMS, |
| 1180 | serverSelectionTimeoutMS: this.collection.client.s.options.serverSelectionTimeoutMS, |
| 1181 | waitQueueTimeoutMS: this.collection.client.s.options.waitQueueTimeoutMS |
| 1182 | }); |
| 1183 | |
| 1184 | if (finalOptions.session == null) { |
| 1185 | // if there is not an explicit session provided to `execute()`, create |
| 1186 | // an implicit session and use that for all batches in the bulk operation |
| 1187 | return await this.collection.client.withSession({ explicit: false }, async session => { |
| 1188 | return await executeCommands(this, { ...finalOptions, session }); |
| 1189 | }); |
| 1190 | } |
| 1191 | |
| 1192 | return await executeCommands(this, { ...finalOptions }); |
| 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * Handles the write error before executing commands |
nothing calls this directly
no test coverage detected