bulk_write.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  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.AbstractCallbackOperation {
  7. constructor(collection, operations, options) {
  8. super(options);
  9. this.options = options;
  10. this.collection = collection;
  11. this.operations = operations;
  12. }
  13. executeCallback(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 }).then(result => callback(undefined, result), error => callback(error));
  32. }
  33. }
  34. exports.BulkWriteOperation = BulkWriteOperation;
  35. (0, operation_1.defineAspects)(BulkWriteOperation, [operation_1.Aspect.WRITE_OPERATION]);
  36. //# sourceMappingURL=bulk_write.js.map