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:
Name | Type | Description |
---|---|---|
from Required | mongoose.Model | The Model to register a discriminator on |
cl Required | U | The Class to build into a Model |
options | IModelOptions | Overwrite 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:
Name | Type | Description |
---|---|---|
from Required | mongoose.Model | The Model to register a discriminator on |
cl Required | U | The Class to build into a Model |
value | IModelOptions | Overwrite 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:
Name | Type | Description |
---|---|---|
from Required | mongoose.Model | The Model to register a discriminator on |
cl Required | U | The Class to build into a Model |
value | IModelOptions | Overwrite the discrimiantor value to register by |
options | IModelOptions | Overwrite 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);
Notes
ModelOption disablePluginsOnDiscriminator
many need to be set to not get duplicate plugins / plugin hooks.
This will not be neccessary for typegoose 9.13.0 and higher.
Example:
function pluginFn(schema) {
schema.pre('save', function hookTestTimesNonGlobal() {});
}
@plugin(pluginFn)
@modelOptions({ options: { disablePluginsOnDiscriminator: true } })
class DisBase {
@prop()
public dummy?: string;
}
const DisBaseModel = getModelForClass(DisBase);
class Dis1 extends DisBase {
@prop()
public dummy2?: string;
}
const Dis1Model = getDiscriminatorModelForClass(DisBaseModel, Dis1);
// should only contain "hookTestTimesNonGlobal" once
// if "disablePluginsOnDiscriminator" is falsy, this will otherwise result in duplicates
console.log('Dis1Model save hooks', (Dis1Model.schema as any).s.hooks._pres.get('save'));