drop.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DropDatabaseOperation = exports.DropCollectionOperation = void 0;
  4. exports.dropCollections = dropCollections;
  5. const __1 = require("..");
  6. const responses_1 = require("../cmap/wire_protocol/responses");
  7. const abstract_cursor_1 = require("../cursor/abstract_cursor");
  8. const error_1 = require("../error");
  9. const timeout_1 = require("../timeout");
  10. const command_1 = require("./command");
  11. const execute_operation_1 = require("./execute_operation");
  12. const operation_1 = require("./operation");
  13. /** @internal */
  14. class DropCollectionOperation extends command_1.CommandOperation {
  15. constructor(db, name, options = {}) {
  16. super(db, options);
  17. this.SERVER_COMMAND_RESPONSE_TYPE = responses_1.MongoDBResponse;
  18. this.options = options;
  19. this.name = name;
  20. }
  21. get commandName() {
  22. return 'drop';
  23. }
  24. buildCommandDocument(_connection, _session) {
  25. return { drop: this.name };
  26. }
  27. handleOk(_response) {
  28. return true;
  29. }
  30. handleError(error) {
  31. if (!(error instanceof __1.MongoServerError))
  32. throw error;
  33. if (Number(error.code) !== error_1.MONGODB_ERROR_CODES.NamespaceNotFound)
  34. throw error;
  35. return false;
  36. }
  37. }
  38. exports.DropCollectionOperation = DropCollectionOperation;
  39. async function dropCollections(db, name, options) {
  40. const timeoutContext = timeout_1.TimeoutContext.create({
  41. session: options.session,
  42. serverSelectionTimeoutMS: db.client.s.options.serverSelectionTimeoutMS,
  43. waitQueueTimeoutMS: db.client.s.options.waitQueueTimeoutMS,
  44. timeoutMS: options.timeoutMS
  45. });
  46. const encryptedFieldsMap = db.client.s.options.autoEncryption?.encryptedFieldsMap;
  47. let encryptedFields = options.encryptedFields ?? encryptedFieldsMap?.[`${db.databaseName}.${name}`];
  48. if (!encryptedFields && encryptedFieldsMap) {
  49. // If the MongoClient was configured with an encryptedFieldsMap,
  50. // and no encryptedFields config was available in it or explicitly
  51. // passed as an argument, the spec tells us to look one up using
  52. // listCollections().
  53. const listCollectionsResult = await db
  54. .listCollections({ name }, {
  55. nameOnly: false,
  56. session: options.session,
  57. timeoutContext: new abstract_cursor_1.CursorTimeoutContext(timeoutContext, Symbol())
  58. })
  59. .toArray();
  60. encryptedFields = listCollectionsResult?.[0]?.options?.encryptedFields;
  61. }
  62. if (encryptedFields) {
  63. const escCollection = encryptedFields.escCollection || `enxcol_.${name}.esc`;
  64. const ecocCollection = encryptedFields.ecocCollection || `enxcol_.${name}.ecoc`;
  65. for (const collectionName of [escCollection, ecocCollection]) {
  66. // Drop auxilliary collections, ignoring potential NamespaceNotFound errors.
  67. const dropOp = new DropCollectionOperation(db, collectionName, options);
  68. await (0, execute_operation_1.executeOperation)(db.client, dropOp, timeoutContext);
  69. }
  70. }
  71. return await (0, execute_operation_1.executeOperation)(db.client, new DropCollectionOperation(db, name, options), timeoutContext);
  72. }
  73. /** @internal */
  74. class DropDatabaseOperation extends command_1.CommandOperation {
  75. constructor(db, options) {
  76. super(db, options);
  77. this.SERVER_COMMAND_RESPONSE_TYPE = responses_1.MongoDBResponse;
  78. this.options = options;
  79. }
  80. get commandName() {
  81. return 'dropDatabase';
  82. }
  83. buildCommandDocument(_connection, _session) {
  84. return { dropDatabase: 1 };
  85. }
  86. handleOk(_response) {
  87. return true;
  88. }
  89. }
  90. exports.DropDatabaseOperation = DropDatabaseOperation;
  91. (0, operation_1.defineAspects)(DropCollectionOperation, [operation_1.Aspect.WRITE_OPERATION]);
  92. (0, operation_1.defineAspects)(DropDatabaseOperation, [operation_1.Aspect.WRITE_OPERATION]);
  93. //# sourceMappingURL=drop.js.map