Skip to content

Commit d0e149b

Browse files
committedMar 6, 2023
Merge pull request #12737 from Automattic/vkarpov15/gh-12654
fix(schema): copy indexes when calling `add()` with schema instance
1 parent e76c41c commit d0e149b

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed
 

‎lib/helpers/schema/merge.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ module.exports = function merge(s1, s2, skipConflictingPaths) {
2323
s1.virtuals[virtual] = s2.virtuals[virtual].clone();
2424
}
2525

26+
s1._indexes = s1._indexes.concat(s2._indexes || []);
2627
s1.s.hooks.merge(s2.s.hooks, false);
2728
};

‎test/schema.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -2401,6 +2401,16 @@ describe('schema', function() {
24012401
assert.equal(TurboManSchema.path('year').instance, 'Number');
24022402
});
24032403

2404+
it('copies indexes when calling add() with schema instance (gh-12654)', function() {
2405+
const ToySchema = Schema({ name: String });
2406+
ToySchema.index({ name: 1 });
2407+
2408+
const TurboManSchema = Schema();
2409+
TurboManSchema.add(ToySchema);
2410+
2411+
assert.deepStrictEqual(TurboManSchema.indexes(), [[{ name: 1 }, { background: true }]]);
2412+
});
2413+
24042414
describe('gh-8849', function() {
24052415
it('treats `select: undefined` as not specifying `select` option', function() {
24062416
const userSchema = new Schema({ name: { type: String, select: undefined } });

0 commit comments

Comments
 (0)
Please sign in to comment.