Background
Discriminators are a schema inheritance mechanism. They enable you to have multiple models with overlapping schemas on top of the same underlying MongoDB collection.
Current egg-mongoose use the function loadModelToApp
to load Models to app.model. However, during loading, I can't get the base Model at another Model.
for instance, I have app/model/Event.js as a base model, and app/model/ClickedLinkEvent.js as another discriminator.
app/model/Event.js
var eventSchema = new mongoose.Schema({time: Date}, options);
return mongoose.model('Event', eventSchema);
app/model/ClickedLinkEvent.js
// I can't get the Event Model here
const { Event } = app.model;
var ClickedLinkEvent = Event.discriminator('ClickedLink',
new mongoose.Schema({url: String}, options));
Proposal
find a way to support mongoose discriminator
1条答案
按热度按时间oogrdqng1#
My workaround for this for now is to put the discriminators in the same file as the base model.
If you want to have the discriminators in separate files, you can put the register discriminator handle in another file, import it and pass Event model to it.
Example
Then I can create it like this or fetch the discriminator from
this.app.model.Event.discriminators
If anyone have any better alternatives, please do share.