Page Contents

Home > @loopback/repository > defineModelClass

defineModelClass() function

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


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


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.


DynamicModelCtor<BaseCtor, Props>


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'));