rename.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RenameOperation = void 0;
  4. const collection_1 = require("../collection");
  5. const error_1 = require("../error");
  6. const utils_1 = require("../utils");
  7. const operation_1 = require("./operation");
  8. const run_command_1 = require("./run_command");
  9. /** @internal */
  10. class RenameOperation extends run_command_1.RunAdminCommandOperation {
  11. constructor(collection, newName, options) {
  12. // Check the collection name
  13. (0, utils_1.checkCollectionName)(newName);
  14. // Build the command
  15. const renameCollection = collection.namespace;
  16. const toCollection = collection.s.namespace.withCollection(newName).toString();
  17. const dropTarget = typeof options.dropTarget === 'boolean' ? options.dropTarget : false;
  18. const cmd = { renameCollection: renameCollection, to: toCollection, dropTarget: dropTarget };
  19. super(collection, cmd, options);
  20. this.options = options;
  21. this.collection = collection;
  22. this.newName = newName;
  23. }
  24. execute(server, session, callback) {
  25. const coll = this.collection;
  26. super.execute(server, session, (err, doc) => {
  27. if (err)
  28. return callback(err);
  29. // We have an error
  30. if (doc === null || doc === void 0 ? void 0 : doc.errmsg) {
  31. return callback(new error_1.MongoServerError(doc));
  32. }
  33. let newColl;
  34. try {
  35. newColl = new collection_1.Collection(coll.s.db, this.newName, coll.s.options);
  36. }
  37. catch (err) {
  38. return callback(err);
  39. }
  40. return callback(undefined, newColl);
  41. });
  42. }
  43. }
  44. exports.RenameOperation = RenameOperation;
  45. (0, operation_1.defineAspects)(RenameOperation, [operation_1.Aspect.WRITE_OPERATION]);
  46. //# sourceMappingURL=rename.js.map