Skip to main content

Models with same name

This Guide shows all the possibilities for a model to use different names.

note

If Classes with the same name are wanted, these methods need to be used so that Typegoose can differentiate between them

info

Since 7.6.0 the name can also be changed in getModelForClass or buildSchema calls.

SchemaOptions collection

For this to work, { options: { automaticName } } must be true.

{ schemaOptions: { collection } } can be used to set a custom collection a model should use. Using { schemaOptions: { collection } } with automaticName automatically adds a suffix of the collection to the model's name.

Example without automaticName:

@modelOptions({ schemaOptions: { collection: 'Something' } })
class MultiModel {}

const model = getModelForClass(MultiModel);
expect(model.modelName).to.be.equal('MultiModel');

Example with automaticName:

@modelOptions({ schemaOptions: { collection: 'Something' }, options: { automaticName: true } })
class MultiModel {}

const model = getModelForClass(MultiModel);
expect(model.modelName).to.be.equal('MultiModel_Something');

Typegoose Custom Options "customName"

{ options: { customName } } can be used to set a custom model name.

Example:

@modelOptions({ options: { customName: 'CustomName' } })
class CustomNameOption {}

const model = getModelForClass(CustomNameOption);
expect(model.modelName).to.be.equal('CustomName');

If { options: { customName } } is used with { options: { automaticName: true } }, then it will be used as a suffix of the normal name.

Example:

@modelOptions({ options: { customName: 'CustomName', automaticName: true } })
class CustomNameOption {}

const model = getModelForClass(CustomNameOption);
expect(model.modelName).to.be.equal('CustomNameOption_CustomName');

Disable Caching

Since Typegoose 10.2.0 there is also the option of disabling the cache globally with setGlobalOptions's disableGlobalCaching or locally via @modelOptions.

import { modelOptions, getModelForClass, prop } from "@typegoose/typegoose";

@modelOptions({ options: { disableCache: true } })
class Kitten {
@prop()
public name: string;
}

const KittenModelDefault = getModelForClass(Kitten);
const KittenModelCon1 = getModelForClass(Kitten, { existingConnection: mongoose.createConnection() });

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

const KittenModel = getModelForClass(Kitten, { options: { disableCaching: true } });
assert.ok(!!KittenModel.schema.path('name'));
}
{
class Kitten {
@prop()
public nameTag: string;
}

const KittenModel = getModelForClass(Kitten, { existingConnection: mongoose.createConnection(), options: { disableCaching: true } }); // still requires being defined on a different connection / mongoose instance
assert.ok(!!KittenModel.schema.path('nameTag'));
}
note

Models still cannot be defined more than once in the same connection / mongoose instance.

caution

Setting the Cache to be disabled globally will make some functions that rely on it error, see E033 and disableGlobalCaching which effects it will have.

Notes

For more details on the usage of these naming features, please look into the tests that are written for them.

See also Typegoose's Name Generation for a complete (and hopefully simple) way to understand name generation.