delete.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.CommandCallbackOperation {
  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. executeCallback(server, session, callback) {
  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. callback(new error_1.MongoCompatibilityError(`hint is not supported with unacknowledged writes`));
  42. return;
  43. }
  44. }
  45. super.executeCommandCallback(server, session, command, callback);
  46. }
  47. }
  48. exports.DeleteOperation = DeleteOperation;
  49. class DeleteOneOperation extends DeleteOperation {
  50. constructor(collection, filter, options) {
  51. super(collection.s.namespace, [makeDeleteStatement(filter, { ...options, limit: 1 })], options);
  52. }
  53. executeCallback(server, session, callback) {
  54. super.executeCallback(server, session, (err, res) => {
  55. if (err || res == null)
  56. return callback(err);
  57. if (res.code)
  58. return callback(new error_1.MongoServerError(res));
  59. if (res.writeErrors)
  60. return callback(new error_1.MongoServerError(res.writeErrors[0]));
  61. if (this.explain)
  62. return callback(undefined, res);
  63. callback(undefined, {
  64. acknowledged: this.writeConcern?.w !== 0 ?? true,
  65. deletedCount: res.n
  66. });
  67. });
  68. }
  69. }
  70. exports.DeleteOneOperation = DeleteOneOperation;
  71. class DeleteManyOperation extends DeleteOperation {
  72. constructor(collection, filter, options) {
  73. super(collection.s.namespace, [makeDeleteStatement(filter, options)], options);
  74. }
  75. executeCallback(server, session, callback) {
  76. super.executeCallback(server, session, (err, res) => {
  77. if (err || res == null)
  78. return callback(err);
  79. if (res.code)
  80. return callback(new error_1.MongoServerError(res));
  81. if (res.writeErrors)
  82. return callback(new error_1.MongoServerError(res.writeErrors[0]));
  83. if (this.explain)
  84. return callback(undefined, res);
  85. callback(undefined, {
  86. acknowledged: this.writeConcern?.w !== 0 ?? true,
  87. deletedCount: res.n
  88. });
  89. });
  90. }
  91. }
  92. exports.DeleteManyOperation = DeleteManyOperation;
  93. function makeDeleteStatement(filter, options) {
  94. const op = {
  95. q: filter,
  96. limit: typeof options.limit === 'number' ? options.limit : 0
  97. };
  98. if (options.collation) {
  99. op.collation = options.collation;
  100. }
  101. if (options.hint) {
  102. op.hint = options.hint;
  103. }
  104. return op;
  105. }
  106. exports.makeDeleteStatement = makeDeleteStatement;
  107. (0, operation_1.defineAspects)(DeleteOperation, [operation_1.Aspect.RETRYABLE, operation_1.Aspect.WRITE_OPERATION]);
  108. (0, operation_1.defineAspects)(DeleteOneOperation, [
  109. operation_1.Aspect.RETRYABLE,
  110. operation_1.Aspect.WRITE_OPERATION,
  111. operation_1.Aspect.EXPLAINABLE,
  112. operation_1.Aspect.SKIP_COLLATION
  113. ]);
  114. (0, operation_1.defineAspects)(DeleteManyOperation, [
  115. operation_1.Aspect.WRITE_OPERATION,
  116. operation_1.Aspect.EXPLAINABLE,
  117. operation_1.Aspect.SKIP_COLLATION
  118. ]);
  119. //# sourceMappingURL=delete.js.map