Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Model type definitions #1685

Merged
merged 1 commit into from
Jan 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT, the property name is always stored as the key in property map (name => definition).

type?: PropertyType;
id?: boolean | number;
}

/**
* 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