update.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.makeUpdateStatement = exports.ReplaceOneOperation = exports.UpdateManyOperation = exports.UpdateOneOperation = exports.UpdateOperation = void 0;
  4. const error_1 = require("../error");
  5. const utils_1 = require("../utils");
  6. const command_1 = require("./command");
  7. const operation_1 = require("./operation");
  8. /** @internal */
  9. class UpdateOperation extends command_1.CommandCallbackOperation {
  10. constructor(ns, statements, options) {
  11. super(undefined, options);
  12. this.options = options;
  13. this.ns = ns;
  14. this.statements = statements;
  15. }
  16. get canRetryWrite() {
  17. if (super.canRetryWrite === false) {
  18. return false;
  19. }
  20. return this.statements.every(op => op.multi == null || op.multi === false);
  21. }
  22. executeCallback(server, session, callback) {
  23. const options = this.options ?? {};
  24. const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
  25. const command = {
  26. update: this.ns.collection,
  27. updates: this.statements,
  28. ordered
  29. };
  30. if (typeof options.bypassDocumentValidation === 'boolean') {
  31. command.bypassDocumentValidation = options.bypassDocumentValidation;
  32. }
  33. if (options.let) {
  34. command.let = options.let;
  35. }
  36. // we check for undefined specifically here to allow falsy values
  37. // eslint-disable-next-line no-restricted-syntax
  38. if (options.comment !== undefined) {
  39. command.comment = options.comment;
  40. }
  41. const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0;
  42. if (unacknowledgedWrite) {
  43. if (this.statements.find((o) => o.hint)) {
  44. // TODO(NODE-3541): fix error for hint with unacknowledged writes
  45. callback(new error_1.MongoCompatibilityError(`hint is not supported with unacknowledged writes`));
  46. return;
  47. }
  48. }
  49. super.executeCommandCallback(server, session, command, callback);
  50. }
  51. }
  52. exports.UpdateOperation = UpdateOperation;
  53. /** @internal */
  54. class UpdateOneOperation extends UpdateOperation {
  55. constructor(collection, filter, update, options) {
  56. super(collection.s.namespace, [makeUpdateStatement(filter, update, { ...options, multi: false })], options);
  57. if (!(0, utils_1.hasAtomicOperators)(update)) {
  58. throw new error_1.MongoInvalidArgumentError('Update document requires atomic operators');
  59. }
  60. }
  61. executeCallback(server, session, callback) {
  62. super.executeCallback(server, session, (err, res) => {
  63. if (err || !res)
  64. return callback(err);
  65. if (this.explain != null)
  66. return callback(undefined, res);
  67. if (res.code)
  68. return callback(new error_1.MongoServerError(res));
  69. if (res.writeErrors)
  70. return callback(new error_1.MongoServerError(res.writeErrors[0]));
  71. callback(undefined, {
  72. acknowledged: this.writeConcern?.w !== 0 ?? true,
  73. modifiedCount: res.nModified != null ? res.nModified : res.n,
  74. upsertedId: Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null,
  75. upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0,
  76. matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n
  77. });
  78. });
  79. }
  80. }
  81. exports.UpdateOneOperation = UpdateOneOperation;
  82. /** @internal */
  83. class UpdateManyOperation extends UpdateOperation {
  84. constructor(collection, filter, update, options) {
  85. super(collection.s.namespace, [makeUpdateStatement(filter, update, { ...options, multi: true })], options);
  86. if (!(0, utils_1.hasAtomicOperators)(update)) {
  87. throw new error_1.MongoInvalidArgumentError('Update document requires atomic operators');
  88. }
  89. }
  90. executeCallback(server, session, callback) {
  91. super.executeCallback(server, session, (err, res) => {
  92. if (err || !res)
  93. return callback(err);
  94. if (this.explain != null)
  95. return callback(undefined, res);
  96. if (res.code)
  97. return callback(new error_1.MongoServerError(res));
  98. if (res.writeErrors)
  99. return callback(new error_1.MongoServerError(res.writeErrors[0]));
  100. callback(undefined, {
  101. acknowledged: this.writeConcern?.w !== 0 ?? true,
  102. modifiedCount: res.nModified != null ? res.nModified : res.n,
  103. upsertedId: Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null,
  104. upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0,
  105. matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n
  106. });
  107. });
  108. }
  109. }
  110. exports.UpdateManyOperation = UpdateManyOperation;
  111. /** @internal */
  112. class ReplaceOneOperation extends UpdateOperation {
  113. constructor(collection, filter, replacement, options) {
  114. super(collection.s.namespace, [makeUpdateStatement(filter, replacement, { ...options, multi: false })], options);
  115. if ((0, utils_1.hasAtomicOperators)(replacement)) {
  116. throw new error_1.MongoInvalidArgumentError('Replacement document must not contain atomic operators');
  117. }
  118. }
  119. executeCallback(server, session, callback) {
  120. super.executeCallback(server, session, (err, res) => {
  121. if (err || !res)
  122. return callback(err);
  123. if (this.explain != null)
  124. return callback(undefined, res);
  125. if (res.code)
  126. return callback(new error_1.MongoServerError(res));
  127. if (res.writeErrors)
  128. return callback(new error_1.MongoServerError(res.writeErrors[0]));
  129. callback(undefined, {
  130. acknowledged: this.writeConcern?.w !== 0 ?? true,
  131. modifiedCount: res.nModified != null ? res.nModified : res.n,
  132. upsertedId: Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null,
  133. upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0,
  134. matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n
  135. });
  136. });
  137. }
  138. }
  139. exports.ReplaceOneOperation = ReplaceOneOperation;
  140. function makeUpdateStatement(filter, update, options) {
  141. if (filter == null || typeof filter !== 'object') {
  142. throw new error_1.MongoInvalidArgumentError('Selector must be a valid JavaScript object');
  143. }
  144. if (update == null || typeof update !== 'object') {
  145. throw new error_1.MongoInvalidArgumentError('Document must be a valid JavaScript object');
  146. }
  147. const op = { q: filter, u: update };
  148. if (typeof options.upsert === 'boolean') {
  149. op.upsert = options.upsert;
  150. }
  151. if (options.multi) {
  152. op.multi = options.multi;
  153. }
  154. if (options.hint) {
  155. op.hint = options.hint;
  156. }
  157. if (options.arrayFilters) {
  158. op.arrayFilters = options.arrayFilters;
  159. }
  160. if (options.collation) {
  161. op.collation = options.collation;
  162. }
  163. return op;
  164. }
  165. exports.makeUpdateStatement = makeUpdateStatement;
  166. (0, operation_1.defineAspects)(UpdateOperation, [operation_1.Aspect.RETRYABLE, operation_1.Aspect.WRITE_OPERATION, operation_1.Aspect.SKIP_COLLATION]);
  167. (0, operation_1.defineAspects)(UpdateOneOperation, [
  168. operation_1.Aspect.RETRYABLE,
  169. operation_1.Aspect.WRITE_OPERATION,
  170. operation_1.Aspect.EXPLAINABLE,
  171. operation_1.Aspect.SKIP_COLLATION
  172. ]);
  173. (0, operation_1.defineAspects)(UpdateManyOperation, [
  174. operation_1.Aspect.WRITE_OPERATION,
  175. operation_1.Aspect.EXPLAINABLE,
  176. operation_1.Aspect.SKIP_COLLATION
  177. ]);
  178. (0, operation_1.defineAspects)(ReplaceOneOperation, [
  179. operation_1.Aspect.RETRYABLE,
  180. operation_1.Aspect.WRITE_OPERATION,
  181. operation_1.Aspect.SKIP_COLLATION
  182. ]);
  183. //# sourceMappingURL=update.js.map