Skip to content

Commit 1e8753f

Browse files
committed
fix: address code review comments
1 parent 2db8430 commit 1e8753f

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

lib/model.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -5009,8 +5009,7 @@ Model.recompileSchema = function recompileSchema() {
50095009
}
50105010
}
50115011

5012-
const overwriteExisting = true;
5013-
applyEmbeddedDiscriminators(this.schema, new WeakSet(), overwriteExisting);
5012+
applyEmbeddedDiscriminators(this.schema, new WeakSet(), true);
50145013
};
50155014

50165015
/**

test/model.test.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -7432,28 +7432,31 @@ describe('Model', function() {
74327432

74337433
// Define discriminated class before model is compiled
74347434
class Deco1 extends Decorator { whoAmI() { return 'I am Test1'; }}
7435-
const deco1Schema = new Schema({}).loadClass(Deco1);
7435+
const deco1Schema = new Schema({});
74367436
deco1Schema.loadClass(Deco1);
74377437
decoratorSchema.discriminator('Test1', deco1Schema);
74387438

74397439
// Define model that uses discriminated schema
74407440
const shopSchema = new Schema({
74417441
item: { type: decoratorSchema, required: true }
74427442
});
7443-
7444-
class Shop {}
7445-
shopSchema.loadClass(Shop);
74467443
const shopModel = db.model('Test', shopSchema);
74477444

74487445
// Define another discriminated class after the model is compiled
74497446
class Deco2 extends Decorator { whoAmI() { return 'I am Test2'; }}
7450-
const deco2Schema = new Schema({}).loadClass(Deco2);
7447+
const deco2Schema = new Schema({});
74517448
deco2Schema.loadClass(Deco2);
74527449
decoratorSchema.discriminator('Test2', deco2Schema);
74537450

7451+
let instance = new shopModel({ item: { type: 'Test1' } });
7452+
assert.equal(instance.item.whoAmI(), 'I am Test1');
7453+
7454+
instance = new shopModel({ item: { type: 'Test2' } });
7455+
assert.equal(instance.item.whoAmI(), 'I am BaseDeco');
7456+
74547457
shopModel.recompileSchema();
74557458

7456-
let instance = new shopModel({ item: { type: 'Test1' } });
7459+
instance = new shopModel({ item: { type: 'Test1' } });
74577460
assert.equal(instance.item.whoAmI(), 'I am Test1');
74587461

74597462
instance = new shopModel({ item: { type: 'Test2' } });

0 commit comments

Comments
 (0)