---
type: ApiDoc
path: src/Model.js
---
# Model Class

## API

### Instance Methods


#### runAction

Runs a registered action by its name.
- `{String} actionName` 
- `{Object} options` options to be passed to the action function

#### toJSON

Represent this model as a pure JavaScript object.
- `{Object} options` 
- `{Array[String]} options.related` - include any defined relations of the mdoel.  It will return any related models and call toJSON on those as well.
- `{Array[String]} options.attributes` - include any defined attributes of the model.

Returns `{Object}`

#### save

Save the underlying document

#### hasMany

Returns a HasManyRelationship instance for this model.  You can call fetchAll on this to get
instances of the related model.
- `{String|Model} modelNameOrModelClass` 
- `{Object} options` 
- `{String} options.heading` - the parent heading the child relations will be found under.

Returns `{HasManyRelationship}`

#### belongsTo

Returns a BelongsToRelationship instance for this model.  You can call fetch on this to get the instance of the parent Model.
- `{String|Model} modelNameOrModelClass` 
- `{Object} options` 
- `{Function} options.id` a function which will be passed this models underlying document, and should return the id of the parent model this belongs to



### Instance Properties


#### id

Returns the ID of the underlying document

Type `{String}`

#### availableActions

Returns an array of action names that can be run with this model.

Type `{Array[String]}`

#### modelName

Get the name of this class

Type `{String}`

#### meta

Returns the underlying document's meta data, pulled from the frontmatter.

Type `{Object}`

#### defaults

You can override this to return default data to be used when creating data for serialization purposes.
For example you can return { meta: { defaultValue: 1 } } to ensure all instances of this model have that
as their metadata.

#### collection

Provide access to the underlying document's parent collection

Type `{Collection}`

#### inflections

A shortcut to the inflections of this model name

#### prefix

The prefix of the model

Type `{String}`

#### document

The underlying document for this model.

Type `{Document}`

#### title

The title of the underlying document

Type `{String}`

#### relationships



### Static / Class Methods


#### is

Returns true if the document can be used by this model.

When defining your own Model class, you should override this method with your own logic.

#### from

Creates an instance of this Model class from a given Document.
- `{Document} document` 
- `{Object} options`

#### registerQuery

Register an query function with this model class.
- `{String} name` the name of the query
- `{Function} fn` a function which returns models matching the query
- `{Object} options`

#### query

Run a query against the model.  Can either pass the name of a query that has already been registered
or a function that will be used to build the query and return results.

Returns `{CollectionQuery}`

#### action

Register an action function with this model class.  An action is an asynchronous function
that will run being passed the model instance as the first argument.
- `{String} name` 
- `{Function} fn` 
- `{Object} options`



### Static / Class Properties


#### prefix

When defining your own Model class, you can override this method with your own prefix.  By default
it will use the lowercase, pluralized name of the model.  E.g. Book -> books

#### inflections

Provides different variations of the model name, plural, singular, class name style, as well as the actual name of the class.

#### queries

Provides access to any named queries that have been registered with this model.

For example, you could have a query named "all" that would return all of the instances,
or a query named "published" which would return all instances which have meta.status == 'published'

Type `{Map}`

#### availableQueries

Gets a list of named queries registered with this model

#### actions

Provides access to a registry of action functions which can be run on this model.

Type `{Map}`

#### availableActions

the names of the actions that have been registered with this model.

Type `{Array[String]}`

#### defaultCollection

Gets the default collection, which will be used for any queries.

Type `{Collection}`

#### collections

Gets any collections this Model can be associated with.

Type `{Map}`


