Skip to content

Commit

Permalink
Merge branch '6.2' of github.com:Automattic/mongoose into 6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 24, 2022
2 parents 2763a63 + bb51b1a commit beea8c4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/helpers/model/discriminator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const CUSTOMIZABLE_DISCRIMINATOR_OPTIONS = {
*/

module.exports = function discriminator(model, name, schema, tiedValue, applyPlugins) {

if (!(schema && schema.instanceOfSchema)) {
throw new Error('You must pass a valid discriminator Schema');
}
Expand Down Expand Up @@ -201,7 +202,7 @@ module.exports = function discriminator(model, name, schema, tiedValue, applyPlu

model.schema.discriminators[name] = schema;

if (model.discriminators[name]) {
if (model.discriminators[name] && !schema.options.overwriteModels) {
throw new Error('Discriminator with name "' + name + '" already exists');
}

Expand Down
4 changes: 3 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1134,11 +1134,13 @@ Model.exists = function exists(filter, options, callback) {
* @param {Object|String} [options] If string, same as `options.value`.
* @param {String} [options.value] the string stored in the `discriminatorKey` property. If not specified, Mongoose uses the `name` parameter.
* @param {Boolean} [options.clone=true] By default, `discriminator()` clones the given `schema`. Set to `false` to skip cloning.
* @param {Boolean} [options.overwriteModels=false] by default, Mongoose does not allow you to define a discriminator with the same name as another discriminator. Set this to allow overwriting discriminators with the same name.
* @return {Model} The newly created discriminator model
* @api public
*/

Model.discriminator = function(name, schema, options) {

let model;
if (typeof name === 'function') {
model = name;
Expand All @@ -1162,7 +1164,7 @@ Model.discriminator = function(name, schema, options) {
}

schema = discriminator(this, name, schema, value, true);
if (this.db.models[name]) {
if (this.db.models[name] && !schema.options.overwriteModels) {
throw new OverwriteModelError(name);
}

Expand Down
20 changes: 20 additions & 0 deletions test/model.discriminator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1867,4 +1867,24 @@ describe('model', function() {
assert.equal(res.c.gc.gc11, 'eleventy');
assert.equal(res.c.gc.gc12, 110);
});
it('Should allow reusing discriminators (gh-10931)', async function() {
const options = { discriminatorKey: 'kind', overwriteModels: true };
const eventSchema = new mongoose.Schema({ time: Date }, options);
const Event = db.model('Event', eventSchema);

const ClickedLinkEvent = Event.discriminator('ClickedLink',
new mongoose.Schema({ url: String }, options));
const reUse = Event.discriminator('ClickedLink', new mongoose.Schema({ url: String }, options));
assert.ok(reUse);


const genericEvent = new Event({ time: Date.now(), url: 'google.com' });
assert.ok(!genericEvent.url);

const clickedEvent = new ClickedLinkEvent({ time: Date.now(), url: 'google.com' });
assert.ok(clickedEvent.url);

const reUseEvent = new reUse({ time: Date.now(), url: 'test.com' });
assert.ok(reUseEvent.url);
});
});

0 comments on commit beea8c4

Please sign in to comment.