Home Reference Source Test Repository
public class | source

Model

Model class.

Conceptually equivalent to the Illuminate\Database\Eloquent\Model class in Laravel.

Test:

Static Member Summary

Static Public Members
public static

events: {eventName: function[]}

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

all(columns: string | string[]): Promise

Fetch all models from this connection.

public static

boot(): void

Boot the model.

public static

create(attributes: Object): Promise

Save a new model and eventually return the instance.

public static

created(callback: Function): void

Register a 'created' event handler.

public static

creating(callback: Function): void

Register a 'creating' event handler.

public static

deleted(callback: Function): void

Register a 'deleted' event handler.

public static

deleting(callback: Function): void

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

saved(callback: Function): void

Register a 'saved' event handler.

public static

saving(callback: Function): void

Register a 'saving' event handler.

public static

setContainer(container: Container): void

Set the container instance to use for making related models.

public static

updated(callback: Function): void

Register a 'updated' event handler.

public static

updating(callback: Function): void

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(attributes: object): Model

Fill the model with an object of attributes.

public

getAttribute(key: string): *

Get the named attribute.

public

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

Get the name of the primary key column.

public

hydrate(items: Object[]): Model[]

Create a collection of models from plain objects.

public

isDate(column: string): Boolean

Check if a column is a date column.

public

load(relations: ...string): Promise

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(attributes: Object): Promise

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

public static events: {eventName: function[]} source

The registered event handlers for all models.

Static Protected Members

protected static booted: boolean source

Flag denoting whether or not this model has already booted.

protected static dates: string[] source

The fields which are date columns.

protected static relations: {relationName: relationFactory} source

Map of relation names to relation-factories.

protected static scopes: string[] source

The names of the scope methods for this model.

Static Public Methods

public static all(columns: string | string[]): Promise source

Fetch all models from this connection.

Params:

NameTypeAttributeDescription
columns string | string[]
  • optional

Return:

Promise

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:

NameTypeAttributeDescription
attributes Object

Return:

Promise

public static created(callback: Function): void source

Register a 'created' event handler.

Params:

NameTypeAttributeDescription
callback Function

Return:

void

public static creating(callback: Function): void source

Register a 'creating' event handler.

Params:

NameTypeAttributeDescription
callback Function

Return:

void

public static deleted(callback: Function): void source

Register a 'deleted' event handler.

Params:

NameTypeAttributeDescription
callback Function

Return:

void

public static deleting(callback: Function): void source

Register a 'deleting' event handler.

Params:

NameTypeAttributeDescription
callback Function

Return:

void

public static query(): Builder source

Get a new Eloquent query builder for this model.

Return:

Builder

public static registerEventHandler(name: string, handler: Function): void source

Register a handler for the named event.

Params:

NameTypeAttributeDescription
name string
handler Function

Return:

void

public static saved(callback: Function): void source

Register a 'saved' event handler.

Params:

NameTypeAttributeDescription
callback Function

Return:

void

public static saving(callback: Function): void source

Register a 'saving' event handler.

Params:

NameTypeAttributeDescription
callback Function

Return:

void

public static setContainer(container: Container): void source

Set the container instance to use for making related models.

Params:

NameTypeAttributeDescription
container Container

Return:

void

public static updated(callback: Function): void source

Register a 'updated' event handler.

Params:

NameTypeAttributeDescription
callback Function

Return:

void

public static updating(callback: Function): void source

Register a 'updating' event handler.

Params:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
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:

NameTypeAttributeDescription
attributes *

Protected Members

protected exists: boolean source

Flag denoting whether or not this model has been persisted.

protected original: Object source

The original attributes of this instance.

Public Methods

public bootIfNotBooted(): void source

Boot model if not already booted.

Return:

void

public delete(): Promise source

Delete the model.

Return:

Promise

Test:

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:

NameTypeAttributeDescription
attributes object

Return:

Model

Test:

public getAttribute(key: string): * source

Get the named attribute.

Params:

NameTypeAttributeDescription
key string

Return:

*

public getAttributes(): * source

Get all the attributes of this model.

Return:

*

Test:

public getDirty(): * source

Get the attributes which have changed since construction.

Return:

*

Test:

public getKey(): Number | undefined source

Get the primary key for this model.

Return:

Number | undefined

public getKeyName(): string source

Get the name of the primary key column.

Return:

string

public hydrate(items: Object[]): Model[] source

Create a collection of models from plain objects.

Params:

NameTypeAttributeDescription
items Object[]

Return:

Model[]

Test:

public isDate(column: string): Boolean source

Check if a column is a date column.

Params:

NameTypeAttributeDescription
column string

Return:

Boolean

public load(relations: ...string): Promise source

Eager load the relations.

Params:

NameTypeAttributeDescription
relations ...string

Return:

Promise

Test:

public newInstance(attributes: Object, exists: boolean): Model source

Create a new instance of the current model.

Params:

NameTypeAttributeDescription
attributes Object
exists boolean

Return:

Model

public newQuery(): Builder source

Get a new Eloquent query builder for this model.

Return:

Builder

Test:

public save(): Promise source

Save the model to the database.

Return:

Promise

Test:

public setAttribute(key: string, value: *): Model source

Set the named attribute.

Params:

NameTypeAttributeDescription
key string
value *

Return:

Model

public triggerEvent(name: string, halt: Boolean): void source

Trigger a model event.

Params:

NameTypeAttributeDescription
name string
halt Boolean

stop calling observers when one returns false

Return:

void

public update(attributes: Object): Promise source

Update the model.

Params:

NameTypeAttributeDescription
attributes Object

Return:

Promise

Test:

Protected Methods

protected _performInsert(): Promise source

Perform an insert operation.

Return:

Promise

protected _performUpdate(): Promise source

Perform an update operation.

Return:

Promise

protected _syncOriginal(): void source

Sync the original attributes with the current.

Return:

void