These are chat archives for Automattic/mongoose

23rd
Jan 2018
Remy Loubradou
@lbdremy
Jan 23 2018 17:42
Hi
Anyone in the room ?
Kev
@lineus
Jan 23 2018 17:45
what's up @lbdremy ?
Remy Loubradou
@lbdremy
Jan 23 2018 17:46
Maybe someone can help, I want to use the discriminator feature, but I want my Discrimator Model and the discriminator value stored in the discriminator key to be different. For example I have a model Event, and a model ClickEvent with the discriminator key being "kind" for an instance of the ClickEvent model I want { _id: "", kind: "click" } instead of { _id: "", kind: "ClickEvent" }
it is possible ?
hey @lineus :)
Kev
@lineus
Jan 23 2018 18:03
@lbdremy I think you can set kind or whichever field you designate as the discriminator key to any string you like. let me test that theory real quick :)
Kev
@lineus
Jan 23 2018 18:25
@lbdremy this works for me: gist is that what you mean?
Remy Loubradou
@lbdremy
Jan 23 2018 18:49
no sorry, if I use my previous example with the model ClickEvent derived from Event with the discriminator key being type. I want to be able to do mongoose.model('ClickEvent') and the type is set with click and not ClickEvent for new document created with this model (ClickEvent). So I guess, I need to indicate somewhere when doing Event.discriminator('ClickEvent', clientEventSchema) that the discriminatorValue is click not the name of the Model ClickEvent.
Kev
@lineus
Jan 23 2018 19:00
just curious, why do they need to be different?
Remy Loubradou
@lbdremy
Jan 23 2018 19:08
not really consistent (case, too much linked with Mongoose behavior) with all the different enum I expose on my API, I could transform this enum with on the toJSON or toObject with the transform option but I would prefer to do this with something like discriminatorValue on the schema options for each derived Model.
in my case I don't care storing a string but it would be nice to be able to use a number for the type of the discriminator
Julian Claus
@ndabAP
Jan 23 2018 20:21
With the release of Mongoose 5, all my find queries don't work anymore. I would need to add .exec() to every query. Is there a workaround for this? Thanks
Juha Lindstedt
@pakastin
Jan 23 2018 20:23
I’ve used .exec() so long I can’t remember how I used to do without 🙂
Julian Claus
@ndabAP
Jan 23 2018 20:23
:D
Juha Lindstedt
@pakastin
Jan 23 2018 20:23
Oh, it was the callback function after query right?
I didn’t know it stopped working
Julian Claus
@ndabAP
Jan 23 2018 20:24
Yeah, I think so
I'm using async await to query
Juha Lindstedt
@pakastin
Jan 23 2018 20:24
However I usually always have .select, .lean, etc..
You can do that with .exec as well..
async/await I mean
Julian Claus
@ndabAP
Jan 23 2018 20:25
I know, but I have to add it to all my queries and it would make my code unnecessarily long
Juha Lindstedt
@pakastin
Jan 23 2018 20:25
It’s only one line 🙂
You could write a wrapper for it though?
Julian Claus
@ndabAP
Jan 23 2018 20:26
But for many queries ;) But you don't know a way, right? I thought a could use mongoose.Promise = global.Promise or something like that
I use a repository, so it wouldn't be too much work. I just thought there is another way ;)
Juha Lindstedt
@pakastin
Jan 23 2018 20:27
I just use exec 😎
Julian Claus
@ndabAP
Jan 23 2018 20:28
RIght, I guess I have to add it. Thanks a lot for taking the time