Skip to main content

getDiscriminatorModelForClass

Overload 1

Typings:

function getDiscriminatorModelForClass<U extends AnyParamConstructor<any>, QueryHelpers = BeAnObject>(
from: mongoose.Model<any, any>,
cl: U,
options?: IModelOptions
): ReturnModelType<U, QueryHelpers>;

Parameters:

NameTypeDescription
from Requiredmongoose.ModelThe Model to register a discriminator on
cl RequiredUThe Class to build into a Model
optionsIModelOptionsOverwrite some Model options, only property schemaOptions is merged with the existing options

Overload 2

Typings:

function getDiscriminatorModelForClass<U extends AnyParamConstructor<any>, QueryHelpers = BeAnObject>(
from: mongoose.Model<any, any>,
cl: U,
value?: string
): ReturnModelType<U, QueryHelpers>;

Parameters:

NameTypeDescription
from Requiredmongoose.ModelThe Model to register a discriminator on
cl RequiredUThe Class to build into a Model
valueIModelOptionsOverwrite the discrimiantor value to register by

Overload 3

Typings:

function getDiscriminatorModelForClass<U extends AnyParamConstructor<any>, QueryHelpers = BeAnObject>(
from: mongoose.Model<any, any>,
cl: U,
value?: string,
options?: IModelOptions
): ReturnModelType<U, QueryHelpers>;

Parameters:

NameTypeDescription
from Requiredmongoose.ModelThe Model to register a discriminator on
cl RequiredUThe Class to build into a Model
valueIModelOptionsOverwrite the discrimiantor value to register by
optionsIModelOptionsOverwrite some Model options, only property schemaOptions is merged with the existing options

Description

getDiscriminatorModelForClass is used to compile a given Class (cl) into a Model and register it as a discriminator on a given Model (from).

Option value is to overwrite the key the class is registered on as a discriminator, by default it is the generated model name, but can be overwritten with any string, recommended is to use a string-enum to keep track of names.

note

Note that existingConnection and existingMongoose will not be used and instead will be registered on the from model's settings.
See Warning W002.

Example

// The Base Class
class Event {
@prop({ required: true })
public name!: string;
}

// A Discriminator Class Variant
class ClickEvent extends Event {
@prop({ required: true, default: 0 })
public timesClicked!: number;
}

const EventModel = getModelForClass(Event);
const ClickEventModel = getDiscriminatorModelForClass(EventModel, ClickEvent);