isIndexEqual.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. const get = require('../get');
  3. const utils = require('../../utils');
  4. module.exports = function isIndexEqual(key, options, dbIndex) {
  5. // If these options are different, need to rebuild the index
  6. const optionKeys = [
  7. 'unique',
  8. 'partialFilterExpression',
  9. 'sparse',
  10. 'expireAfterSeconds',
  11. 'collation'
  12. ];
  13. for (const key of optionKeys) {
  14. if (!(key in options) && !(key in dbIndex)) {
  15. continue;
  16. }
  17. if (key === 'collation') {
  18. const definedKeys = Object.keys(options.collation);
  19. const schemaCollation = options.collation;
  20. const dbCollation = dbIndex.collation;
  21. for (const opt of definedKeys) {
  22. if (get(schemaCollation, opt) !== get(dbCollation, opt)) {
  23. return false;
  24. }
  25. }
  26. } else if (!utils.deepEqual(options[key], dbIndex[key])) {
  27. return false;
  28. }
  29. }
  30. const schemaIndexKeys = Object.keys(key);
  31. const dbIndexKeys = Object.keys(dbIndex.key);
  32. if (schemaIndexKeys.length !== dbIndexKeys.length) {
  33. return false;
  34. }
  35. for (let i = 0; i < schemaIndexKeys.length; ++i) {
  36. if (schemaIndexKeys[i] !== dbIndexKeys[i]) {
  37. return false;
  38. }
  39. if (!utils.deepEqual(key[schemaIndexKeys[i]], dbIndex.key[dbIndexKeys[i]])) {
  40. return false;
  41. }
  42. }
  43. return true;
  44. };