Skip to main content

getClass

Typings:

function getClass(
input:
| mongoose.Document
| { typegooseName: () => string }
| { typegooseName: string }
| string
| any
): NewableFunction | undefined

Parameters:

NameTypeDescription
input RequiredanyA Input to get a name from

getClass is used to get a Class from a variety of inputs, which include:

  • mongoose.Document; Get the name to look-up from doc.constructor.modelName, only applies if no typegooseName function or property is present
  • { typegooseName: () => string }: Get the name to look-up from the typegooseName function (currently automatically added by typegoose)
  • { typegooseName: string }: Get the name to look-up from the typegooseName getter / property.
  • string: Directly specify the name to look-up.
  • any: Try to get a Class from any of the above, without having proper types.
caution

This look-up only works if the class has been correctly registered with addModelToTypegoose (automatically done when calling getModelForClass or getDiscriminatorModelForClass, but not for buildSchema).

caution

Will throw a Error when caching is disabled E033.

tip

Any class that got compiled with buildSchema (transparently used by getModelForClass or getDiscriminatorModelForClass) a typegooseName function gets automatically added. This even makes it possible to get the class from sub-documents / sub-classes.

Example‚Äč

// Nested-GetClass & Document-GetClass
class Food {
@prop()
public name: string;
}

class Kitten {
@prop()
public currentFood: Food;
}

const KittenModel = getModelForClass(Kitten);

const input = new KittenModel();

// Nested
getClass(input.currentFood) === Food; // should be "true"
// Document
getClass(input) === Kitten; // should be "true"
// by string
getClass('Kitten') == Kitten; // should be "true"