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](Session) you instantiate from an [ORM](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](#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](#Model). To define
information about the data model, override static class methods. Define instance
logic by defining prototype methods (without static
keyword).
Kind: global class
- Model
new Model(props)
- instance
ref
⇒ ObjectgetClass()
⇒ ModelgetId()
⇒ *toString()
⇒ stringequals(otherModel)
⇒ Booleanset(propertyName, value)
⇒ undefinedupdate(userMergeObj)
⇒ undefinedrefreshFromState()
⇒ undefineddelete()
⇒ undefinedgetNextState()
- static
idAttribute
⇒ stringquery
options()
⇒ ObjectmarkAccessed(ids)
⇒ undefinedmarkFullTableScanned()
⇒ undefinedmarkAccessedIndexes(indexes)
⇒ undefinedgetQuerySet()
⇒ ObjectinvalidateClassCache()
⇒ undefinedcreate(userProps)
⇒ Modelupsert(userProps)
⇒ ModelwithId(id)
⇒ Model ⎮ nullidExists(id)
⇒ Booleanexists(props)
⇒ Booleanget(lookupObj)
⇒ ModelhasId(id)
⇒ Boolean
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](Model#create).
Param | Type | Description |
---|---|---|
props | Object | the properties to instantiate with |
ref
⇒ Object
Returns a reference to the plain JS object in the store.
It contains all the properties that you pass when creating the model,
except for primary keys of many-to-many relationships with a custom accessor.
Make sure never to mutate this.
Kind: instance property of Model
Returns: Object -
a reference to the plain JS object in the store
getClass()
⇒ Model
Gets the [Model](#Model) class or subclass constructor (the class that
instantiated this instance).
Kind: instance method of Model
Returns: Model -
The Model class or subclass constructor used to instantiate
this instance.
getId()
⇒ *
Gets the id value of the current instance by looking up the id attribute.
Kind: instance method of Model
Returns: * -
The id value of the current instance.
toString()
⇒ string
Returns a string representation of the [Model](#Model) instance.
Kind: instance method of Model
Returns: string -
A string representation of this Model instance.
equals(otherModel)
⇒ Boolean
Returns a boolean indicating if otherModel
equals this [Model](#Model) instance.
Equality is determined by shallow comparing their attributes.
This equality is used when you call [update](#Model+update).
You can prevent model updates by returning true
here.
However, a model will always be updated if its relationships are changed.
Kind: instance method of Model
Returns: Boolean -
a boolean indicating if the Model instance's are equal.
Param | Type | Description |
---|---|---|
otherModel | Model | a Model instance to compare |
set(propertyName, value)
⇒ undefined
Updates a property name to given value for this [Model](#Model) instance.
The values are immediately committed to the database.
Kind: instance method of Model
Param | Type | Description |
---|---|---|
propertyName | string | name of the property to set |
value | * | value assigned to the property |
update(userMergeObj)
⇒ undefined
Assigns multiple fields and corresponding values to this [Model](#Model) instance.
The updates are immediately committed to the database.
Kind: instance method of Model
Param | Type | Description |
---|---|---|
userMergeObj | Object | an object that will be merged with this instance. |
refreshFromState()
⇒ undefined
Updates [Model](#Model) instance attributes to reflect the
database state in the current session.
Kind: instance method of Model
delete()
⇒ undefined
Deletes the record for this [Model](#Model) instance.
You'll still be able to access fields and values on the instance.
Kind: instance method of Model
getNextState()
getNextState()
Deprecated
Kind: instance method of Model
Throws:
- Error
Due to deprecation.
static idAttribute
⇒ string
Returns the id attribute of this [Model](#Model).
Kind: static property of Model
Returns: string -
The id attribute of this Model.
static query
Kind: static property of Model
See: {@link Model.getQuerySet}
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.
Kind: static method of Model
Returns: Object -
the options object passed to the database for the table
representing this Model class.
static markAccessed(ids)
⇒ undefined
Manually mark individual instances as accessed.
This allows invalidating selector memoization within mutable sessions.
Kind: static method of Model
Param | Type | Description |
---|---|---|
ids | Array.<*> | Array of primary key values |
static markFullTableScanned()
⇒ undefined
Manually mark this model's table as scanned.
This allows invalidating selector memoization within mutable sessions.
Kind: static method of Model
static markAccessedIndexes(indexes)
⇒ undefined
Manually mark indexes as accessed.
This allows invalidating selector memoization within mutable sessions.
Kind: static method of Model
Param | Type | Description |
---|---|---|
indexes | Array.<Array.<*, *>> | Array of column-value pairs |
static getQuerySet()
⇒ Object
Returns an instance of the model's querySetClass
field.
By default, this will be an empty [QuerySet](QuerySet).
Kind: static method of Model
Returns: Object -
An instance of the model's querySetClass
.
static invalidateClassCache()
⇒ undefined
Kind: static method of Model
static create(userProps)
⇒ Model
Creates a new record in the database, instantiates a [Model](#Model) and returns it.
If you pass values for many-to-many fields, instances are created on the through
model as well.
Kind: static method of Model
Returns: Model -
a new Model instance.
Param | Type | Description |
---|---|---|
userProps | Object | the new Model's properties. |
static upsert(userProps)
⇒ Model
Creates a new or update existing record in the database, instantiates a [Model](#Model) and returns it.
If you pass values for many-to-many fields, instances are created on the through
model as well.
Kind: static method of Model
Returns: Model -
a Model instance.
Param | Type | Description |
---|---|---|
userProps | Object | the required Model's properties. |
static withId(id)
⇒ Model,null
Returns a [Model](#Model) instance for the object with id id
.
Returns null
if the model has no instance with id id
.
You can use [Model#idExists](Model#idExists) to check for existence instead.
Kind: static method of Model
Returns: Model ⎮ null -
Model instance with id id
Throws:
- -
If object with id
id
doesn't exist
Param | Type | Description |
---|---|---|
id | * | the |
static idExists(id)
⇒ Boolean
Returns a boolean indicating if an entity
with the id id
exists in the state.
Kind: static method of Model
Returns: Boolean -
a boolean indicating if entity with id
exists in the state
Since: 0.11.0
Param | Type | Description |
---|---|---|
id | * | a value corresponding to the id attribute of the Model class. |
static exists(props)
⇒ Boolean
Returns a boolean indicating if an entity
with the given props exists in the state.
Kind: static method of Model
Returns: Boolean -
a boolean indicating if entity with props
exists in the state
Param | Type | Description |
---|---|---|
props | * | a key-value that Model instances should have to be considered as existing. |
static get(lookupObj)
⇒ Model
Gets the [Model](#Model) instance that matches properties in lookupObj
.
Throws an error if [Model](#Model) if multiple records match
the properties.
Kind: static method of Model
Returns: Model -
a Model instance that matches the properties in lookupObj
.
Throws:
- Error
If more than one entity matches the properties in
lookupObj
.
Param | Type | Description |
---|---|---|
lookupObj | Object | the properties used to match a single entity. |
static hasId(id)
⇒ Boolean ~~
~~Deprecated
Returns a boolean indicating if an entity
with the id id
exists in the state.
Kind: static method of Model
Returns: Boolean -
a boolean indicating if entity with id
exists in the state
Param | Type | Description |
---|---|---|
id | * | a value corresponding to the id attribute of the Model class. |