insert.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.InsertOneOperation = exports.InsertOperation = void 0;
  4. const responses_1 = require("../cmap/wire_protocol/responses");
  5. const error_1 = require("../error");
  6. const utils_1 = require("../utils");
  7. const command_1 = require("./command");
  8. const operation_1 = require("./operation");
  9. /** @internal */
  10. class InsertOperation extends command_1.CommandOperation {
  11. constructor(ns, documents, options) {
  12. super(undefined, options);
  13. this.SERVER_COMMAND_RESPONSE_TYPE = responses_1.MongoDBResponse;
  14. this.options = { ...options, checkKeys: options.checkKeys ?? false };
  15. this.ns = ns;
  16. this.documents = documents;
  17. }
  18. get commandName() {
  19. return 'insert';
  20. }
  21. buildCommandDocument(_connection, _session) {
  22. const options = this.options ?? {};
  23. const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
  24. const command = {
  25. insert: this.ns.collection,
  26. documents: this.documents,
  27. ordered
  28. };
  29. if (typeof options.bypassDocumentValidation === 'boolean') {
  30. command.bypassDocumentValidation = options.bypassDocumentValidation;
  31. }
  32. // we check for undefined specifically here to allow falsy values
  33. // eslint-disable-next-line no-restricted-syntax
  34. if (options.comment !== undefined) {
  35. command.comment = options.comment;
  36. }
  37. return command;
  38. }
  39. }
  40. exports.InsertOperation = InsertOperation;
  41. class InsertOneOperation extends InsertOperation {
  42. constructor(collection, doc, options) {
  43. super(collection.s.namespace, [(0, utils_1.maybeAddIdToDocuments)(collection, doc, options)], options);
  44. }
  45. handleOk(response) {
  46. const res = super.handleOk(response);
  47. if (res.code)
  48. throw new error_1.MongoServerError(res);
  49. if (res.writeErrors) {
  50. // This should be a WriteError but we can't change it now because of error hierarchy
  51. throw new error_1.MongoServerError(res.writeErrors[0]);
  52. }
  53. return {
  54. acknowledged: this.writeConcern?.w !== 0,
  55. insertedId: this.documents[0]._id
  56. };
  57. }
  58. }
  59. exports.InsertOneOperation = InsertOneOperation;
  60. (0, operation_1.defineAspects)(InsertOperation, [
  61. operation_1.Aspect.RETRYABLE,
  62. operation_1.Aspect.WRITE_OPERATION,
  63. operation_1.Aspect.SUPPORTS_RAW_DATA
  64. ]);
  65. (0, operation_1.defineAspects)(InsertOneOperation, [
  66. operation_1.Aspect.RETRYABLE,
  67. operation_1.Aspect.WRITE_OPERATION,
  68. operation_1.Aspect.SUPPORTS_RAW_DATA
  69. ]);
  70. //# sourceMappingURL=insert.js.map