find_and_modify.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // force primary read preference
  51. this.readPreference = read_preference_1.ReadPreference.primary;
  52. this.collection = collection;
  53. this.query = query;
  54. }
  55. execute(server, session, callback) {
  56. var _a;
  57. const coll = this.collection;
  58. const query = this.query;
  59. const options = { ...this.options, ...this.bsonOptions };
  60. // Create findAndModify command object
  61. const cmd = {
  62. findAndModify: coll.collectionName,
  63. query: query,
  64. ...this.cmdBase
  65. };
  66. // Have we specified collation
  67. try {
  68. (0, utils_1.decorateWithCollation)(cmd, coll, options);
  69. }
  70. catch (err) {
  71. return callback(err);
  72. }
  73. if (options.hint) {
  74. // TODO: once this method becomes a CommandOperation we will have the server
  75. // in place to check.
  76. const unacknowledgedWrite = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.w) === 0;
  77. if (unacknowledgedWrite || (0, utils_1.maxWireVersion)(server) < 8) {
  78. callback(new error_1.MongoCompatibilityError('The current topology does not support a hint on findAndModify commands'));
  79. return;
  80. }
  81. cmd.hint = options.hint;
  82. }
  83. if (this.explain && (0, utils_1.maxWireVersion)(server) < 4) {
  84. callback(new error_1.MongoCompatibilityError(`Server ${server.name} does not support explain on findAndModify`));
  85. return;
  86. }
  87. // Execute the command
  88. super.executeCommand(server, session, cmd, (err, result) => {
  89. if (err)
  90. return callback(err);
  91. return callback(undefined, result);
  92. });
  93. }
  94. }
  95. /** @internal */
  96. class FindOneAndDeleteOperation extends FindAndModifyOperation {
  97. constructor(collection, filter, options) {
  98. // Basic validation
  99. if (filter == null || typeof filter !== 'object') {
  100. throw new error_1.MongoInvalidArgumentError('Argument "filter" must be an object');
  101. }
  102. super(collection, filter, options);
  103. this.cmdBase.remove = true;
  104. }
  105. }
  106. exports.FindOneAndDeleteOperation = FindOneAndDeleteOperation;
  107. /** @internal */
  108. class FindOneAndReplaceOperation extends FindAndModifyOperation {
  109. constructor(collection, filter, replacement, options) {
  110. if (filter == null || typeof filter !== 'object') {
  111. throw new error_1.MongoInvalidArgumentError('Argument "filter" must be an object');
  112. }
  113. if (replacement == null || typeof replacement !== 'object') {
  114. throw new error_1.MongoInvalidArgumentError('Argument "replacement" must be an object');
  115. }
  116. if ((0, utils_1.hasAtomicOperators)(replacement)) {
  117. throw new error_1.MongoInvalidArgumentError('Replacement document must not contain atomic operators');
  118. }
  119. super(collection, filter, options);
  120. this.cmdBase.update = replacement;
  121. configureFindAndModifyCmdBaseUpdateOpts(this.cmdBase, options);
  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)) {
  135. throw new error_1.MongoInvalidArgumentError('Update document requires atomic operators');
  136. }
  137. super(collection, filter, options);
  138. this.cmdBase.update = update;
  139. configureFindAndModifyCmdBaseUpdateOpts(this.cmdBase, options);
  140. if (options.arrayFilters) {
  141. this.cmdBase.arrayFilters = options.arrayFilters;
  142. }
  143. }
  144. }
  145. exports.FindOneAndUpdateOperation = FindOneAndUpdateOperation;
  146. (0, operation_1.defineAspects)(FindAndModifyOperation, [
  147. operation_1.Aspect.WRITE_OPERATION,
  148. operation_1.Aspect.RETRYABLE,
  149. operation_1.Aspect.EXPLAINABLE
  150. ]);
  151. //# sourceMappingURL=find_and_modify.js.map