delete.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 command_1 = require("./command");
  6. const operation_1 = require("./operation");
  7. /** @internal */
  8. class DeleteOperation extends command_1.CommandOperation {
  9. constructor(ns, statements, options) {
  10. super(undefined, options);
  11. this.options = options;
  12. this.ns = ns;
  13. this.statements = statements;
  14. }
  15. get canRetryWrite() {
  16. if (super.canRetryWrite === false) {
  17. return false;
  18. }
  19. return this.statements.every(op => (op.limit != null ? op.limit > 0 : true));
  20. }
  21. async execute(server, session) {
  22. const options = this.options ?? {};
  23. const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
  24. const command = {
  25. delete: this.ns.collection,
  26. deletes: this.statements,
  27. ordered
  28. };
  29. if (options.let) {
  30. command.let = options.let;
  31. }
  32. // we check for undefined specifically here to allow falsy values
  33. // eslint-disable-next-line no-restricted-syntax
  34. if (options.comment !== undefined) {
  35. command.comment = options.comment;
  36. }
  37. const unacknowledgedWrite = this.writeConcern && this.writeConcern.w === 0;
  38. if (unacknowledgedWrite) {
  39. if (this.statements.find((o) => o.hint)) {
  40. // TODO(NODE-3541): fix error for hint with unacknowledged writes
  41. throw new error_1.MongoCompatibilityError(`hint is not supported with unacknowledged writes`);
  42. }
  43. }
  44. return super.executeCommand(server, session, command);
  45. }
  46. }
  47. exports.DeleteOperation = DeleteOperation;
  48. class DeleteOneOperation extends DeleteOperation {
  49. constructor(collection, filter, options) {
  50. super(collection.s.namespace, [makeDeleteStatement(filter, { ...options, limit: 1 })], options);
  51. }
  52. async execute(server, session) {
  53. const res = (await super.execute(server, session));
  54. if (this.explain)
  55. return res;
  56. if (res.code)
  57. throw new error_1.MongoServerError(res);
  58. if (res.writeErrors)
  59. throw new error_1.MongoServerError(res.writeErrors[0]);
  60. return {
  61. acknowledged: this.writeConcern?.w !== 0,
  62. deletedCount: res.n
  63. };
  64. }
  65. }
  66. exports.DeleteOneOperation = DeleteOneOperation;
  67. class DeleteManyOperation extends DeleteOperation {
  68. constructor(collection, filter, options) {
  69. super(collection.s.namespace, [makeDeleteStatement(filter, options)], options);
  70. }
  71. async execute(server, session) {
  72. const res = (await super.execute(server, session));
  73. if (this.explain)
  74. return res;
  75. if (res.code)
  76. throw new error_1.MongoServerError(res);
  77. if (res.writeErrors)
  78. throw new error_1.MongoServerError(res.writeErrors[0]);
  79. return {
  80. acknowledged: this.writeConcern?.w !== 0,
  81. deletedCount: res.n
  82. };
  83. }
  84. }
  85. exports.DeleteManyOperation = DeleteManyOperation;
  86. function makeDeleteStatement(filter, options) {
  87. const op = {
  88. q: filter,
  89. limit: typeof options.limit === 'number' ? options.limit : 0
  90. };
  91. if (options.collation) {
  92. op.collation = options.collation;
  93. }
  94. if (options.hint) {
  95. op.hint = options.hint;
  96. }
  97. return op;
  98. }
  99. exports.makeDeleteStatement = makeDeleteStatement;
  100. (0, operation_1.defineAspects)(DeleteOperation, [operation_1.Aspect.RETRYABLE, operation_1.Aspect.WRITE_OPERATION]);
  101. (0, operation_1.defineAspects)(DeleteOneOperation, [
  102. operation_1.Aspect.RETRYABLE,
  103. operation_1.Aspect.WRITE_OPERATION,
  104. operation_1.Aspect.EXPLAINABLE,
  105. operation_1.Aspect.SKIP_COLLATION
  106. ]);
  107. (0, operation_1.defineAspects)(DeleteManyOperation, [
  108. operation_1.Aspect.WRITE_OPERATION,
  109. operation_1.Aspect.EXPLAINABLE,
  110. operation_1.Aspect.SKIP_COLLATION
  111. ]);
  112. //# sourceMappingURL=delete.js.map