find_and_modify.js 6.1 KB

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