drop.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DropDatabaseOperation = exports.DropCollectionOperation = void 0;
  4. const error_1 = require("../error");
  5. const command_1 = require("./command");
  6. const operation_1 = require("./operation");
  7. /** @internal */
  8. class DropCollectionOperation extends command_1.CommandOperation {
  9. constructor(db, name, options = {}) {
  10. super(db, options);
  11. this.db = db;
  12. this.options = options;
  13. this.name = name;
  14. }
  15. async execute(server, session) {
  16. const db = this.db;
  17. const options = this.options;
  18. const name = this.name;
  19. const encryptedFieldsMap = db.client.options.autoEncryption?.encryptedFieldsMap;
  20. let encryptedFields = options.encryptedFields ?? encryptedFieldsMap?.[`${db.databaseName}.${name}`];
  21. if (!encryptedFields && encryptedFieldsMap) {
  22. // If the MongoClient was configured with an encryptedFieldsMap,
  23. // and no encryptedFields config was available in it or explicitly
  24. // passed as an argument, the spec tells us to look one up using
  25. // listCollections().
  26. const listCollectionsResult = await db
  27. .listCollections({ name }, { nameOnly: false })
  28. .toArray();
  29. encryptedFields = listCollectionsResult?.[0]?.options?.encryptedFields;
  30. }
  31. if (encryptedFields) {
  32. const escCollection = encryptedFields.escCollection || `enxcol_.${name}.esc`;
  33. const ecocCollection = encryptedFields.ecocCollection || `enxcol_.${name}.ecoc`;
  34. for (const collectionName of [escCollection, ecocCollection]) {
  35. // Drop auxilliary collections, ignoring potential NamespaceNotFound errors.
  36. const dropOp = new DropCollectionOperation(db, collectionName);
  37. try {
  38. await dropOp.executeWithoutEncryptedFieldsCheck(server, session);
  39. }
  40. catch (err) {
  41. if (!(err instanceof error_1.MongoServerError) ||
  42. err.code !== error_1.MONGODB_ERROR_CODES.NamespaceNotFound) {
  43. throw err;
  44. }
  45. }
  46. }
  47. }
  48. return this.executeWithoutEncryptedFieldsCheck(server, session);
  49. }
  50. async executeWithoutEncryptedFieldsCheck(server, session) {
  51. await super.executeCommand(server, session, { drop: this.name });
  52. return true;
  53. }
  54. }
  55. exports.DropCollectionOperation = DropCollectionOperation;
  56. /** @internal */
  57. class DropDatabaseOperation extends command_1.CommandOperation {
  58. constructor(db, options) {
  59. super(db, options);
  60. this.options = options;
  61. }
  62. async execute(server, session) {
  63. await super.executeCommand(server, session, { dropDatabase: 1 });
  64. return true;
  65. }
  66. }
  67. exports.DropDatabaseOperation = DropDatabaseOperation;
  68. (0, operation_1.defineAspects)(DropCollectionOperation, [operation_1.Aspect.WRITE_OPERATION]);
  69. (0, operation_1.defineAspects)(DropDatabaseOperation, [operation_1.Aspect.WRITE_OPERATION]);
  70. //# sourceMappingURL=drop.js.map