validate_collection.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ValidateCollectionOperation = void 0;
  4. const error_1 = require("../error");
  5. const command_1 = require("./command");
  6. /** @internal */
  7. class ValidateCollectionOperation extends command_1.CommandOperation {
  8. constructor(admin, collectionName, options) {
  9. // Decorate command with extra options
  10. const command = { validate: collectionName };
  11. const keys = Object.keys(options);
  12. for (let i = 0; i < keys.length; i++) {
  13. if (Object.prototype.hasOwnProperty.call(options, keys[i]) && keys[i] !== 'session') {
  14. command[keys[i]] = options[keys[i]];
  15. }
  16. }
  17. super(admin.s.db, options);
  18. this.options = options;
  19. this.command = command;
  20. this.collectionName = collectionName;
  21. }
  22. execute(server, session, callback) {
  23. const collectionName = this.collectionName;
  24. super.executeCommand(server, session, this.command, (err, doc) => {
  25. if (err != null)
  26. return callback(err);
  27. // TODO(NODE-3483): Replace these with MongoUnexpectedServerResponseError
  28. if (doc.ok === 0)
  29. return callback(new error_1.MongoRuntimeError('Error with validate command'));
  30. if (doc.result != null && typeof doc.result !== 'string')
  31. return callback(new error_1.MongoRuntimeError('Error with validation data'));
  32. if (doc.result != null && doc.result.match(/exception|corrupt/) != null)
  33. return callback(new error_1.MongoRuntimeError(`Invalid collection ${collectionName}`));
  34. if (doc.valid != null && !doc.valid)
  35. return callback(new error_1.MongoRuntimeError(`Invalid collection ${collectionName}`));
  36. return callback(undefined, doc);
  37. });
  38. }
  39. }
  40. exports.ValidateCollectionOperation = ValidateCollectionOperation;
  41. //# sourceMappingURL=validate_collection.js.map