validate_collection.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. const CommandOperation = require('./command');
  3. class ValidateCollectionOperation extends CommandOperation {
  4. constructor(admin, collectionName, options) {
  5. // Decorate command with extra options
  6. let command = { validate: collectionName };
  7. const keys = Object.keys(options);
  8. for (let i = 0; i < keys.length; i++) {
  9. if (Object.prototype.hasOwnProperty.call(options, keys[i]) && keys[i] !== 'session') {
  10. command[keys[i]] = options[keys[i]];
  11. }
  12. }
  13. super(admin.s.db, options, null, command);
  14. this.collectionName = collectionName;
  15. }
  16. execute(callback) {
  17. const collectionName = this.collectionName;
  18. super.execute((err, doc) => {
  19. if (err != null) return callback(err, null);
  20. if (doc.ok === 0) return callback(new Error('Error with validate command'), null);
  21. if (doc.result != null && doc.result.constructor !== String)
  22. return callback(new Error('Error with validation data'), null);
  23. if (doc.result != null && doc.result.match(/exception|corrupt/) != null)
  24. return callback(new Error('Error: invalid collection ' + collectionName), null);
  25. if (doc.valid != null && !doc.valid)
  26. return callback(new Error('Error: invalid collection ' + collectionName), null);
  27. return callback(null, doc);
  28. });
  29. }
  30. }
  31. module.exports = ValidateCollectionOperation;