drop.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. }
  31. exports.DropCollectionOperation = DropCollectionOperation;
  32. async function dropCollections(db, name, options) {
  33. const timeoutContext = timeout_1.TimeoutContext.create({
  34. session: options.session,
  35. serverSelectionTimeoutMS: db.client.s.options.serverSelectionTimeoutMS,
  36. waitQueueTimeoutMS: db.client.s.options.waitQueueTimeoutMS,
  37. timeoutMS: options.timeoutMS
  38. });
  39. const encryptedFieldsMap = db.client.s.options.autoEncryption?.encryptedFieldsMap;
  40. let encryptedFields = options.encryptedFields ?? encryptedFieldsMap?.[`${db.databaseName}.${name}`];
  41. if (!encryptedFields && encryptedFieldsMap) {
  42. // If the MongoClient was configured with an encryptedFieldsMap,
  43. // and no encryptedFields config was available in it or explicitly
  44. // passed as an argument, the spec tells us to look one up using
  45. // listCollections().
  46. const listCollectionsResult = await db
  47. .listCollections({ name }, {
  48. nameOnly: false,
  49. session: options.session,
  50. timeoutContext: new abstract_cursor_1.CursorTimeoutContext(timeoutContext, Symbol())
  51. })
  52. .toArray();
  53. encryptedFields = listCollectionsResult?.[0]?.options?.encryptedFields;
  54. }
  55. if (encryptedFields) {
  56. const escCollection = encryptedFields.escCollection || `enxcol_.${name}.esc`;
  57. const ecocCollection = encryptedFields.ecocCollection || `enxcol_.${name}.ecoc`;
  58. for (const collectionName of [escCollection, ecocCollection]) {
  59. // Drop auxilliary collections, ignoring potential NamespaceNotFound errors.
  60. const dropOp = new DropCollectionOperation(db, collectionName, options);
  61. try {
  62. await (0, execute_operation_1.executeOperation)(db.client, dropOp, timeoutContext);
  63. }
  64. catch (err) {
  65. if (!(err instanceof __1.MongoServerError) ||
  66. err.code !== error_1.MONGODB_ERROR_CODES.NamespaceNotFound) {
  67. throw err;
  68. }
  69. }
  70. }
  71. }
  72. return await (0, execute_operation_1.executeOperation)(db.client, new DropCollectionOperation(db, name, options), timeoutContext);
  73. }
  74. /** @internal */
  75. class DropDatabaseOperation extends command_1.CommandOperation {
  76. constructor(db, options) {
  77. super(db, options);
  78. this.SERVER_COMMAND_RESPONSE_TYPE = responses_1.MongoDBResponse;
  79. this.options = options;
  80. }
  81. get commandName() {
  82. return 'dropDatabase';
  83. }
  84. buildCommandDocument(_connection, _session) {
  85. return { dropDatabase: 1 };
  86. }
  87. handleOk(_response) {
  88. return true;
  89. }
  90. }
  91. exports.DropDatabaseOperation = DropDatabaseOperation;
  92. (0, operation_1.defineAspects)(DropCollectionOperation, [operation_1.Aspect.WRITE_OPERATION]);
  93. (0, operation_1.defineAspects)(DropDatabaseOperation, [operation_1.Aspect.WRITE_OPERATION]);
  94. //# sourceMappingURL=drop.js.map