Skip to content

Commit

Permalink
fix: address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Apr 7, 2024
1 parent 2db8430 commit 1e8753f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
3 changes: 1 addition & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5009,8 +5009,7 @@ Model.recompileSchema = function recompileSchema() {
}
}

const overwriteExisting = true;
applyEmbeddedDiscriminators(this.schema, new WeakSet(), overwriteExisting);
applyEmbeddedDiscriminators(this.schema, new WeakSet(), true);
};

/**
Expand Down
15 changes: 9 additions & 6 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7432,28 +7432,31 @@ describe('Model', function() {

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

// Define model that uses discriminated schema
const shopSchema = new Schema({
item: { type: decoratorSchema, required: true }
});

class Shop {}
shopSchema.loadClass(Shop);
const shopModel = db.model('Test', shopSchema);

// Define another discriminated class after the model is compiled
class Deco2 extends Decorator { whoAmI() { return 'I am Test2'; }}
const deco2Schema = new Schema({}).loadClass(Deco2);
const deco2Schema = new Schema({});
deco2Schema.loadClass(Deco2);
decoratorSchema.discriminator('Test2', deco2Schema);

let instance = new shopModel({ item: { type: 'Test1' } });
assert.equal(instance.item.whoAmI(), 'I am Test1');

instance = new shopModel({ item: { type: 'Test2' } });
assert.equal(instance.item.whoAmI(), 'I am BaseDeco');

shopModel.recompileSchema();

let instance = new shopModel({ item: { type: 'Test1' } });
instance = new shopModel({ item: { type: 'Test1' } });
assert.equal(instance.item.whoAmI(), 'I am Test1');

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

0 comments on commit 1e8753f

Please sign in to comment.