Discriminators

Discriminators are a schema inheritance mechanism. They enable you to have multiple models with overlapping schemas on top of the same underlying MongoDB collection.

const eventSchema = new Schema({
    type: Type.String(),
    time: Type.Date()
});

class Event extends Model(eventSchema, connection, 'Event') {
    get summary() {
        return `${this.time}: event ${this.type}`;
    }
}
const visitEventSchema = new Schema({
    url: Type.String()
});

class VisitEvent extends Event.discriminate({ type: 'visit' }, visitEventSchema) {
    get summary() {
        return `${this.time}: a click on ${this.url}`;
    }
}

Last updated