drop.js 4.1 KB

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