Page Contents

Home > @loopback/repository > defineModelClass

defineModelClass() function

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

Example usage:

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

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  
definition ModelDefinition  

Returns:

DynamicModelCtor<BaseCtor, Props>