Skip to main content



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


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.


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


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


class Kitten {
public name?: string;

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