update.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.CommandOperation {
  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. execute(server, session, callback) {
  23. var _a;
  24. const options = (_a = this.options) !== null && _a !== void 0 ? _a : {};
  25. const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
  26. const command = {
  27. update: this.ns.collection,
  28. updates: this.statements,
  29. ordered
  30. };
  31. if (typeof options.bypassDocumentValidation === 'boolean') {
  32. command.bypassDocumentValidation = options.bypassDocumentValidation;
  33. }
  34. if (options.let) {
  35. command.let = options.let;
  36. }
  37. // we check for undefined specifically here to allow falsy values
  38. // eslint-disable-next-line no-restricted-syntax
  39. if (options.comment !== undefined) {
  40. command.comment = options.comment;
  41. }
  42. const statementWithCollation = this.statements.find(statement => !!statement.collation);
  43. if ((0, utils_1.collationNotSupported)(server, options) ||
  44. (statementWithCollation && (0, utils_1.collationNotSupported)(server, statementWithCollation))) {
  45. callback(new error_1.MongoCompatibilityError(`Server ${server.name} does not support collation`));
  46. return;
  47. }
  48. const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0;
  49. if (unacknowledgedWrite || (0, utils_1.maxWireVersion)(server) < 5) {
  50. if (this.statements.find((o) => o.hint)) {
  51. callback(new error_1.MongoCompatibilityError(`Servers < 3.4 do not support hint on update`));
  52. return;
  53. }
  54. }
  55. if (this.explain && (0, utils_1.maxWireVersion)(server) < 3) {
  56. callback(new error_1.MongoCompatibilityError(`Server ${server.name} does not support explain on update`));
  57. return;
  58. }
  59. if (this.statements.some(statement => !!statement.arrayFilters) && (0, utils_1.maxWireVersion)(server) < 6) {
  60. callback(new error_1.MongoCompatibilityError('Option "arrayFilters" is only supported on MongoDB 3.6+'));
  61. return;
  62. }
  63. super.executeCommand(server, session, command, callback);
  64. }
  65. }
  66. exports.UpdateOperation = UpdateOperation;
  67. /** @internal */
  68. class UpdateOneOperation extends UpdateOperation {
  69. constructor(collection, filter, update, options) {
  70. super(collection.s.namespace, [makeUpdateStatement(filter, update, { ...options, multi: false })], options);
  71. if (!(0, utils_1.hasAtomicOperators)(update)) {
  72. throw new error_1.MongoInvalidArgumentError('Update document requires atomic operators');
  73. }
  74. }
  75. execute(server, session, callback) {
  76. super.execute(server, session, (err, res) => {
  77. var _a, _b;
  78. if (err || !res)
  79. return callback(err);
  80. if (this.explain != null)
  81. return callback(undefined, res);
  82. if (res.code)
  83. return callback(new error_1.MongoServerError(res));
  84. if (res.writeErrors)
  85. return callback(new error_1.MongoServerError(res.writeErrors[0]));
  86. callback(undefined, {
  87. acknowledged: (_b = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.w) !== 0) !== null && _b !== void 0 ? _b : true,
  88. modifiedCount: res.nModified != null ? res.nModified : res.n,
  89. upsertedId: Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null,
  90. upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0,
  91. matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n
  92. });
  93. });
  94. }
  95. }
  96. exports.UpdateOneOperation = UpdateOneOperation;
  97. /** @internal */
  98. class UpdateManyOperation extends UpdateOperation {
  99. constructor(collection, filter, update, options) {
  100. super(collection.s.namespace, [makeUpdateStatement(filter, update, { ...options, multi: true })], options);
  101. if (!(0, utils_1.hasAtomicOperators)(update)) {
  102. throw new error_1.MongoInvalidArgumentError('Update document requires atomic operators');
  103. }
  104. }
  105. execute(server, session, callback) {
  106. super.execute(server, session, (err, res) => {
  107. var _a, _b;
  108. if (err || !res)
  109. return callback(err);
  110. if (this.explain != null)
  111. return callback(undefined, res);
  112. if (res.code)
  113. return callback(new error_1.MongoServerError(res));
  114. if (res.writeErrors)
  115. return callback(new error_1.MongoServerError(res.writeErrors[0]));
  116. callback(undefined, {
  117. acknowledged: (_b = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.w) !== 0) !== null && _b !== void 0 ? _b : true,
  118. modifiedCount: res.nModified != null ? res.nModified : res.n,
  119. upsertedId: Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null,
  120. upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0,
  121. matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n
  122. });
  123. });
  124. }
  125. }
  126. exports.UpdateManyOperation = UpdateManyOperation;
  127. /** @internal */
  128. class ReplaceOneOperation extends UpdateOperation {
  129. constructor(collection, filter, replacement, options) {
  130. super(collection.s.namespace, [makeUpdateStatement(filter, replacement, { ...options, multi: false })], options);
  131. if ((0, utils_1.hasAtomicOperators)(replacement)) {
  132. throw new error_1.MongoInvalidArgumentError('Replacement document must not contain atomic operators');
  133. }
  134. }
  135. execute(server, session, callback) {
  136. super.execute(server, session, (err, res) => {
  137. var _a, _b;
  138. if (err || !res)
  139. return callback(err);
  140. if (this.explain != null)
  141. return callback(undefined, res);
  142. if (res.code)
  143. return callback(new error_1.MongoServerError(res));
  144. if (res.writeErrors)
  145. return callback(new error_1.MongoServerError(res.writeErrors[0]));
  146. callback(undefined, {
  147. acknowledged: (_b = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.w) !== 0) !== null && _b !== void 0 ? _b : true,
  148. modifiedCount: res.nModified != null ? res.nModified : res.n,
  149. upsertedId: Array.isArray(res.upserted) && res.upserted.length > 0 ? res.upserted[0]._id : null,
  150. upsertedCount: Array.isArray(res.upserted) && res.upserted.length ? res.upserted.length : 0,
  151. matchedCount: Array.isArray(res.upserted) && res.upserted.length > 0 ? 0 : res.n
  152. });
  153. });
  154. }
  155. }
  156. exports.ReplaceOneOperation = ReplaceOneOperation;
  157. function makeUpdateStatement(filter, update, options) {
  158. if (filter == null || typeof filter !== 'object') {
  159. throw new error_1.MongoInvalidArgumentError('Selector must be a valid JavaScript object');
  160. }
  161. if (update == null || typeof update !== 'object') {
  162. throw new error_1.MongoInvalidArgumentError('Document must be a valid JavaScript object');
  163. }
  164. const op = { q: filter, u: update };
  165. if (typeof options.upsert === 'boolean') {
  166. op.upsert = options.upsert;
  167. }
  168. if (options.multi) {
  169. op.multi = options.multi;
  170. }
  171. if (options.hint) {
  172. op.hint = options.hint;
  173. }
  174. if (options.arrayFilters) {
  175. op.arrayFilters = options.arrayFilters;
  176. }
  177. if (options.collation) {
  178. op.collation = options.collation;
  179. }
  180. return op;
  181. }
  182. exports.makeUpdateStatement = makeUpdateStatement;
  183. (0, operation_1.defineAspects)(UpdateOperation, [operation_1.Aspect.RETRYABLE, operation_1.Aspect.WRITE_OPERATION, operation_1.Aspect.SKIP_COLLATION]);
  184. (0, operation_1.defineAspects)(UpdateOneOperation, [
  185. operation_1.Aspect.RETRYABLE,
  186. operation_1.Aspect.WRITE_OPERATION,
  187. operation_1.Aspect.EXPLAINABLE,
  188. operation_1.Aspect.SKIP_COLLATION
  189. ]);
  190. (0, operation_1.defineAspects)(UpdateManyOperation, [
  191. operation_1.Aspect.WRITE_OPERATION,
  192. operation_1.Aspect.EXPLAINABLE,
  193. operation_1.Aspect.SKIP_COLLATION
  194. ]);
  195. (0, operation_1.defineAspects)(ReplaceOneOperation, [
  196. operation_1.Aspect.RETRYABLE,
  197. operation_1.Aspect.WRITE_OPERATION,
  198. operation_1.Aspect.SKIP_COLLATION
  199. ]);
  200. //# sourceMappingURL=update.js.map