From 81fc722b72beebb73fc9716abb84728834253e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Thu, 24 Jan 2019 09:39:50 +0100 Subject: [PATCH] Improve Model type definitions Define two new types: ModelSettings and ModelProperties to describe the objects describing model properties and settings. Add static property `base` and static method `extend` to the definition of `ModelBase` class. Add property `defaultModelBaseClass` to `ModelBuilder` class definition. Fix `PropertyDefinition` interface: remove `name`, add optional `id`. --- types/model.d.ts | 71 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/types/model.d.ts b/types/model.d.ts index 6e525071..f16da3fe 100644 --- a/types/model.d.ts +++ b/types/model.d.ts @@ -19,8 +19,8 @@ export type PropertyType = * Property definition */ export interface PropertyDefinition extends AnyObject { - name: string; type?: PropertyType; + id?: boolean | number; } /** @@ -28,8 +28,8 @@ export interface PropertyDefinition extends AnyObject { */ export interface Schema { name: string; - properties: {[property: string]: PropertyDefinition}; - settings?: AnyObject; + properties: ModelProperties; + settings?: ModelSettings; } /** @@ -53,21 +53,46 @@ export interface ColumnMetadata extends AnyObject { name: string; } +/** + * Definition of model properties, for example + * ```ts + * { + * name: {type: String, required: true}, + * } + * ``` + */ +export interface ModelProperties { + [name: string]: PropertyDefinition +} + +/** + * Model settings, for example + * ```ts + * { + * strict: true, + * } + * ``` + */ +export interface ModelSettings extends AnyObject { + strict?: boolean; + forceId?: boolean; +} + /** * Model definition */ export declare class ModelDefinition extends EventEmitter implements Schema { name: string; - properties: AnyObject; + properties: ModelProperties; rawProperties: AnyObject; - settings?: AnyObject; + settings?: ModelSettings; relations?: AnyObject[]; constructor( modelBuilder: ModelBuilder | null | undefined, name: string, - properties?: {[name: string]: PropertyDefinition}, - settings?: AnyObject, + properties?: ModelProperties, + settings?: ModelSettings, ); constructor(modelBuilder: ModelBuilder | null | undefined, schema: Schema); @@ -96,6 +121,30 @@ export declare class ModelBase { static dataSource?: DataSource; static modelName: string; static definition: ModelDefinition; + static readonly base: typeof ModelBase; + + /** + * Extend the model with the specified model, properties, and other settings. + * For example, to extend an existing model: + * + * ```js + * const Customer = User.extend('Customer', { + * accountId: String, + * vip: Boolean + * }); + * ``` + * + * @param className Name of the new model being defined. + * @param subClassProperties child model properties, added to base model + * properties. + * @param subClassSettings child model settings such as relations and acls, + * merged with base model settings. + */ + static extend( + modelName: string, + properties?: ModelProperties, + settings?: ModelSettings, + ): ChildModel; /** * Attach the model class to a data source @@ -203,7 +252,9 @@ export declare class ModelBuilder extends EventEmitter { models: {[name: string]: ModelBaseClass}; definitions: {[name: string]: ModelDefinition}; - settings: AnyObject; + settings: ModelSettings; + + defaultModelBaseClass: typeof ModelBase; getModel(name: string, forceCreate?: boolean): ModelBaseClass; @@ -211,8 +262,8 @@ export declare class ModelBuilder extends EventEmitter { define( className: string, - properties?: AnyObject, - settings?: AnyObject, + properties?: ModelProperties, + settings?: ModelSettings, parent?: ModelBaseClass, ): ModelBaseClass;