Definitelytyped: @types/mongoose - Schema.path() does not contain discriminator

Created on 21 Nov 2018  Â·  13Comments  Â·  Source: DefinitelyTyped/DefinitelyTyped

I am at the moment using the discriminator in mongoose on a Schema.path() which returns SchemaType.
This is how it should work

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

var batchSchema = new Schema({ events: [eventSchema] });
batchSchema.path('events').discriminator('Clicked', new Schema({
  element: String
}, { _id: false }));
batchSchema.path('events').discriminator('Purchased', new Schema({
  product: String
}, { _id: false }));

However it works just like this

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

var batchSchema = new Schema({ events: [eventSchema] });
const batchPath = batchSchema.path('events') as any;
batchPath .discriminator('Clicked', new Schema({
  element: String
}, { _id: false }));
batchPath .discriminator('Purchased', new Schema({
  product: String
}, { _id: false }));

I guess its just missing in the types definition. Current versions are

TypeScript: 3.1.6
Mongoose: 5.3.13
@types/mongoose: 5.3.1 (Authors @horiuchi @sindrenm @lukasz-zak @Alorel )

If you know how to fix the issue, make a pull request instead.

  • [x] I tried using the @types/xxxx package and had problems.
  • [x] I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • [x] I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • [x] [Mention](https://github.com/blog/821-mention-somebody-they-re-notified) the authors (see Definitions by: in index.d.ts) so they can respond.

    • Authors: @....

If you do not mention the authors the issue will be ignored.

All 13 comments

I am running over the same issue, discriminator is missing in the version: "@types/mongoose": "5.3.5"

Same here. Anyone has a fix in mind?
@horiuchi @lukasz-zak @Alorel

Not sure to understand with @relief-melone code example. According mongoose documentation and code, discriminator method is attached to the model object.

I'm missing these as well and look forward to a fix. Why was the PR closed?

@emmanuelgautier There's a discriminator function for embedded documents in arrays as well:

@qqilihq The PR was automaticaly closed because of inactivity.

According mongoose code, discriminator is attached to DocumentArray not SchemaType in this examples. Please make another PR with the discriminator on DocumentArray instead.

@emmanuelgautier Thanks for the clarification. I created a new PR.

My PR above was merged, so I feel this can be closed.

My usage pattern:

const itemArrayType = schema.path('items') as mongoose.Schema.Types.DocumentArray;
itemArrayType.discriminator('foo', new mongoose.Schema({ … }));
itemArrayType.discriminator('bar', new mongoose.Schema({ … }));
itemArrayType.discriminator('baz', new mongoose.Schema({ … }));

@qqilihq If it isn't too much work for you, can you please add a type for embedded documents as well? e.g. if events was an object instead of array in example above.

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

I have checked and Mongoose does support it. See SingleNestedPath in mongoose. Also see this https://thecodebarbarian.com/mongoose-4.12-single-embedded-discriminators.html

I don't mean to put off work on you but I have never contributed to this repo and I don't have time to get everything setup right now.

@bbhopesh I currently do not have time to do this myself, but this shouldn't be a biggie and a good opportunity to get into DT.

Just have a look at my change set back then: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/33014/files

Happy to review your PR.

Okay.. I will do it when as I get some time. I will have to add a new class though because there is no typescript class for SingleNestedPath today.

So is it possible to use discriminators with typescript and mongoose?

@bbhopesh Is it done then? Wanted to use it in NestJs with mongoose..

@qqilihq What do you think about this attempt? I am not familiar with the dt syntax... The big change is that Schema.path() returns SingleNestedPath instead of SchemaType (where SingleNestedPath extends SchemaType (which seems to be the case per code)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tyv picture tyv  Â·  3Comments

csharpner picture csharpner  Â·  3Comments

JWT
svipas picture svipas  Â·  3Comments

victor-guoyu picture victor-guoyu  Â·  3Comments

variousauthors picture variousauthors  Â·  3Comments