delete.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.makeDeleteStatement = exports.DeleteManyOperation = exports.DeleteOneOperation = exports.DeleteOperation = 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 DeleteOperation 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.limit != null ? op.limit > 0 : true));
  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. delete: this.ns.collection,
  28. deletes: this.statements,
  29. ordered
  30. };
  31. if (options.let) {
  32. command.let = options.let;
  33. }
  34. // we check for undefined specifically here to allow falsy values
  35. // eslint-disable-next-line no-restricted-syntax
  36. if (options.comment !== undefined) {
  37. command.comment = options.comment;
  38. }
  39. if (options.explain != null && (0, utils_1.maxWireVersion)(server) < 3) {
  40. return callback
  41. ? callback(new error_1.MongoCompatibilityError(`Server ${server.name} does not support explain on delete`))
  42. : undefined;
  43. }
  44. const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0;
  45. if (unacknowledgedWrite || (0, utils_1.maxWireVersion)(server) < 5) {
  46. if (this.statements.find((o) => o.hint)) {
  47. callback(new error_1.MongoCompatibilityError(`Servers < 3.4 do not support hint on delete`));
  48. return;
  49. }
  50. }
  51. const statementWithCollation = this.statements.find(statement => !!statement.collation);
  52. if (statementWithCollation && (0, utils_1.collationNotSupported)(server, statementWithCollation)) {
  53. callback(new error_1.MongoCompatibilityError(`Server ${server.name} does not support collation`));
  54. return;
  55. }
  56. super.executeCommand(server, session, command, callback);
  57. }
  58. }
  59. exports.DeleteOperation = DeleteOperation;
  60. class DeleteOneOperation extends DeleteOperation {
  61. constructor(collection, filter, options) {
  62. super(collection.s.namespace, [makeDeleteStatement(filter, { ...options, limit: 1 })], options);
  63. }
  64. execute(server, session, callback) {
  65. super.execute(server, session, (err, res) => {
  66. var _a, _b;
  67. if (err || res == null)
  68. return callback(err);
  69. if (res.code)
  70. return callback(new error_1.MongoServerError(res));
  71. if (res.writeErrors)
  72. return callback(new error_1.MongoServerError(res.writeErrors[0]));
  73. if (this.explain)
  74. return callback(undefined, res);
  75. callback(undefined, {
  76. acknowledged: (_b = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.w) !== 0) !== null && _b !== void 0 ? _b : true,
  77. deletedCount: res.n
  78. });
  79. });
  80. }
  81. }
  82. exports.DeleteOneOperation = DeleteOneOperation;
  83. class DeleteManyOperation extends DeleteOperation {
  84. constructor(collection, filter, options) {
  85. super(collection.s.namespace, [makeDeleteStatement(filter, options)], options);
  86. }
  87. execute(server, session, callback) {
  88. super.execute(server, session, (err, res) => {
  89. var _a, _b;
  90. if (err || res == null)
  91. return callback(err);
  92. if (res.code)
  93. return callback(new error_1.MongoServerError(res));
  94. if (res.writeErrors)
  95. return callback(new error_1.MongoServerError(res.writeErrors[0]));
  96. if (this.explain)
  97. return callback(undefined, res);
  98. callback(undefined, {
  99. acknowledged: (_b = ((_a = this.writeConcern) === null || _a === void 0 ? void 0 : _a.w) !== 0) !== null && _b !== void 0 ? _b : true,
  100. deletedCount: res.n
  101. });
  102. });
  103. }
  104. }
  105. exports.DeleteManyOperation = DeleteManyOperation;
  106. function makeDeleteStatement(filter, options) {
  107. const op = {
  108. q: filter,
  109. limit: typeof options.limit === 'number' ? options.limit : 0
  110. };
  111. if (options.single === true) {
  112. op.limit = 1;
  113. }
  114. if (options.collation) {
  115. op.collation = options.collation;
  116. }
  117. if (options.hint) {
  118. op.hint = options.hint;
  119. }
  120. return op;
  121. }
  122. exports.makeDeleteStatement = makeDeleteStatement;
  123. (0, operation_1.defineAspects)(DeleteOperation, [operation_1.Aspect.RETRYABLE, operation_1.Aspect.WRITE_OPERATION]);
  124. (0, operation_1.defineAspects)(DeleteOneOperation, [
  125. operation_1.Aspect.RETRYABLE,
  126. operation_1.Aspect.WRITE_OPERATION,
  127. operation_1.Aspect.EXPLAINABLE,
  128. operation_1.Aspect.SKIP_COLLATION
  129. ]);
  130. (0, operation_1.defineAspects)(DeleteManyOperation, [
  131. operation_1.Aspect.WRITE_OPERATION,
  132. operation_1.Aspect.EXPLAINABLE,
  133. operation_1.Aspect.SKIP_COLLATION
  134. ]);
  135. //# sourceMappingURL=delete.js.map