find_and_modify.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.CommandOperation {
  25. constructor(collection, query, options) {
  26. super(collection, options);
  27. this.options = options !== null && options !== void 0 ? options : {};
  28. this.cmdBase = {
  29. remove: false,
  30. new: false,
  31. upsert: false
  32. };
  33. const sort = (0, sort_1.formatSort)(options.sort);
  34. if (sort) {
  35. this.cmdBase.sort = sort;
  36. }
  37. if (options.projection) {
  38. this.cmdBase.fields = options.projection;
  39. }
  40. if (options.maxTimeMS) {
  41. this.cmdBase.maxTimeMS = options.maxTimeMS;
  42. }
  43. // Decorate the findAndModify command with the write Concern
  44. if (options.writeConcern) {
  45. this.cmdBase.writeConcern = options.writeConcern;
  46. }
  47. if (options.let) {
  48. this.cmdBase.let = options.let;
  49. }
  50. // we check for undefined specifically here to allow falsy values
  51. // eslint-disable-next-line no-restricted-syntax
  52. if (options.comment !== undefined) {
  53. this.cmdBase.comment = options.comment;
  54. }
  55. // force primary read preference
  56. this.readPreference = read_preference_1.ReadPreference.primary;
  57. this.collection = collection;
  58. this.query = query;
  59. }
  60. execute(server, session, callback) {
  61. var _a;
  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 = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.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. if (this.explain && (0, utils_1.maxWireVersion)(server) < 4) {
  89. callback(new error_1.MongoCompatibilityError(`Server ${server.name} does not support explain on findAndModify`));
  90. return;
  91. }
  92. // Execute the command
  93. super.executeCommand(server, session, cmd, (err, result) => {
  94. if (err)
  95. return callback(err);
  96. return callback(undefined, result);
  97. });
  98. }
  99. }
  100. /** @internal */
  101. class FindOneAndDeleteOperation extends FindAndModifyOperation {
  102. constructor(collection, filter, options) {
  103. // Basic validation
  104. if (filter == null || typeof filter !== 'object') {
  105. throw new error_1.MongoInvalidArgumentError('Argument "filter" must be an object');
  106. }
  107. super(collection, filter, options);
  108. this.cmdBase.remove = true;
  109. }
  110. }
  111. exports.FindOneAndDeleteOperation = FindOneAndDeleteOperation;
  112. /** @internal */
  113. class FindOneAndReplaceOperation extends FindAndModifyOperation {
  114. constructor(collection, filter, replacement, options) {
  115. if (filter == null || typeof filter !== 'object') {
  116. throw new error_1.MongoInvalidArgumentError('Argument "filter" must be an object');
  117. }
  118. if (replacement == null || typeof replacement !== 'object') {
  119. throw new error_1.MongoInvalidArgumentError('Argument "replacement" must be an object');
  120. }
  121. if ((0, utils_1.hasAtomicOperators)(replacement)) {
  122. throw new error_1.MongoInvalidArgumentError('Replacement document must not contain atomic operators');
  123. }
  124. super(collection, filter, options);
  125. this.cmdBase.update = replacement;
  126. configureFindAndModifyCmdBaseUpdateOpts(this.cmdBase, options);
  127. }
  128. }
  129. exports.FindOneAndReplaceOperation = FindOneAndReplaceOperation;
  130. /** @internal */
  131. class FindOneAndUpdateOperation extends FindAndModifyOperation {
  132. constructor(collection, filter, update, options) {
  133. if (filter == null || typeof filter !== 'object') {
  134. throw new error_1.MongoInvalidArgumentError('Argument "filter" must be an object');
  135. }
  136. if (update == null || typeof update !== 'object') {
  137. throw new error_1.MongoInvalidArgumentError('Argument "update" must be an object');
  138. }
  139. if (!(0, utils_1.hasAtomicOperators)(update)) {
  140. throw new error_1.MongoInvalidArgumentError('Update document requires atomic operators');
  141. }
  142. super(collection, filter, options);
  143. this.cmdBase.update = update;
  144. configureFindAndModifyCmdBaseUpdateOpts(this.cmdBase, options);
  145. if (options.arrayFilters) {
  146. this.cmdBase.arrayFilters = options.arrayFilters;
  147. }
  148. }
  149. }
  150. exports.FindOneAndUpdateOperation = FindOneAndUpdateOperation;
  151. (0, operation_1.defineAspects)(FindAndModifyOperation, [
  152. operation_1.Aspect.WRITE_OPERATION,
  153. operation_1.Aspect.RETRYABLE,
  154. operation_1.Aspect.EXPLAINABLE
  155. ]);
  156. //# sourceMappingURL=find_and_modify.js.map