Model
Model class.
Conceptually equivalent to the Illuminate\Database\Eloquent\Model class in Laravel.
Test:
Static Member Summary
Static Public Members | ||
public static |
The registered event handlers for all models. |
Static Protected Members | ||
protected static |
Flag denoting whether or not this model has already booted. |
|
protected static |
The fields which are date columns. |
|
protected static |
relations: {relationName: relationFactory} Map of relation names to relation-factories. |
|
protected static |
The names of the scope methods for this model. |
Static Method Summary
Static Public Methods | ||
public static |
Fetch all models from this connection. |
|
public static |
boot(): void Boot the model. |
|
public static |
Save a new model and eventually return the instance. |
|
public static |
Register a 'created' event handler. |
|
public static |
Register a 'creating' event handler. |
|
public static |
Register a 'deleted' event handler. |
|
public static |
Register a 'deleting' event handler. |
|
public static |
Get a new Eloquent query builder for this model. |
|
public static |
registerEventHandler(name: string, handler: Function): void Register a handler for the named event. |
|
public static |
Register a 'saved' event handler. |
|
public static |
Register a 'saving' event handler. |
|
public static |
setContainer(container: Container): void Set the container instance to use for making related models. |
|
public static |
Register a 'updated' event handler. |
|
public static |
Register a 'updating' event handler. |
Static Protected Methods | ||
protected static |
_bootBaseModel(): void Boot the base model class. |
|
protected static |
_bootScopes(scopes: string[]): void Boot scopes for this model. |
|
protected static |
_bootSelf(): void Boot the current class. |
Constructor Summary
Public Constructor | ||
public |
constructor(attributes: *) Create a new Model instance. |
Member Summary
Protected Members | ||
protected |
Flag denoting whether or not this model has been persisted. |
|
protected |
The original attributes of this instance. |
Method Summary
Public Methods | ||
public |
bootIfNotBooted(): void Boot model if not already booted. |
|
public |
Delete the model. |
|
public |
Fill the model with an object of attributes. |
|
public |
getAttribute(key: string): * Get the named attribute. |
|
public |
getAttributes(): * Get all the attributes of this model. |
|
public |
getDirty(): * Get the attributes which have changed since construction. |
|
public |
Get the primary key for this model. |
|
public |
getKeyName(): string Get the name of the primary key column. |
|
public |
Create a collection of models from plain objects. |
|
public |
Check if a column is a date column. |
|
public |
Eager load the relations. |
|
public |
newInstance(attributes: Object, exists: boolean): Model Create a new instance of the current model. |
|
public |
Get a new Eloquent query builder for this model. |
|
public |
Save the model to the database. |
|
public |
setAttribute(key: string, value: *): Model Set the named attribute. |
|
public |
triggerEvent(name: string, halt: Boolean): void Trigger a model event. |
|
public |
Update the model. |
Protected Methods | ||
protected |
Perform an insert operation. |
|
protected |
Perform an update operation. |
|
protected |
_syncOriginal(): void Sync the original attributes with the current. |
Static Public Members
Static Protected Members
protected static relations: {relationName: relationFactory} source
Map of relation names to relation-factories.
Static Public Methods
public static all(columns: string | string[]): Promise source
Fetch all models from this connection.
public static boot(): void source
Boot the model.
Booting lets us defer much of the setup for using EloquentJs until it's actually needed. This means we can load a single build of EloquentJs on every page and have access to all our models, with minimal impact on performance.
Return:
void |
public static create(attributes: Object): Promise source
Save a new model and eventually return the instance.
Params:
Name | Type | Attribute | Description |
attributes | Object |
public static created(callback: Function): void source
Register a 'created' event handler.
Params:
Name | Type | Attribute | Description |
callback | Function |
Return:
void |
public static creating(callback: Function): void source
Register a 'creating' event handler.
Params:
Name | Type | Attribute | Description |
callback | Function |
Return:
void |
public static deleted(callback: Function): void source
Register a 'deleted' event handler.
Params:
Name | Type | Attribute | Description |
callback | Function |
Return:
void |
public static deleting(callback: Function): void source
Register a 'deleting' event handler.
Params:
Name | Type | Attribute | Description |
callback | Function |
Return:
void |
public static registerEventHandler(name: string, handler: Function): void source
Register a handler for the named event.
Return:
void |
public static saved(callback: Function): void source
Register a 'saved' event handler.
Params:
Name | Type | Attribute | Description |
callback | Function |
Return:
void |
public static saving(callback: Function): void source
Register a 'saving' event handler.
Params:
Name | Type | Attribute | Description |
callback | Function |
Return:
void |
public static setContainer(container: Container): void source
Set the container instance to use for making related models.
Params:
Name | Type | Attribute | Description |
container | Container |
Return:
void |
public static updated(callback: Function): void source
Register a 'updated' event handler.
Params:
Name | Type | Attribute | Description |
callback | Function |
Return:
void |
public static updating(callback: Function): void source
Register a 'updating' event handler.
Params:
Name | Type | Attribute | Description |
callback | Function |
Return:
void |
Static Protected Methods
protected static _bootBaseModel(): void source
Boot the base model class.
This is where we can set up functionality that's common to all models, and only needs to happen once regardless of how many child models are used.
Return:
void |
protected static _bootScopes(scopes: string[]): void source
Boot scopes for this model.
Scopes are provided as a simple array since all we want to do is keep track of their calls in the query stack. Here we can add those named scopes as methods on our prototype, ensuring consistency with the Laravel API.
Params:
Name | Type | Attribute | Description |
scopes | string[] |
Return:
void |
protected static _bootSelf(): void source
Boot the current class.
This happens once per model, and is where we can take
any configuration values attached as properties of the
constructor (which is the this
in a static ES6 class
method, incidentally) and adjust our prototype as needed.
Return:
void |
Public Constructors
public constructor(attributes: *) source
Create a new Model instance.
Params:
Name | Type | Attribute | Description |
attributes | * |
Protected Members
Public Methods
public fill(attributes: object): Model source
Fill the model with an object of attributes.
This is where Laravel would guard against mass assignment. While it would be possible to implement similar functionality here, the extra complexity it'd introduce doesn't seem worth it, at least for now...
Params:
Name | Type | Attribute | Description |
attributes | object |
public getAttribute(key: string): * source
Get the named attribute.
Params:
Name | Type | Attribute | Description |
key | string |
Return:
* |
public hydrate(items: Object[]): Model[] source
Create a collection of models from plain objects.
Params:
Name | Type | Attribute | Description |
items | Object[] |
Test:
public isDate(column: string): Boolean source
Check if a column is a date column.
Params:
Name | Type | Attribute | Description |
column | string |
public load(relations: ...string): Promise source
Eager load the relations.
Params:
Name | Type | Attribute | Description |
relations | ...string |
public newInstance(attributes: Object, exists: boolean): Model source
Create a new instance of the current model.
public setAttribute(key: string, value: *): Model source
Set the named attribute.
Params:
Name | Type | Attribute | Description |
key | string | ||
value | * |
public update(attributes: Object): Promise source
Update the model.
Params:
Name | Type | Attribute | Description |
attributes | Object |