bulk_write.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.BulkWriteOperation = void 0;
  4. const operation_1 = require("./operation");
  5. /** @internal */
  6. class BulkWriteOperation extends operation_1.AbstractOperation {
  7. constructor(collection, operations, options) {
  8. super(options);
  9. this.options = options;
  10. this.collection = collection;
  11. this.operations = operations;
  12. }
  13. execute(server, session, callback) {
  14. const coll = this.collection;
  15. const operations = this.operations;
  16. const options = { ...this.options, ...this.bsonOptions, readPreference: this.readPreference };
  17. // Create the bulk operation
  18. const bulk = options.ordered === false
  19. ? coll.initializeUnorderedBulkOp(options)
  20. : coll.initializeOrderedBulkOp(options);
  21. // for each op go through and add to the bulk
  22. try {
  23. for (let i = 0; i < operations.length; i++) {
  24. bulk.raw(operations[i]);
  25. }
  26. }
  27. catch (err) {
  28. return callback(err);
  29. }
  30. // Execute the bulk
  31. bulk.execute({ ...options, session }, (err, r) => {
  32. // We have connection level error
  33. if (!r && err) {
  34. return callback(err);
  35. }
  36. // Return the results
  37. callback(undefined, r);
  38. });
  39. }
  40. }
  41. exports.BulkWriteOperation = BulkWriteOperation;
  42. (0, operation_1.defineAspects)(BulkWriteOperation, [operation_1.Aspect.WRITE_OPERATION]);
  43. //# sourceMappingURL=bulk_write.js.map