Page Contents

Home > @loopback/repository > defineModelClass

defineModelClass() function

Create (define) a new model class with the given name and definition.

Signature:

export declare function defineModelClass<BaseCtor extends typeof Model, Props extends object = {}>(base: BaseCtor, definition: ModelDefinition): DynamicModelCtor<BaseCtor, Props>;

Parameters

Parameter Type Description
base BaseCtor The base model to extend, typically Model or Entity. You can also use your own base class, e.g. User.
definition ModelDefinition Definition of the model to create.

Returns:

DynamicModelCtor<BaseCtor, Props>

Remarks

const Product = defineModelClass(Entity, new ModelDefinition('Product'));

To enable type safety, you should describe properties of your model:

const Product = defineModelClass<
 typeof Entity,
 {id: number, name: string}
>(Entity, new ModelDefinition('Product'));

If your model allows arbitrary (free-form) properties, then add AnyObject to the type describing model properties.

const Product = defineModelClass<
 typeof Entity,
 AnyObject & {id: number},
>(Entity, new ModelDefinition('Product'));