delete.js 5.2 KB

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