index_exists.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. const OperationBase = require('./operation').OperationBase;
  3. const handleCallback = require('../utils').handleCallback;
  4. const indexInformationDb = require('./db_ops').indexInformation;
  5. class IndexExistsOperation extends OperationBase {
  6. constructor(collection, indexes, options) {
  7. super(options);
  8. this.collection = collection;
  9. this.indexes = indexes;
  10. }
  11. execute(callback) {
  12. const coll = this.collection;
  13. const indexes = this.indexes;
  14. const options = this.options;
  15. indexInformationDb(coll.s.db, coll.collectionName, options, (err, indexInformation) => {
  16. // If we have an error return
  17. if (err != null) return handleCallback(callback, err, null);
  18. // Let's check for the index names
  19. if (!Array.isArray(indexes))
  20. return handleCallback(callback, null, indexInformation[indexes] != null);
  21. // Check in list of indexes
  22. for (let i = 0; i < indexes.length; i++) {
  23. if (indexInformation[indexes[i]] == null) {
  24. return handleCallback(callback, null, false);
  25. }
  26. }
  27. // All keys found return true
  28. return handleCallback(callback, null, true);
  29. });
  30. }
  31. }
  32. module.exports = IndexExistsOperation;