update.js 8.9 KB

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