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`.
This commit is contained in:
parent
0ec951ee11
commit
81fc722b72
|
@ -19,8 +19,8 @@ export type PropertyType =
|
||||||
* Property definition
|
* Property definition
|
||||||
*/
|
*/
|
||||||
export interface PropertyDefinition extends AnyObject {
|
export interface PropertyDefinition extends AnyObject {
|
||||||
name: string;
|
|
||||||
type?: PropertyType;
|
type?: PropertyType;
|
||||||
|
id?: boolean | number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,8 +28,8 @@ export interface PropertyDefinition extends AnyObject {
|
||||||
*/
|
*/
|
||||||
export interface Schema {
|
export interface Schema {
|
||||||
name: string;
|
name: string;
|
||||||
properties: {[property: string]: PropertyDefinition};
|
properties: ModelProperties;
|
||||||
settings?: AnyObject;
|
settings?: ModelSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -53,21 +53,46 @@ export interface ColumnMetadata extends AnyObject {
|
||||||
name: string;
|
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
|
* Model definition
|
||||||
*/
|
*/
|
||||||
export declare class ModelDefinition extends EventEmitter implements Schema {
|
export declare class ModelDefinition extends EventEmitter implements Schema {
|
||||||
name: string;
|
name: string;
|
||||||
properties: AnyObject;
|
properties: ModelProperties;
|
||||||
rawProperties: AnyObject;
|
rawProperties: AnyObject;
|
||||||
settings?: AnyObject;
|
settings?: ModelSettings;
|
||||||
relations?: AnyObject[];
|
relations?: AnyObject[];
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
modelBuilder: ModelBuilder | null | undefined,
|
modelBuilder: ModelBuilder | null | undefined,
|
||||||
name: string,
|
name: string,
|
||||||
properties?: {[name: string]: PropertyDefinition},
|
properties?: ModelProperties,
|
||||||
settings?: AnyObject,
|
settings?: ModelSettings,
|
||||||
);
|
);
|
||||||
constructor(modelBuilder: ModelBuilder | null | undefined, schema: Schema);
|
constructor(modelBuilder: ModelBuilder | null | undefined, schema: Schema);
|
||||||
|
|
||||||
|
@ -96,6 +121,30 @@ export declare class ModelBase {
|
||||||
static dataSource?: DataSource;
|
static dataSource?: DataSource;
|
||||||
static modelName: string;
|
static modelName: string;
|
||||||
static definition: ModelDefinition;
|
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<ChildModel extends typeof ModelBase = typeof ModelBase>(
|
||||||
|
modelName: string,
|
||||||
|
properties?: ModelProperties,
|
||||||
|
settings?: ModelSettings,
|
||||||
|
): ChildModel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attach the model class to a data source
|
* Attach the model class to a data source
|
||||||
|
@ -203,7 +252,9 @@ export declare class ModelBuilder extends EventEmitter {
|
||||||
|
|
||||||
models: {[name: string]: ModelBaseClass};
|
models: {[name: string]: ModelBaseClass};
|
||||||
definitions: {[name: string]: ModelDefinition};
|
definitions: {[name: string]: ModelDefinition};
|
||||||
settings: AnyObject;
|
settings: ModelSettings;
|
||||||
|
|
||||||
|
defaultModelBaseClass: typeof ModelBase;
|
||||||
|
|
||||||
getModel(name: string, forceCreate?: boolean): ModelBaseClass;
|
getModel(name: string, forceCreate?: boolean): ModelBaseClass;
|
||||||
|
|
||||||
|
@ -211,8 +262,8 @@ export declare class ModelBuilder extends EventEmitter {
|
||||||
|
|
||||||
define(
|
define(
|
||||||
className: string,
|
className: string,
|
||||||
properties?: AnyObject,
|
properties?: ModelProperties,
|
||||||
settings?: AnyObject,
|
settings?: ModelSettings,
|
||||||
parent?: ModelBaseClass,
|
parent?: ModelBaseClass,
|
||||||
): ModelBaseClass;
|
): ModelBaseClass;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue