find_and_modify.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 ?? {};
  28. this.cmdBase = {
  29. remove: false,
  30. new: false,
  31. upsert: false
  32. };
  33. options.includeResultMetadata ??= false;
  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. async execute(server, session) {
  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 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. throw new error_1.MongoCompatibilityError('The current topology does not support a hint on findAndModify commands');
  84. }
  85. cmd.hint = options.hint;
  86. }
  87. // Execute the command
  88. const result = await super.executeCommand(server, session, cmd);
  89. return options.includeResultMetadata ? result : result.value ?? null;
  90. }
  91. }
  92. /** @internal */
  93. class FindOneAndDeleteOperation extends FindAndModifyOperation {
  94. constructor(collection, filter, options) {
  95. // Basic validation
  96. if (filter == null || typeof filter !== 'object') {
  97. throw new error_1.MongoInvalidArgumentError('Argument "filter" must be an object');
  98. }
  99. super(collection, filter, options);
  100. this.cmdBase.remove = true;
  101. }
  102. }
  103. exports.FindOneAndDeleteOperation = FindOneAndDeleteOperation;
  104. /** @internal */
  105. class FindOneAndReplaceOperation extends FindAndModifyOperation {
  106. constructor(collection, filter, replacement, options) {
  107. if (filter == null || typeof filter !== 'object') {
  108. throw new error_1.MongoInvalidArgumentError('Argument "filter" must be an object');
  109. }
  110. if (replacement == null || typeof replacement !== 'object') {
  111. throw new error_1.MongoInvalidArgumentError('Argument "replacement" must be an object');
  112. }
  113. if ((0, utils_1.hasAtomicOperators)(replacement)) {
  114. throw new error_1.MongoInvalidArgumentError('Replacement document must not contain atomic operators');
  115. }
  116. super(collection, filter, options);
  117. this.cmdBase.update = replacement;
  118. configureFindAndModifyCmdBaseUpdateOpts(this.cmdBase, options);
  119. }
  120. }
  121. exports.FindOneAndReplaceOperation = FindOneAndReplaceOperation;
  122. /** @internal */
  123. class FindOneAndUpdateOperation extends FindAndModifyOperation {
  124. constructor(collection, filter, update, options) {
  125. if (filter == null || typeof filter !== 'object') {
  126. throw new error_1.MongoInvalidArgumentError('Argument "filter" must be an object');
  127. }
  128. if (update == null || typeof update !== 'object') {
  129. throw new error_1.MongoInvalidArgumentError('Argument "update" must be an object');
  130. }
  131. if (!(0, utils_1.hasAtomicOperators)(update)) {
  132. throw new error_1.MongoInvalidArgumentError('Update document requires atomic operators');
  133. }
  134. super(collection, filter, options);
  135. this.cmdBase.update = update;
  136. configureFindAndModifyCmdBaseUpdateOpts(this.cmdBase, options);
  137. if (options.arrayFilters) {
  138. this.cmdBase.arrayFilters = options.arrayFilters;
  139. }
  140. }
  141. }
  142. exports.FindOneAndUpdateOperation = FindOneAndUpdateOperation;
  143. (0, operation_1.defineAspects)(FindAndModifyOperation, [
  144. operation_1.Aspect.WRITE_OPERATION,
  145. operation_1.Aspect.RETRYABLE,
  146. operation_1.Aspect.EXPLAINABLE
  147. ]);
  148. //# sourceMappingURL=find_and_modify.js.map