-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(schema): avoid throwing duplicate index error if index spec keys …
…have different order or index has a custom name Fix #15109
- Loading branch information
Showing
4 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Compares two index specifications to determine if they are equal. | ||
* | ||
* #### Example: | ||
* isIndexSpecEqual({ a: 1, b: 1 }, { a: 1, b: 1 }); // true | ||
* isIndexSpecEqual({ a: 1, b: 1 }, { b: 1, a: 1 }); // false | ||
* isIndexSpecEqual({ a: 1, b: -1 }, { a: 1, b: 1 }); // false | ||
* | ||
* @param {Object} spec1 - The first index specification to compare. | ||
* @param {Object} spec2 - The second index specification to compare. | ||
* @returns {Boolean} Returns true if the index specifications are equal, otherwise returns false. | ||
*/ | ||
|
||
module.exports = function isIndexSpecEqual(spec1, spec2) { | ||
const spec1Keys = Object.keys(spec1); | ||
const spec2Keys = Object.keys(spec2); | ||
|
||
if (spec1Keys.length !== spec2Keys.length) { | ||
return false; | ||
} | ||
|
||
for (let i = 0; i < spec1Keys.length; i++) { | ||
const key = spec1Keys[i]; | ||
if (key !== spec2Keys[i] || spec1[key] !== spec2[key]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const isIndexSpecEqual = require('../../lib/helpers/indexes/isIndexSpecEqual'); | ||
|
||
describe('isIndexSpecEqual', function() { | ||
it('should return true for equal index specifications', () => { | ||
const spec1 = { name: 1, age: -1 }; | ||
const spec2 = { name: 1, age: -1 }; | ||
const result = isIndexSpecEqual(spec1, spec2); | ||
assert.strictEqual(result, true); | ||
}); | ||
|
||
it('should return false for different key order', () => { | ||
const spec1 = { name: 1, age: -1 }; | ||
const spec2 = { age: -1, name: 1 }; | ||
const result = isIndexSpecEqual(spec1, spec2); | ||
assert.strictEqual(result, false); | ||
}); | ||
|
||
it('should return false for different index keys', () => { | ||
const spec1 = { name: 1, age: -1 }; | ||
const spec2 = { name: 1, dob: -1 }; | ||
const result = isIndexSpecEqual(spec1, spec2); | ||
assert.strictEqual(result, false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters