find_and_modify.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FindOneAndUpdateOperation = exports.FindOneAndReplaceOperation = exports.FindOneAndDeleteOperation = exports.FindAndModifyOperation = exports.ReturnDocument = void 0;
  4. const responses_1 = require("../cmap/wire_protocol/responses");
  5. const error_1 = require("../error");
  6. const read_preference_1 = require("../read_preference");
  7. const sort_1 = require("../sort");
  8. const utils_1 = require("../utils");
  9. const command_1 = require("./command");
  10. const operation_1 = require("./operation");
  11. /** @public */
  12. exports.ReturnDocument = Object.freeze({
  13. BEFORE: 'before',
  14. AFTER: 'after'
  15. });
  16. function configureFindAndModifyCmdBaseUpdateOpts(cmdBase, options) {
  17. cmdBase.new = options.returnDocument === exports.ReturnDocument.AFTER;
  18. cmdBase.upsert = options.upsert === true;
  19. if (options.bypassDocumentValidation === true) {
  20. cmdBase.bypassDocumentValidation = options.bypassDocumentValidation;
  21. }
  22. return cmdBase;
  23. }
  24. /** @internal */
  25. class FindAndModifyOperation extends command_1.CommandOperation {
  26. constructor(collection, query, options) {
  27. super(collection, options);
  28. this.SERVER_COMMAND_RESPONSE_TYPE = responses_1.MongoDBResponse;
  29. this.options = options;
  30. // force primary read preference
  31. this.readPreference = read_preference_1.ReadPreference.primary;
  32. this.collection = collection;
  33. this.query = query;
  34. }
  35. get commandName() {
  36. return 'findAndModify';
  37. }
  38. buildCommandDocument(connection, _session) {
  39. const options = this.options;
  40. const command = {
  41. findAndModify: this.collection.collectionName,
  42. query: this.query,
  43. remove: false,
  44. new: false,
  45. upsert: false
  46. };
  47. options.includeResultMetadata ??= false;
  48. const sort = (0, sort_1.formatSort)(options.sort);
  49. if (sort) {
  50. command.sort = sort;
  51. }
  52. if (options.projection) {
  53. command.fields = options.projection;
  54. }
  55. if (options.maxTimeMS) {
  56. command.maxTimeMS = options.maxTimeMS;
  57. }
  58. // Decorate the findAndModify command with the write Concern
  59. if (options.writeConcern) {
  60. command.writeConcern = options.writeConcern;
  61. }
  62. if (options.let) {
  63. command.let = options.let;
  64. }
  65. // we check for undefined specifically here to allow falsy values
  66. // eslint-disable-next-line no-restricted-syntax
  67. if (options.comment !== undefined) {
  68. command.comment = options.comment;
  69. }
  70. (0, utils_1.decorateWithCollation)(command, options);
  71. if (options.hint) {
  72. const unacknowledgedWrite = this.writeConcern?.w === 0;
  73. if (unacknowledgedWrite && (0, utils_1.maxWireVersion)(connection) < 9) {
  74. throw new error_1.MongoCompatibilityError('hint for the findAndModify command is only supported on MongoDB 4.4+');
  75. }
  76. command.hint = options.hint;
  77. }
  78. return command;
  79. }
  80. handleOk(response) {
  81. const result = super.handleOk(response);
  82. return this.options.includeResultMetadata ? result : (result.value ?? null);
  83. }
  84. }
  85. exports.FindAndModifyOperation = FindAndModifyOperation;
  86. /** @internal */
  87. class FindOneAndDeleteOperation extends FindAndModifyOperation {
  88. constructor(collection, filter, options) {
  89. // Basic validation
  90. if (filter == null || typeof filter !== 'object') {
  91. throw new error_1.MongoInvalidArgumentError('Argument "filter" must be an object');
  92. }
  93. super(collection, filter, options);
  94. }
  95. buildCommandDocument(connection, session) {
  96. const document = super.buildCommandDocument(connection, session);
  97. document.remove = true;
  98. return document;
  99. }
  100. }
  101. exports.FindOneAndDeleteOperation = FindOneAndDeleteOperation;
  102. /** @internal */
  103. class FindOneAndReplaceOperation extends FindAndModifyOperation {
  104. constructor(collection, filter, replacement, options) {
  105. if (filter == null || typeof filter !== 'object') {
  106. throw new error_1.MongoInvalidArgumentError('Argument "filter" must be an object');
  107. }
  108. if (replacement == null || typeof replacement !== 'object') {
  109. throw new error_1.MongoInvalidArgumentError('Argument "replacement" must be an object');
  110. }
  111. if ((0, utils_1.hasAtomicOperators)(replacement)) {
  112. throw new error_1.MongoInvalidArgumentError('Replacement document must not contain atomic operators');
  113. }
  114. super(collection, filter, options);
  115. this.replacement = replacement;
  116. }
  117. buildCommandDocument(connection, session) {
  118. const document = super.buildCommandDocument(connection, session);
  119. document.update = this.replacement;
  120. configureFindAndModifyCmdBaseUpdateOpts(document, this.options);
  121. return document;
  122. }
  123. }
  124. exports.FindOneAndReplaceOperation = FindOneAndReplaceOperation;
  125. /** @internal */
  126. class FindOneAndUpdateOperation extends FindAndModifyOperation {
  127. constructor(collection, filter, update, options) {
  128. if (filter == null || typeof filter !== 'object') {
  129. throw new error_1.MongoInvalidArgumentError('Argument "filter" must be an object');
  130. }
  131. if (update == null || typeof update !== 'object') {
  132. throw new error_1.MongoInvalidArgumentError('Argument "update" must be an object');
  133. }
  134. if (!(0, utils_1.hasAtomicOperators)(update, options)) {
  135. throw new error_1.MongoInvalidArgumentError('Update document requires atomic operators');
  136. }
  137. super(collection, filter, options);
  138. this.update = update;
  139. this.options = options;
  140. }
  141. buildCommandDocument(connection, session) {
  142. const document = super.buildCommandDocument(connection, session);
  143. document.update = this.update;
  144. configureFindAndModifyCmdBaseUpdateOpts(document, this.options);
  145. if (this.options.arrayFilters) {
  146. document.arrayFilters = this.options.arrayFilters;
  147. }
  148. return document;
  149. }
  150. }
  151. exports.FindOneAndUpdateOperation = FindOneAndUpdateOperation;
  152. (0, operation_1.defineAspects)(FindAndModifyOperation, [
  153. operation_1.Aspect.WRITE_OPERATION,
  154. operation_1.Aspect.RETRYABLE,
  155. operation_1.Aspect.EXPLAINABLE,
  156. operation_1.Aspect.SUPPORTS_RAW_DATA
  157. ]);
  158. //# sourceMappingURL=find_and_modify.js.map