Redux-ORM

Redux-ORM

  • Getting Started
  • Docs
  • API
  • GitHub

›API Reference

Introduction

  • Getting Started
  • Installation

Basics

  • Quick Start
  • Relational Fields
  • Reducers
  • Selectors
  • Usage with React

Advanced

  • Complex Selectors
  • Custom Reducer

API Reference

  • API Reference
  • ORM
  • Model
  • QuerySet
  • Session
  • Fields
  • Redux

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 ⇒ Object
      • getClass() ⇒ Model
      • getId() ⇒ *
      • toString() ⇒ string
      • equals(otherModel) ⇒ Boolean
      • set(propertyName, value) ⇒ undefined
      • update(userMergeObj) ⇒ undefined
      • refreshFromState() ⇒ undefined
      • delete() ⇒ undefined
      • getNextState()
    • static
      • idAttribute ⇒ string
      • query
      • options() ⇒ Object
      • markAccessed(ids) ⇒ undefined
      • markFullTableScanned() ⇒ undefined
      • markAccessedIndexes(indexes) ⇒ undefined
      • getQuerySet() ⇒ Object
      • invalidateClassCache() ⇒ undefined
      • create(userProps) ⇒ Model
      • upsert(userProps) ⇒ Model
      • withId(id) ⇒ Model ⎮ null
      • idExists(id) ⇒ Boolean
      • exists(props) ⇒ Boolean
      • get(lookupObj) ⇒ Model
      • hasId(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).

ParamTypeDescription
propsObject

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.

ParamTypeDescription
otherModelModel

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

ParamTypeDescription
propertyNamestring

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

ParamTypeDescription
userMergeObjObject

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()

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

ParamTypeDescription
idsArray.<*>

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

ParamTypeDescription
indexesArray.<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.

ParamTypeDescription
userPropsObject

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.

ParamTypeDescription
userPropsObject

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

ParamTypeDescription
id*

the id of the object to get

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

ParamTypeDescription
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

ParamTypeDescription
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.

ParamTypeDescription
lookupObjObject

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

ParamTypeDescription
id*

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

Last updated on 22.9.2019
← ORMQuerySet →
  • new Model(props)
  • ref⇒ Object
  • getClass()⇒ Model
  • getId()⇒ *
  • toString()⇒ string
  • equals(otherModel)⇒ Boolean
  • set(propertyName, value)⇒ undefined
  • update(userMergeObj)⇒ undefined
  • refreshFromState()⇒ undefined
  • delete()⇒ undefined
  • getNextState()
  • static idAttribute⇒ string
  • static query
  • static options()⇒ Object
  • static markAccessed(ids)⇒ undefined
  • static markFullTableScanned()⇒ undefined
  • static markAccessedIndexes(indexes)⇒ undefined
  • static getQuerySet()⇒ Object
  • static invalidateClassCache()⇒ undefined
  • static create(userProps)⇒ Model
  • static upsert(userProps)⇒ Model
  • static withId(id)⇒ Model,null
  • static idExists(id)⇒ Boolean
  • static exists(props)⇒ Boolean
  • static get(lookupObj)⇒ Model
  • ~~static hasId(id)⇒ Boolean ~~
Redux-ORM
Docs
Getting StartedQuick StartAdvanced scenarios
Community
Stack OverflowGitter
More
GitHubStar
Copyright © 2020 Redux-ORM documentation authors.
Some icons copyright Font Awesome.