Skip to main content

addModelToTypegoose

Typings:

function addModelToTypegoose<U extends AnyParamConstructor<any>, QueryHelpers = BeAnObject>(
model: mongoose.Model<any>,
cl: U,
options?: { existingMongoose?: mongoose.Mongoose; existingConnection?: any }
)

Parameters:

NameTypeDescription
modelmongoose.ModelThe Model to add to the Class mapping
cl RequiredUThe Class to add to the mapping
optionsIModelOptionsOverwrite which existingMongoose and existingConnection the Class-Model mapping is on

addModelToTypegoose is used to add a the Class (cl) and Model (model) to the typegoose cache.
This function also returns the input Model (model) with the typegoose typings.

This cache is used for functions like [getClass](/typegoose/docs/api/functions/get-class] to find a class by the name mapping.
This function gets automatically called by functions like getModelForClass and getDiscriminatorModelForClass.

tip

For a full example with buildSchema and addModelToTypegoose see Manual Schema Modification.

note

This function will basically not do much if Caching is disabled.
It will still do checks that the passed model and class are valid.

Example‚Äč

class Kitten {
@prop()
public name?: string;
}

const kittenSchema = buildSchema(Kitten);
const KittenModel = addModelToTypegoose(mongoose.model('Kitten', kittenSchema), Kitten);
// "KittenModel" is now a valid Typegoose model