Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define discriminator at subSchema level direcly in subschema #7971

Closed
xileftenurb opened this issue Jul 9, 2019 · 1 comment · Fixed by #11600
Closed

Define discriminator at subSchema level direcly in subschema #7971

xileftenurb opened this issue Jul 9, 2019 · 1 comment · Fixed by #11600
Assignees
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Milestone

Comments

@xileftenurb
Copy link

xileftenurb commented Jul 9, 2019

This is a feature request.

actually, following the documentation (and my research in the code),
you can do (taked from https://mongoosejs.com/docs/discriminators.html)

var eventSchema = new Schema({ message: String },
  { discriminatorKey: 'kind', _id: false });

var batchSchema = new Schema({ events: [eventSchema] });

// `batchSchema.path('events')` gets the mongoose `DocumentArray`
var docArray = batchSchema.path('events');

var clickedSchema = new Schema({
  element: {
    type: String,
    required: true
  }
}, { _id: false });
var Clicked = docArray.discriminator('Clicked', clickedSchema);

but if we change batchSchema to

var batchSchemav2 = new Schema({ events : [eventSchema], mainEvent : eventSchema})

now, you need to add both field to discriminator

batchSchemav2.path("events").discriminator('Clicked', clickedSchema);
batchSchemav2.path("mainEvent").discriminator('Clicked', clickedSchema);

this is event worse if "eventSchema" is used in many place, and have many discriminator, as you need to call all discriminator for each place eventSchema is used.

This would be far better if we could define discriminator direcly on eventSchema, as the discriminator would only be declared once for all schema who use "eventSchema".

from an api point of view, the best would be to be able to do

eventSchema.discriminator('Clicked', clickedSchema)

This api would also permit to create top-level schema-level discriminator.
This is not a breaking change, as it only add a possibility (a function to eventSchema), and do not have to break the other way of using discriminator. If used at the same time of other way, it can safely throw (as old code would not have this new function)

the capacity of using discriminator function at documentArray and Embedded level is still usefull, as it permit to not create a separate schema for embedded discrimination.

(As a side note, the documentation do not talk anywhere of the capacity of using discriminator on embedded subdocument in addition to documentArray. I had to check direcly in code to find it was possible)

version of program used :
mongoose : ^5.5.11
node : 11.7.0
mongodb : 4.0.0

@vkarpov15 vkarpov15 added this to the 5.x Unprioritized milestone Jul 11, 2019
@vkarpov15 vkarpov15 added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Jul 11, 2019
@vkarpov15
Copy link
Collaborator

Thanks for the suggestion, we will consider this feature for a future minor release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants