Class: ModelInstance

ModelInstance

ModelInstance class is the compiled class from a schema definition. It extends Model.
All models generated are an instance of ModelInstance. It also inherits hooks-fixed
See hooks-fixed for pre and post hooks.

new ModelInstance(data, options)

This would be the constructor for the generated models.

Name Type Description
data Object

the model instance data

options Object

optional creation options

Name Type Description
clone Boolean

Whether to deep clone the incoming data. Default: false.
Make sure you wish to do this as it has performance implications. This is
useful if you are creating multiple instances from same base data and then
wish to modify each instance.

Extends

Members

static ModelInstance.modelName

The name of the model.

static ModelInstance.schema

Schema the schema of this model.

inherited modelNameString

The name the name of the model. This is both a static and instance property.

Example

var schema = plaster.schema({ name: String });
var Cat = plaster.model('Cat', schema);
var kitty = new Cat({ name: 'Zildjian' });
console.log(Cat.modelName); // 'Cat'
console.log(kitty.modelName); // 'Cat'

Schema the schema of this model. This is both a static and instance property.

Methods

Clear the model data.

inherited clearErrors()

Clears all the errors.

inherited getErrors()

Gets the errors object.

inherited hasErrors(){Boolean}

Checks whether we have any errors.

Returns:
Type Description
Boolean true if we have errors, false otherwise.

Helper for console.log. Just invokes default toObject.

Sets data on the model based on the schema.
Accepts a key of property and value for the property, or object representing the data for document.

Example

user.set('fistName', 'Joe');
user.set({ lastName: 'Smith');

inherited toJSON(options){Object}

Similar as toObject but applied when JSON.stringify is called

Name Type Description
options Object

Same options as toObject.

Returns:
Type Description
Object Plain javascript object representation of document.

inherited toObject(options){Object}

Converts this document into a plain javascript object.

Name Type Description
options Object
Name Type Description
transform function

a transform function to apply to the resulting document before returning.

virtuals Boolean

apply virtual getters. Default: false

minimize Boolean

remove empty objects. Default: true

dateToISO Boolean

convert dates to string in ISO format using Date.toISOString(). Default: false

Returns:
Type Description
Object Plain javascript object representation of document.
Examples

var userSchema = plaster.schema({ name: String });
var User = plaster.model('User', userSchema);
var user = new User({name: 'Joe Smith'});
console.log(user); // automatically invokes toObject()

Example with transform option.


var xform = function (doc, ret, options) {
ret.name = ret.name.toUpperCase();
return ret;
};
console.dir(user.toObject({transform: xform}); // { name: 'JOE SMITH' }

Helper for console.log. Alias for inspect.