UNPKG

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