Class: ModelInstance

ModelInstance


new ModelInstance(data)

Constructs a new model instance and for models with a default constructor, applies the data in object passed to the instance.

Parameters:
Name Type Description
data Object

Methods


<static> applyData(mdlInst, data)

Applies the data passed in data to the model instance mdlInst.

Parameters:
Name Type Description
mdlInst ModelInstance
data Object

<static> count(filter [, options], callback)

Performs a count of the objects matching the filter in the data store.

Parameters:
Name Type Argument Description
filter Object
options Object <optional>
Properties
Name Type Description
sort string | Array.<string>
limit number
skip number
callback function

<static> create(data, callback)

Creates a new ModelInstance with the passed data and then immediately attempts to save it to the data store.

Parameters:
Name Type Description
data Object
callback function

<static> find(filter [, options], callback)

Perform a filter based search to locate specific model instances.

Parameters:
Name Type Argument Description
filter Object
options Object <optional>
Properties
Name Type Description
sort string | Array.<string>
limit number
skip number
callback function

<static> fromData(data)

Creates a new instance of this Model from the data passed.

Parameters:
Name Type Description
data Object
Returns:

ModelInstance


<static> getById(id, callback)

Retrieves a specific model instance from the data store by ID.

Parameters:
Name Type Description
id string
callback function

<static> loadAll(items, callback)

This is a helper method which allows us to load an array of model instances all at once.

Parameters:
Name Type Description
items Array.<ModelInstance>
callback function

<static> namePath()

Returns the full name of the model, including namespace.

Returns:
Type
string

<static> plugin(the, options)

Registers a plugin for this model. This function will be called immediately with the model itself as the first argument, and the provided options as the second argument.

Parameters:
Name Type Description
the pluginFn

plugin function.

options option

object to pass to the plugin.

Returns:
Type
ModelInstance

<static> post(event, fn)

Specify a function to be invoked following a particular event on this ModelInstance. The handler will always be called with two arguments: the model instance, and a callback.

Parameters:
Name Type Description
event "validate" | "save" | "load" | "remove"
fn function

<static> pre(event, handler)

Specify a handler to be invoked prior to a particular event for this ModelInstance. The handler will always be called with two arguments: the model instance, and a callback function that the handler should call to continue processing. If a pre handler calls the provided callback with an error, the event will not continue.

Parameters:
Name Type Description
event "validate" | "save" | "load" | "remove"
handler function

<static> ref(id)

Creates an unloaded model instance referencing a data store item by id.

Parameters:
Name Type Description
id string
Returns:
Type
ModelInstance

<static> refByKey(key)

Creates an unloaded model instance referencing a data store item by key.

Parameters:
Name Type Description
key string
Returns:
Type
ModelInstance

id()

Returns the ID of this model instance.

Returns:
Type
string

load( [paths], callback)

Loads this object, or sub-objects from this object from the data store. This function can be called while a model instance is already loaded to reload the model instance, or to load specific sub-model-instances.

Parameters:
Name Type Argument Description
paths string <optional>
<repeatable>

Paths to sub-model-instances to load.

callback function

loaded()

Returns whether this model instance is loaded or not.

Returns:
Type
boolean

remove(callback)

Removes this model instance from the data store.

Parameters:
Name Type Description
callback function

save(callback)

Saves this model instance to the data store.

Parameters:
Name Type Description
callback function

toCoo()

Returns a JSON database-serialized version of this model instance.

Returns:
Type
Object

toJSON()

Returns a JSON serialized version of this model instance.

The JSON serialized version is the same as the Coo, with certain internals changed (references) so they are not exposed outside of the DB.

Returns:
Type
Object