Class: Model

Model

Model class represents an actual instance of an object. Clients do not create Models. Generated Models from
schema extend this class.

new Model(values, options, schema, name){Model}

Clients do not need to create Models manually.

Name Type Description
values
options
schema
name
Returns:
Type Description
Model

Members

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

Clear the model data.

clearErrors()

Clears all the errors.

getErrors()

Gets the errors object.

hasErrors(){Boolean}

Checks whether we have any errors.

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

inspect()

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');

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.

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' }

toString()

Helper for console.log. Alias for inspect.