update.js 7.4 KB

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