Class: Model

Model

The heart of an ORM, the data model.

The fields you specify to the Model will be used to generate a schema to the database, related property accessors, and possibly through models.

In each Session you instantiate from an ORM instance, you will receive a session-specific subclass of this Model. The methods you define here will be available to you in sessions.

An instance of Model represents a record in the database, though it is possible to generate multiple instances from the same record in the database.

To create data models in your schema, subclass Model. To define information about the data model, override static class methods. Define instance logic by defining prototype methods (without static keyword).

Constructor

new Model(props)

Creates a Model instance from it's properties. Don't use this to create a new record; Use the static method Model#create.

Parameters:
Name Type Description
props Object

the properties to instantiate with

Source:

Members

(static) idAttribute

Returns the id attribute of this Model.

Source:

ref

Returns a reference to the plain JS object in the store. Make sure to not mutate this.

Source:

Methods

(static) all() → {QuerySet}

Returns a QuerySet containing all Model instances.

Source:
Returns:

a QuerySet containing all Model instances

Type
QuerySet

(static) create(userProps) → {Model}

Creates a new record in the database, instantiates a Model and returns it.

If you pass values for many-to-many fields, instances are created on the through model as well.

Parameters:
Name Type Description
userProps props

the new Model's properties.

Source:
Returns:

a new Model instance.

Type
Model

(static) get(lookupObj) → {Model}

Gets the Model instance that matches properties in lookupObj. Throws an error if Model is not found, or multiple records match the properties.

Parameters:
Name Type Description
lookupObj Object

the properties used to match a single entity.

Source:
Returns:

a Model instance that matches lookupObj properties.

Type
Model

(static) hasId(id) → {Boolean}

Returns a boolean indicating if an entity with the id id exists in the state.

Parameters:
Name Type Description
id *

a value corresponding to the id attribute of the Model class.

Source:
Returns:

a boolean indicating if entity with id exists in the state

Type
Boolean

(static) options() → {Object}

Returns the options object passed to the database for the table that represents this Model class.

Returns an empty object by default, which means the database will use default options. You can either override this function to return the options you want to use, or assign the options object as a static property of the same name to the Model class.

Source:
Returns:

the options object passed to the database for the table representing this Model class.

Type
Object

(static) upsert(userProps) → {Model}

Creates a new or update existing record in the database, instantiates a Model and returns it.

If you pass values for many-to-many fields, instances are created on the through model as well.

Parameters:
Name Type Description
userProps props

the required Model's properties.

Source:
Returns:

a Model instance.

Type
Model

(static) withId(id) → {Model}

Returns a Model instance for the object with id id. This throws if the id doesn't exist. Use Model#hasId to check for existence first if you're not certain.

Parameters:
Name Type Description
id *

the id of the object to get

Source:
Throws:

If object with id id doesn't exist

Returns:

Model instance with id id

Type
Model

_refreshMany2Many(relations)

Update many-many relations for model.

Parameters:
Name Type Description
relations
Source:

delete() → {undefined}

Deletes the record for this Model instance. You'll still be able to access fields and values on the instance.

Source:
Returns:
Type
undefined

equals(otherModel) → {Boolean}

Returns a boolean indicating if otherModel equals this Model instance. Equality is determined by shallow comparing their attributes.

Parameters:
Name Type Description
otherModel Model

a Model instance to compare

Source:
Returns:

a boolean indicating if the Model instance's are equal.

Type
Boolean

getClass() → {Model}

Gets the Model class or subclass constructor (the class that instantiated this instance).

Source:
Returns:

The Model class or subclass constructor used to instantiate this instance.

Type
Model

getId() → {*}

Gets the id value of the current instance by looking up the id attribute.

Source:
Returns:

The id value of the current instance.

Type
*

refreshFromState() → {undefined}

Updates Model instance attributes to reflect the database state in the current session.

Source:
Returns:
Type
undefined

set(propertyName, value) → {undefined}

Updates a property name to given value for this Model instance. The values are immediately committed to the database.

Parameters:
Name Type Description
propertyName string

name of the property to set

value *

value assigned to the property

Source:
Returns:
Type
undefined

toString() → {string}

Returns a string representation of the Model instance.

Source:
Returns:

A string representation of this Model instance.

Type
string

update(userMergeObj) → {undefined}

Assigns multiple fields and corresponding values to this Model instance. The updates are immediately committed to the database.

Parameters:
Name Type Description
userMergeObj Object

an object that will be merged with this instance.

Source:
Returns:
Type
undefined