drop.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.CommandCallbackOperation {
  9. constructor(db, name, options = {}) {
  10. super(db, options);
  11. this.db = db;
  12. this.options = options;
  13. this.name = name;
  14. }
  15. executeCallback(server, session, callback) {
  16. (async () => {
  17. const db = this.db;
  18. const options = this.options;
  19. const name = this.name;
  20. const encryptedFieldsMap = db.client.options.autoEncryption?.encryptedFieldsMap;
  21. let encryptedFields = options.encryptedFields ?? encryptedFieldsMap?.[`${db.databaseName}.${name}`];
  22. if (!encryptedFields && encryptedFieldsMap) {
  23. // If the MongoClient was configured with an encryptedFieldsMap,
  24. // and no encryptedFields config was available in it or explicitly
  25. // passed as an argument, the spec tells us to look one up using
  26. // listCollections().
  27. const listCollectionsResult = await db
  28. .listCollections({ name }, { nameOnly: false })
  29. .toArray();
  30. encryptedFields = listCollectionsResult?.[0]?.options?.encryptedFields;
  31. }
  32. if (encryptedFields) {
  33. const escCollection = encryptedFields.escCollection || `enxcol_.${name}.esc`;
  34. const ecocCollection = encryptedFields.ecocCollection || `enxcol_.${name}.ecoc`;
  35. for (const collectionName of [escCollection, ecocCollection]) {
  36. // Drop auxilliary collections, ignoring potential NamespaceNotFound errors.
  37. const dropOp = new DropCollectionOperation(db, collectionName);
  38. try {
  39. await dropOp.executeWithoutEncryptedFieldsCheck(server, session);
  40. }
  41. catch (err) {
  42. if (!(err instanceof error_1.MongoServerError) ||
  43. err.code !== error_1.MONGODB_ERROR_CODES.NamespaceNotFound) {
  44. throw err;
  45. }
  46. }
  47. }
  48. }
  49. return this.executeWithoutEncryptedFieldsCheck(server, session);
  50. })().then(result => callback(undefined, result), err => callback(err));
  51. }
  52. executeWithoutEncryptedFieldsCheck(server, session) {
  53. return new Promise((resolve, reject) => {
  54. super.executeCommandCallback(server, session, { drop: this.name }, (err, result) => {
  55. if (err)
  56. return reject(err);
  57. resolve(!!result.ok);
  58. });
  59. });
  60. }
  61. }
  62. exports.DropCollectionOperation = DropCollectionOperation;
  63. /** @internal */
  64. class DropDatabaseOperation extends command_1.CommandCallbackOperation {
  65. constructor(db, options) {
  66. super(db, options);
  67. this.options = options;
  68. }
  69. executeCallback(server, session, callback) {
  70. super.executeCommandCallback(server, session, { dropDatabase: 1 }, (err, result) => {
  71. if (err)
  72. return callback(err);
  73. if (result.ok)
  74. return callback(undefined, true);
  75. callback(undefined, false);
  76. });
  77. }
  78. }
  79. exports.DropDatabaseOperation = DropDatabaseOperation;
  80. (0, operation_1.defineAspects)(DropCollectionOperation, [operation_1.Aspect.WRITE_OPERATION]);
  81. (0, operation_1.defineAspects)(DropDatabaseOperation, [operation_1.Aspect.WRITE_OPERATION]);
  82. //# sourceMappingURL=drop.js.map