Skip to content

Commit

Permalink
Improve Model type definitions
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
bajtos committed Jan 24, 2019
1 parent 0ec951e commit 102ca40
Showing 1 changed file with 61 additions and 10 deletions.
71 changes: 61 additions & 10 deletions types/model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ export type PropertyType =
* Property definition
*/
export interface PropertyDefinition extends AnyObject {
name: string;
type?: PropertyType;
id?: boolean;
}

/**
* Schema definition
*/
export interface Schema {
name: string;
properties: {[property: string]: PropertyDefinition};
settings?: AnyObject;
properties: ModelProperties;
settings?: ModelSettings;
}

/**
Expand All @@ -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);

Expand Down Expand Up @@ -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<ChildModel extends typeof ModelBase = typeof ModelBase>(
modelName: string,
properties?: ModelProperties,
settings?: ModelSettings,
): ChildModel;

/**
* Attach the model class to a data source
Expand Down Expand Up @@ -203,16 +252,18 @@ 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;

getModelDefinition(name: string): ModelDefinition | undefined;

define(
className: string,
properties?: AnyObject,
settings?: AnyObject,
properties?: ModelProperties,
settings?: ModelSettings,
parent?: ModelBaseClass,
): ModelBaseClass;

Expand Down

0 comments on commit 102ca40

Please sign in to comment.